質問編集履歴
2
フォント指定に問題があると思いましたのでタイトル等を変更しました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
pillowで日本語字幕を表示させようとすると
|
1
|
+
pillowで日本語字幕を表示させようとするとフォント指定でエラーが発生してしまう。
|
test
CHANGED
@@ -2,262 +2,258 @@
|
|
2
2
|
|
3
3
|
OpenCVとKerasを利用して、webカメラに写されたイラストの顔を検知し判別するプログラムを作りました。
|
4
4
|
|
5
|
-
イラストを判別したときに作品名を画面上に日本語で表示させたいと思い、OpenCVは日本語に対応していないとのことなのでpillowを使って表示させようと思ったのですが、
|
5
|
+
イラストを判別したときに作品名を画面上に日本語で表示させたいと思い、OpenCVは日本語に対応していないとのことなのでpillowを使って表示させようと思ったのですが、フォントのパスの部分
|
6
|
+
|
7
|
+
fon = ImageFont.truetype('C:\Windows\Fonts\meiryo.ttc', 27)
|
8
|
+
|
9
|
+
でエラーが発生してしまいます。
|
10
|
+
|
11
|
+
試しに画像に日本語文字を入れた際に同様のフォントパスを指定してみるとうまく実行できましたのでフォントパスに問題はないと思います。
|
12
|
+
|
13
|
+
どなたか解決方法がわかる方、教えていただきたいです。。
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
イラストの顔を判別するプログラムコードは以下のホームページとほぼ同じ物を書いています
|
18
|
+
|
19
|
+
↓ ↓
|
20
|
+
|
21
|
+
[https://ai-coordinator.jp/real-time-person-detection
|
22
|
+
|
23
|
+
](https://ai-coordinator.jp/real-time-person-detection)
|
24
|
+
|
25
|
+
### 発生している問題・エラーメッセージ
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
Traceback (most recent call last):
|
32
|
+
|
33
|
+
File "C:\Users\Taisei\Anaconda3\envs\opencv\lib\site-packages\PIL\ImageDraw.py", line 309, in draw_text
|
34
|
+
|
35
|
+
mask, offset = font.getmask2(
|
6
36
|
|
7
37
|
AttributeError: 'ImageFont' object has no attribute 'getmask2'
|
8
38
|
|
39
|
+
|
40
|
+
|
41
|
+
During handling of the above exception, another exception occurred:
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
Traceback (most recent call last):
|
46
|
+
|
47
|
+
File "check3.py", line 86, in <module>
|
48
|
+
|
9
|
-
|
49
|
+
main()
|
50
|
+
|
10
|
-
|
51
|
+
File "check3.py", line 60, in main
|
52
|
+
|
53
|
+
draw.text((10, 10), u'これはモナリザです。', fill=(255, 0, 0), fon=fon)
|
54
|
+
|
55
|
+
File "C:\Users\Taisei\Anaconda3\envs\opencv\lib\site-packages\PIL\ImageDraw.py", line 352, in text
|
56
|
+
|
57
|
+
draw_text(ink)
|
58
|
+
|
59
|
+
File "C:\Users\Taisei\Anaconda3\envs\opencv\lib\site-packages\PIL\ImageDraw.py", line 330, in draw_text
|
60
|
+
|
61
|
+
**kwargs
|
62
|
+
|
63
|
+
File "C:\Users\Taisei\Anaconda3\envs\opencv\lib\site-packages\PIL\ImageFont.py", line 140, in getmask
|
64
|
+
|
65
|
+
return self.font.getmask(text, mode)
|
66
|
+
|
11
|
-
UnicodeEncodeError: 'latin-1'
|
67
|
+
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-9: ordinal not in range(256)
|
12
|
-
|
13
|
-
|
68
|
+
|
14
|
-
|
15
|
-
どなたか解決方法がわかる方、教えていただきたいです。。
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
69
|
+
[ WARN:0] terminating async callback
|
20
|
-
|
21
|
-
↓ ↓
|
22
|
-
|
23
|
-
https://ai-coordinator.jp/real-time-person-detection
|
24
|
-
|
25
|
-
※画像の上にpillowを使って日本語を書いてみたところできていたのでpillowのインストール等は問題ないと思います。
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
### 発生している問題・エラーメッセージ
|
30
|
-
|
31
|
-
|
32
70
|
|
33
71
|
```
|
34
72
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
73
|
+
|
74
|
+
|
75
|
+
### 該当のソースコード
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
```python
|
80
|
+
|
81
|
+
import gakusyu2 as face
|
82
|
+
|
83
|
+
import sys, os
|
84
|
+
|
85
|
+
from keras.preprocessing.image import load_img, img_to_array
|
86
|
+
|
87
|
+
import numpy as np
|
88
|
+
|
89
|
+
from PIL import Image, ImageDraw, ImageFont
|
90
|
+
|
91
|
+
import cv2
|
92
|
+
|
93
|
+
import time
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
cascade_path = 'C:\Users\Taisei\Anaconda3\envs\OpenCV\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml'
|
98
|
+
|
99
|
+
cascade = cv2.CascadeClassifier(cascade_path)
|
100
|
+
|
101
|
+
cam = cv2.VideoCapture(1)
|
102
|
+
|
103
|
+
color = (255, 0, 255)
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
image_size = 32
|
108
|
+
|
109
|
+
categories = ["monariza","Lady with an Ermine","Girl with a Pearl Earring"]
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
def main():
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
while(True):
|
118
|
+
|
119
|
+
ret, frame = cam.read()
|
120
|
+
|
121
|
+
facerect = cascade.detectMultiScale(frame, scaleFactor=1.3, minNeighbors=2, minSize=(10, 10))
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
frame_img = Image.fromarray(np.uint8(frame)) # numpy.ndarray to Image
|
126
|
+
|
127
|
+
draw = ImageDraw.Draw(frame_img)
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
fon = ImageFont.truetype('C:\Windows\Fonts\meiryo.ttc', 27)
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
for rect in facerect:
|
136
|
+
|
137
|
+
cv2.rectangle(frame, tuple(rect[0:2]),tuple(rect[0:2] + rect[2:4]), color, thickness=2)
|
138
|
+
|
139
|
+
x = rect[0]
|
140
|
+
|
141
|
+
y = rect[1]
|
142
|
+
|
143
|
+
width = rect[2]
|
144
|
+
|
145
|
+
height = rect[3]
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
cv2.imwrite("frontalface.png", frame)
|
150
|
+
|
151
|
+
img = cv2.imread("frontalface.png")
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
dst = img[y:y+height, x:x+width]
|
156
|
+
|
157
|
+
cv2.imwrite("output.png", dst)
|
158
|
+
|
159
|
+
cv2.imread("output.png")
|
160
|
+
|
161
|
+
X = []
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
img = load_img("output.png", target_size=(image_size,image_size))
|
166
|
+
|
167
|
+
in_data = img_to_array(img)
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
X.append(in_data)
|
172
|
+
|
173
|
+
X = np.array(X)
|
174
|
+
|
175
|
+
X = X.astype("float") / 256
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
model = face.build_model(X.shape[1:])
|
180
|
+
|
181
|
+
model.load_weights("C:\python code\system2.hdf5")
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
pre = model.predict(X)
|
186
|
+
|
187
|
+
print(pre)
|
188
|
+
|
189
|
+
if pre[0][0] > 0.95:
|
190
|
+
|
191
|
+
print(categories[0])
|
192
|
+
|
193
|
+
text = categories[0]
|
194
|
+
|
195
|
+
font = cv2.FONT_HERSHEY_PLAIN
|
196
|
+
|
197
|
+
cv2.putText(frame,text,(rect[0],rect[1]-10),font, 2, color, 2, cv2.LINE_AA)
|
198
|
+
|
199
|
+
draw.text((10, 10), u'これはモナリザです。', fill=(255, 0, 0), fon=fon)
|
200
|
+
|
201
|
+
elif pre[0][1] > 0.95:
|
202
|
+
|
203
|
+
print(categories[1])
|
204
|
+
|
205
|
+
text = categories[1]
|
206
|
+
|
207
|
+
font = cv2.FONT_HERSHEY_PLAIN
|
208
|
+
|
209
|
+
cv2.putText(frame,text,(rect[0],rect[1]-10),font, 2, color, 2, cv2.LINE_AA)
|
210
|
+
|
211
|
+
draw.text((10, 10), u'これは真珠の耳飾りです。', fill=(255, 0, 0), fon=fon)
|
212
|
+
|
213
|
+
elif pre[0][2] > 0.95:
|
214
|
+
|
215
|
+
print(categories[2])
|
216
|
+
|
217
|
+
text = categories[2]
|
218
|
+
|
219
|
+
font = cv2.FONT_HERSHEY_PLAIN
|
220
|
+
|
221
|
+
cv2.putText(frame,text,(rect[0],rect[1]-10),font, 2, color, 2, cv2.LINE_AA)
|
222
|
+
|
223
|
+
draw.text((10, 10), u'これは牛乳を注ぐ女です。', fill=(255, 0, 0), fon=fon)
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
cv2.imshow("Show FLAME Image", frame)
|
228
|
+
|
229
|
+
#time.sleep(0.1)
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
k = cv2.waitKey(1)
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
if k == ord('q'):
|
238
|
+
|
239
|
+
break
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
cam.release()
|
244
|
+
|
245
|
+
cv2.destroyAllWindows()
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
if __name__ == '__main__':
|
52
250
|
|
53
251
|
main()
|
54
252
|
|
55
|
-
File "check3.py", line 60, in main
|
56
|
-
|
57
|
-
draw.text((10, 10), u'これはモナリザです。', fill=(255, 0, 0), fon=fon)
|
58
|
-
|
59
|
-
File "C:\Users\Taisei\Anaconda3\envs\opencv\lib\site-packages\PIL\ImageDraw.py", line 352, in text
|
60
|
-
|
61
|
-
draw_text(ink)
|
62
|
-
|
63
|
-
File "C:\Users\Taisei\Anaconda3\envs\opencv\lib\site-packages\PIL\ImageDraw.py", line 330, in draw_text
|
64
|
-
|
65
|
-
**kwargs
|
66
|
-
|
67
|
-
File "C:\Users\Taisei\Anaconda3\envs\opencv\lib\site-packages\PIL\ImageFont.py", line 140, in getmask
|
68
|
-
|
69
|
-
return self.font.getmask(text, mode)
|
70
|
-
|
71
|
-
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-9: ordinal not in range(256)
|
72
|
-
|
73
|
-
[ WARN:0] terminating async callback
|
74
|
-
|
75
253
|
```
|
76
254
|
|
77
255
|
|
78
256
|
|
79
|
-
### 該当のソースコード
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
```python
|
84
|
-
|
85
|
-
import gakusyu2 as face
|
86
|
-
|
87
|
-
import sys, os
|
88
|
-
|
89
|
-
from keras.preprocessing.image import load_img, img_to_array
|
90
|
-
|
91
|
-
import numpy as np
|
92
|
-
|
93
|
-
from PIL import Image, ImageDraw, ImageFont
|
94
|
-
|
95
|
-
import cv2
|
96
|
-
|
97
|
-
import time
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
cascade_path = 'C:\Users\Taisei\Anaconda3\envs\OpenCV\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml'
|
102
|
-
|
103
|
-
cascade = cv2.CascadeClassifier(cascade_path)
|
104
|
-
|
105
|
-
cam = cv2.VideoCapture(1)
|
106
|
-
|
107
|
-
color = (255, 0, 255)
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
image_size = 32
|
112
|
-
|
113
|
-
categories = ["monariza","Lady with an Ermine","Girl with a Pearl Earring"]
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
def main():
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
while(True):
|
122
|
-
|
123
|
-
ret, frame = cam.read()
|
124
|
-
|
125
|
-
facerect = cascade.detectMultiScale(frame, scaleFactor=1.3, minNeighbors=2, minSize=(10, 10))
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
frame_img = Image.fromarray(np.uint8(frame)) # numpy.ndarray to Image
|
130
|
-
|
131
|
-
draw = ImageDraw.Draw(frame_img)
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
fon = ImageFont.truetype('C:\Windows\Fonts\meiryo.ttc', 27)
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
for rect in facerect:
|
140
|
-
|
141
|
-
cv2.rectangle(frame, tuple(rect[0:2]),tuple(rect[0:2] + rect[2:4]), color, thickness=2)
|
142
|
-
|
143
|
-
x = rect[0]
|
144
|
-
|
145
|
-
y = rect[1]
|
146
|
-
|
147
|
-
width = rect[2]
|
148
|
-
|
149
|
-
height = rect[3]
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
cv2.imwrite("frontalface.png", frame)
|
154
|
-
|
155
|
-
img = cv2.imread("frontalface.png")
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
dst = img[y:y+height, x:x+width]
|
160
|
-
|
161
|
-
cv2.imwrite("output.png", dst)
|
162
|
-
|
163
|
-
cv2.imread("output.png")
|
164
|
-
|
165
|
-
X = []
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
img = load_img("output.png", target_size=(image_size,image_size))
|
170
|
-
|
171
|
-
in_data = img_to_array(img)
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
X.append(in_data)
|
176
|
-
|
177
|
-
X = np.array(X)
|
178
|
-
|
179
|
-
X = X.astype("float") / 256
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
model = face.build_model(X.shape[1:])
|
184
|
-
|
185
|
-
model.load_weights("C:\python code\system2.hdf5")
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
pre = model.predict(X)
|
190
|
-
|
191
|
-
print(pre)
|
192
|
-
|
193
|
-
if pre[0][0] > 0.95:
|
194
|
-
|
195
|
-
print(categories[0])
|
196
|
-
|
197
|
-
text = categories[0]
|
198
|
-
|
199
|
-
font = cv2.FONT_HERSHEY_PLAIN
|
200
|
-
|
201
|
-
cv2.putText(frame,text,(rect[0],rect[1]-10),font, 2, color, 2, cv2.LINE_AA)
|
202
|
-
|
203
|
-
draw.text((10, 10), u'これはモナリザです。', fill=(255, 0, 0), fon=fon)
|
204
|
-
|
205
|
-
elif pre[0][1] > 0.95:
|
206
|
-
|
207
|
-
print(categories[1])
|
208
|
-
|
209
|
-
text = categories[1]
|
210
|
-
|
211
|
-
font = cv2.FONT_HERSHEY_PLAIN
|
212
|
-
|
213
|
-
cv2.putText(frame,text,(rect[0],rect[1]-10),font, 2, color, 2, cv2.LINE_AA)
|
214
|
-
|
215
|
-
draw.text((10, 10), u'これは真珠の耳飾りです。', fill=(255, 0, 0), fon=fon)
|
216
|
-
|
217
|
-
elif pre[0][2] > 0.95:
|
218
|
-
|
219
|
-
print(categories[2])
|
220
|
-
|
221
|
-
text = categories[2]
|
222
|
-
|
223
|
-
font = cv2.FONT_HERSHEY_PLAIN
|
224
|
-
|
225
|
-
cv2.putText(frame,text,(rect[0],rect[1]-10),font, 2, color, 2, cv2.LINE_AA)
|
226
|
-
|
227
|
-
draw.text((10, 10), u'これは牛乳を注ぐ女です。', fill=(255, 0, 0), fon=fon)
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
cv2.imshow("Show FLAME Image", frame)
|
232
|
-
|
233
|
-
#time.sleep(0.1)
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
k = cv2.waitKey(1)
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
if k == ord('q'):
|
242
|
-
|
243
|
-
break
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
cam.release()
|
248
|
-
|
249
|
-
cv2.destroyAllWindows()
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
if __name__ == '__main__':
|
254
|
-
|
255
|
-
main()
|
256
|
-
|
257
|
-
```
|
258
|
-
|
259
|
-
|
260
|
-
|
261
257
|
### 試したこと
|
262
258
|
|
263
259
|
|
1
モジュールのバージョンを追記しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -268,6 +268,16 @@
|
|
268
268
|
|
269
269
|
### 補足情報(FW/ツールのバージョンなど)
|
270
270
|
|
271
|
-
|
272
|
-
|
273
|
-
|
271
|
+
バージョンを確認したところ
|
272
|
+
|
273
|
+
Keras 2.3.1
|
274
|
+
|
275
|
+
TensorFlow 2.0.0
|
276
|
+
|
277
|
+
OpenCV 4.1.0
|
278
|
+
|
279
|
+
Python 3.6.8
|
280
|
+
|
281
|
+
pillow 6.2.1
|
282
|
+
|
283
|
+
でした。
|