質問編集履歴

1

修正依頼

2020/07/27 14:01

投稿

Tetsuya_Kosaka
Tetsuya_Kosaka

スコア1

test CHANGED
File without changes
test CHANGED
@@ -67,3 +67,241 @@
67
67
 
68
68
 
69
69
  理由がわかる方、教えていただけたら幸いです。宜しくお願い致します。
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+ ### 追加事項
78
+
79
+ programの全体を載せます。宜しくお願い致します。
80
+
81
+
82
+
83
+ ```python
84
+
85
+ import cv2
86
+
87
+ import numpy as np
88
+
89
+ import math
90
+
91
+ import dlib
92
+
93
+ import os
94
+
95
+ import glob
96
+
97
+ import time
98
+
99
+
100
+
101
+ def fitting_rotated_image(img, angle):
102
+
103
+ # 画像の縦、横のサイズを取得する
104
+
105
+ height, width = img.shape[:2]
106
+
107
+ # 画像の中心値を取得
108
+
109
+ center = (int(width/2), int(height/2))
110
+
111
+ # 度からラジアンに変換(deg to rad)
112
+
113
+ radians = np.deg2rad(angle)
114
+
115
+ # 回転の変換行列
116
+
117
+ M = cv2.getRotationMatrix2D(center, angle, 1.0)
118
+
119
+
120
+
121
+ new_width = int(abs(np.sin(radians) * height) + abs(np.cos(radians) * width))
122
+
123
+ new_height = int(abs(np.sin(radians) * width) + abs(np.cos(radians) * height))
124
+
125
+
126
+
127
+ M[0,2] += int((new_width-width)/2)
128
+
129
+ M[1,2] += int((new_height-height)/2)
130
+
131
+
132
+
133
+ # アファイン変換した画像を返す
134
+
135
+ return cv2.warpAffine(img, M, (new_width, new_height))
136
+
137
+
138
+
139
+ start = time.time()
140
+
141
+
142
+
143
+ # 顔の位置を取得する
144
+
145
+ detector = dlib.get_frontal_face_detector()
146
+
147
+ # 顔の68箇所の位置の座標を取得する
148
+
149
+ predictor = dlib.shape_predictor('./shape_predictor_68_face_landmarks.dat')
150
+
151
+
152
+
153
+ # ディレクトリ内の画像を指定
154
+
155
+ image_list = glob.glob('./~/*')
156
+
157
+ count = 1
158
+
159
+ pro_time = 0
160
+
161
+ # 新しいディレクトリを作成
162
+
163
+ os.makedirs('~', exist_ok = True)
164
+
165
+ # for文で一枚ずつ処理していく
166
+
167
+ for image in image_list:
168
+
169
+ pro_start = time.time()
170
+
171
+ pro_time += 1
172
+
173
+ print("Processint Time :", pro_time)
174
+
175
+ # 処理する画像を指定
176
+
177
+ im = cv2.imread(image, cv2.IMREAD_COLOR)
178
+
179
+
180
+
181
+ # 画像が見つからない場合
182
+
183
+ if im is None:
184
+
185
+ print("count:", count, '画像が見つかりません')
186
+
187
+ #exit()
188
+
189
+ continue
190
+
191
+
192
+
193
+ rects = detector(im, 1)
194
+
195
+
196
+
197
+ if len(rects) == 0:
198
+
199
+ print("count:", count, '顔が抽出されませんでした')
200
+
201
+ #exit()
202
+
203
+ continue
204
+
205
+
206
+
207
+ for index, rect in enumerate(rects):
208
+
209
+
210
+
211
+ # 顔だけ切り出す
212
+
213
+ rectWidth = rect.width()
214
+
215
+ rectHeight = rect.height()
216
+
217
+ rectCenter = rect.center()
218
+
219
+ x_start = rectCenter.x - rectWidth
220
+
221
+ x_end = x_start + rectWidth * 2
222
+
223
+ y_start = rectCenter.y - rectHeight
224
+
225
+ y_end = y_start + rectHeight * 2
226
+
227
+ face_im = im[y_start:y_end, x_start:x_end]
228
+
229
+
230
+
231
+ # 顔の角度を修正
232
+
233
+ points = []
234
+
235
+ for point in predictor(im, rect).parts():
236
+
237
+ points.append([int(point.x), int(point.y)])
238
+
239
+
240
+
241
+ x_diff = points[45][0] - points[36][0]
242
+
243
+ y_diff = points[45][1] - points[36][1]
244
+
245
+ angle = math.degrees(math.atan2(y_diff, x_diff))
246
+
247
+ rotated_im = fitting_rotated_image(face_im, angle)
248
+
249
+
250
+
251
+ # 回転後の画像で顔検出して画像保存
252
+
253
+ rotated_rects = detector(rotated_im, 1)
254
+
255
+ if len(rotated_rects) == 0:
256
+
257
+ print("count:", count, '顔が抽出されませんでした')
258
+
259
+ continue
260
+
261
+
262
+
263
+ else:
264
+
265
+ rotated_rect = rotated_rects[0]
266
+
267
+ x_start = rotated_rect.left()
268
+
269
+ x_end = rotated_rect.right()
270
+
271
+ y_start = rotated_rect.top()
272
+
273
+ y_end = rotated_rect.bottom()
274
+
275
+ cropped_im = rotated_im[y_start:y_end, x_start:x_end]
276
+
277
+
278
+
279
+ cv2.imwrite('./~.jpg'.format(count), cropped_im)
280
+
281
+ pro_finish = time.time()
282
+
283
+
284
+
285
+ print("count", count, "finish.")
286
+
287
+ print("Processing Time:", pro_finish - pro_start, "sec")
288
+
289
+
290
+
291
+ count += 1
292
+
293
+
294
+
295
+ print("\n")
296
+
297
+
298
+
299
+ finish = time.time()
300
+
301
+
302
+
303
+ print("All Processing Time:", finish - start, "sec")
304
+
305
+ print(count, "face")
306
+
307
+ ```