質問編集履歴
1
誤字
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
[Pycharm]Dlibの顔検出
|
1
|
+
[Pycharm]Dlibの顔検出の複数人検出で困っています
|
test
CHANGED
@@ -1,22 +1,282 @@
|
|
1
1
|
・前提・実現したいこと
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
複数人の顔検出を行い、特定の人物にだけ画像貼り付けを行う。
|
3
|
+
pycharmでdlibを使い、複数人の顔検出を行い、特定の人物にだけ画像貼り付けを行う。
|
6
4
|
|
7
5
|
|
8
6
|
|
9
7
|
・発生している問題・エラーメッセージ
|
10
8
|
|
11
|
-
一人の顔しか検出されない。
|
9
|
+
一人の顔しか検出されない。for文の変数がわからない。
|
10
|
+
|
11
|
+
|
12
12
|
|
13
13
|
|
14
14
|
|
15
15
|
・該当のソースコード
|
16
16
|
|
17
|
+
import cv2
|
18
|
+
|
19
|
+
import dlib
|
20
|
+
|
21
|
+
from datetime import datetime
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
IMAGE_PATH = "./niko.png"
|
26
|
+
|
27
|
+
CAPTURE_SCALE = 0.5
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
def main():
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
detector = dlib.get_frontal_face_detector()
|
36
|
+
|
37
|
+
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
cap = cv2.VideoCapture(0)
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
while True:
|
46
|
+
|
47
|
+
_, frame = cap.read()
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
frame = img_resize(frame, CAPTURE_SCALE)
|
52
|
+
|
53
|
+
h, w, _ = frame.shape
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
img = frame
|
58
|
+
|
59
|
+
dets = detector(frame[:, :, ::-1])
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
if len(dets) > 0:
|
64
|
+
|
65
|
+
//for w,h in dets:
|
66
|
+
|
67
|
+
parts = predictor(frame, dets[0]).parts()
|
68
|
+
|
69
|
+
distance, pos = calc_distance_and_pos(img, parts)
|
70
|
+
|
71
|
+
icon, icon_w, icon_h = load_icon(IMAGE_PATH, distance)
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
if (pos != None) and (distance > 0.0):
|
76
|
+
|
77
|
+
//for w,h in dets:
|
78
|
+
|
79
|
+
x = pos.x - int(icon_w / 2)
|
80
|
+
|
81
|
+
y = pos.y - int(icon_h / 2)
|
82
|
+
|
83
|
+
if (0 <= y) and (y <= (h - int(icon_h))) and (0 <= x) and (x <= (w - int(icon_w))):
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
img = merge_images(img, icon, x, y)
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
cv2.imshow("camera", img)
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
k = cv2.waitKey(1) & 0xff
|
96
|
+
|
97
|
+
if k == ord('q'):
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
break
|
102
|
+
|
103
|
+
elif k == ord('p'):
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
save_image(img)
|
108
|
+
|
109
|
+
cv2.imshow("saved", img)
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
cap.release()
|
116
|
+
|
117
|
+
cv2.destroyAllWindows()
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
def load_icon(path, distance):
|
126
|
+
|
127
|
+
icon = cv2.imread(path, -1)
|
128
|
+
|
129
|
+
icon_height, _ = icon.shape[:2]
|
130
|
+
|
131
|
+
icon = img_resize(icon, float(distance * 1.5 / icon_height))
|
132
|
+
|
133
|
+
icon_h, icon_w = icon.shape[:2]
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
return icon, icon_w, icon_h
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
def img_resize(img, scale):
|
146
|
+
|
147
|
+
h, w = img.shape[:2]
|
148
|
+
|
149
|
+
img = cv2.resize(img, (int(w * scale), int(h * scale)))
|
150
|
+
|
151
|
+
return img
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
def calc_distance_and_pos(img, parts):
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
cnt = 0
|
164
|
+
|
165
|
+
pos = None
|
166
|
+
|
167
|
+
p1 = None
|
168
|
+
|
169
|
+
distance = 0.0
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
for i in parts:
|
174
|
+
|
175
|
+
if (cnt == 0):
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
p1 = i
|
180
|
+
|
181
|
+
if (cnt == 16):
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
distance = ((p1.x - i.x) ** 2 + (p1.y - i.y) ** 2) ** 0.5
|
186
|
+
|
187
|
+
if (cnt == 33):
|
188
|
+
|
189
|
+
pos = i
|
190
|
+
|
191
|
+
cv2.putText(img, str(cnt), (i.x, i.y), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 255), thickness=1,
|
192
|
+
|
193
|
+
lineType=cv2.LINE_8)
|
194
|
+
|
195
|
+
cv2.circle(img, (i.x, i.y), 1, (255, 0, 0), -1)
|
196
|
+
|
197
|
+
cnt = cnt + 1
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
return distance, pos
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
def save_image(img):
|
210
|
+
|
211
|
+
date = datetime.now().strftime("%Y%m%d_%H%M%S")
|
212
|
+
|
213
|
+
path = "./" + date + ".png"
|
214
|
+
|
215
|
+
cv2.imwrite(path, img)
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
def merge_images(bg, fg_alpha, s_x, s_y):
|
224
|
+
|
225
|
+
alpha = fg_alpha[:, :, 3]
|
226
|
+
|
227
|
+
alpha = cv2.cvtColor(alpha, cv2.COLOR_GRAY2BGR)
|
228
|
+
|
229
|
+
alpha = alpha / 255.0
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
fg = fg_alpha[:, :, :3]
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
f_h, f_w, _ = fg.shape
|
238
|
+
|
239
|
+
b_h, b_w, _ = bg.shape
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
print("f_w:{} f_h:{} b_w:{} b_h:{} s({}, {})".format(f_w, f_h, b_w, b_h, s_x, s_y))
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
bg[s_y:f_h + s_y, s_x:f_w + s_x] = (bg[s_y:f_h + s_y, s_x:f_w + s_x] * (1.0 - alpha)).astype(
|
250
|
+
|
251
|
+
'uint8')
|
252
|
+
|
253
|
+
bg[s_y:f_h + s_y, s_x:f_w + s_x] = (bg[s_y:f_h + s_y, s_x:f_w + s_x] + (fg * alpha)).astype('uint8')
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
return bg
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
if __name__ == '__main__':
|
264
|
+
|
265
|
+
main()
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
この方のを参照しております。
|
270
|
+
|
17
271
|
http://rikoubou.hatenablog.com/entry/2019/05/15/172615
|
18
272
|
|
273
|
+
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
|
278
|
+
|
19
|
-
|
279
|
+
//の部分のfor文を完成させて、複数人検出がしたいのですが、変数がわかりません。
|
20
280
|
|
21
281
|
|
22
282
|
|