前提・実現したいこと
OpenCVとKerasを利用して、webカメラに写されたイラストの顔を検知し判別するプログラムを作りました。
イラストを判別したときに作品名を画面上に日本語で表示させたいと思い、OpenCVは日本語に対応していないとのことなのでpillowを使って表示させようと思ったのですが、フォントのパスの部分
fon = ImageFont.truetype('C:\Windows\Fonts\meiryo.ttc', 27)
でエラーが発生してしまいます。
試しに画像に日本語文字を入れた際に同様のフォントパスを指定してみるとうまく実行できましたのでフォントパスに問題はないと思います。
どなたか解決方法がわかる方、教えていただきたいです。。
イラストの顔を判別するプログラムコードは以下のホームページとほぼ同じ物を書いています
↓ ↓
https://ai-coordinator.jp/real-time-person-detection
発生している問題・エラーメッセージ
Traceback (most recent call last): File "C:\Users\Taisei\Anaconda3\envs\opencv\lib\site-packages\PIL\ImageDraw.py", line 309, in draw_text mask, offset = font.getmask2( AttributeError: 'ImageFont' object has no attribute 'getmask2' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "check3.py", line 86, in <module> main() File "check3.py", line 60, in main draw.text((10, 10), u'これはモナリザです。', fill=(255, 0, 0), fon=fon) File "C:\Users\Taisei\Anaconda3\envs\opencv\lib\site-packages\PIL\ImageDraw.py", line 352, in text draw_text(ink) File "C:\Users\Taisei\Anaconda3\envs\opencv\lib\site-packages\PIL\ImageDraw.py", line 330, in draw_text **kwargs File "C:\Users\Taisei\Anaconda3\envs\opencv\lib\site-packages\PIL\ImageFont.py", line 140, in getmask return self.font.getmask(text, mode) UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-9: ordinal not in range(256) [ WARN:0] terminating async callback
該当のソースコード
python
1import gakusyu2 as face 2import sys, os 3from keras.preprocessing.image import load_img, img_to_array 4import numpy as np 5from PIL import Image, ImageDraw, ImageFont 6import cv2 7import time 8 9cascade_path = 'C:\Users\Taisei\Anaconda3\envs\OpenCV\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml' 10cascade = cv2.CascadeClassifier(cascade_path) 11cam = cv2.VideoCapture(1) 12color = (255, 0, 255) 13 14image_size = 32 15categories = ["monariza","Lady with an Ermine","Girl with a Pearl Earring"] 16 17def main(): 18 19 while(True): 20 ret, frame = cam.read() 21 facerect = cascade.detectMultiScale(frame, scaleFactor=1.3, minNeighbors=2, minSize=(10, 10)) 22 23 frame_img = Image.fromarray(np.uint8(frame)) # numpy.ndarray to Image 24 draw = ImageDraw.Draw(frame_img) 25 26 fon = ImageFont.truetype('C:\Windows\Fonts\meiryo.ttc', 27) 27 28 for rect in facerect: 29 cv2.rectangle(frame, tuple(rect[0:2]),tuple(rect[0:2] + rect[2:4]), color, thickness=2) 30 x = rect[0] 31 y = rect[1] 32 width = rect[2] 33 height = rect[3] 34 35 cv2.imwrite("frontalface.png", frame) 36 img = cv2.imread("frontalface.png") 37 38 dst = img[y:y+height, x:x+width] 39 cv2.imwrite("output.png", dst) 40 cv2.imread("output.png") 41 X = [] 42 43 img = load_img("output.png", target_size=(image_size,image_size)) 44 in_data = img_to_array(img) 45 46 X.append(in_data) 47 X = np.array(X) 48 X = X.astype("float") / 256 49 50 model = face.build_model(X.shape[1:]) 51 model.load_weights("C:\python code\system2.hdf5") 52 53 pre = model.predict(X) 54 print(pre) 55 if pre[0][0] > 0.95: 56 print(categories[0]) 57 text = categories[0] 58 font = cv2.FONT_HERSHEY_PLAIN 59 cv2.putText(frame,text,(rect[0],rect[1]-10),font, 2, color, 2, cv2.LINE_AA) 60 draw.text((10, 10), u'これはモナリザです。', fill=(255, 0, 0), fon=fon) 61 elif pre[0][1] > 0.95: 62 print(categories[1]) 63 text = categories[1] 64 font = cv2.FONT_HERSHEY_PLAIN 65 cv2.putText(frame,text,(rect[0],rect[1]-10),font, 2, color, 2, cv2.LINE_AA) 66 draw.text((10, 10), u'これは真珠の耳飾りです。', fill=(255, 0, 0), fon=fon) 67 elif pre[0][2] > 0.95: 68 print(categories[2]) 69 text = categories[2] 70 font = cv2.FONT_HERSHEY_PLAIN 71 cv2.putText(frame,text,(rect[0],rect[1]-10),font, 2, color, 2, cv2.LINE_AA) 72 draw.text((10, 10), u'これは牛乳を注ぐ女です。', fill=(255, 0, 0), fon=fon) 73 74 cv2.imshow("Show FLAME Image", frame) 75 #time.sleep(0.1) 76 77 k = cv2.waitKey(1) 78 79 if k == ord('q'): 80 break 81 82 cam.release() 83 cv2.destroyAllWindows() 84 85if __name__ == '__main__': 86 main()
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
バージョンを確認したところ
Keras 2.3.1
TensorFlow 2.0.0
OpenCV 4.1.0
Python 3.6.8
pillow 6.2.1
でした。
回答1件
あなたの回答
tips
プレビュー