@@ -245,16 +245,20 @@ def extract_faces(
245
245
246
246
x = facial_area [0 ]
247
247
y = facial_area [1 ]
248
- w = facial_area [2 ]
249
- h = facial_area [3 ]
248
+ w = facial_area [2 ] - x
249
+ h = facial_area [3 ] - y
250
250
251
- # expand the facial area to be extracted and stay within img.shape limits
252
- x1 = max (0 , x - int ((w * expand_face_area ) / 100 )) # expand left
253
- y1 = max (0 , y - int ((h * expand_face_area ) / 100 )) # expand top
254
- x2 = min (img .shape [1 ], w + int ((w * expand_face_area ) / 100 )) # expand right
255
- y2 = min (img .shape [0 ], h + int ((h * expand_face_area ) / 100 )) # expand bottom
251
+ if expand_face_area > 0 :
252
+ expanded_w = w + int (w * expand_face_area / 100 )
253
+ expanded_h = h + int (h * expand_face_area / 100 )
256
254
257
- facial_img = img [y1 :y2 , x1 :x2 ]
255
+ # overwrite facial area
256
+ x = max (0 , x - int ((expanded_w - w ) / 2 ))
257
+ y = max (0 , y - int ((expanded_h - h ) / 2 ))
258
+ w = min (img .shape [1 ] - x , expanded_w )
259
+ h = min (img .shape [0 ] - y , expanded_h )
260
+
261
+ facial_img = img [y : y + h , x : x + w ]
258
262
259
263
if align is True :
260
264
landmarks = identity ["landmarks" ]
@@ -265,32 +269,17 @@ def extract_faces(
265
269
# mouth_left = landmarks["mouth_left"]
266
270
267
271
# notice that left eye of one is seen on the right from your perspective
268
- facial_img , rotate_angle , rotate_direction = postprocess .alignment_procedure (
272
+ aligned_img , rotate_angle , rotate_direction = postprocess .alignment_procedure (
269
273
img = img , left_eye = right_eye , right_eye = left_eye , nose = nose
270
274
)
271
275
272
276
# find new facial area coordinates after alignment
273
- projected_facial_area = postprocess .rotate_facial_area (
274
- facial_area , rotate_angle , rotate_direction , img .shape
275
- )
276
- # Expand the facial area to be extracted and stay within img.shape limits
277
- x1 = max (
278
- 0 ,
279
- projected_facial_area [0 ] - int ((projected_facial_area [2 ] * expand_face_area ) / 100 ),
280
- )
281
- y1 = max (
282
- 0 ,
283
- projected_facial_area [1 ] - int ((projected_facial_area [3 ] * expand_face_area ) / 100 ),
284
- )
285
- x2 = min (
286
- img .shape [1 ],
287
- projected_facial_area [2 ] + int ((projected_facial_area [2 ] * expand_face_area ) / 100 ),
288
- )
289
- y2 = min (
290
- img .shape [0 ],
291
- projected_facial_area [3 ] + int ((projected_facial_area [3 ] * expand_face_area ) / 100 ),
277
+ rotated_x1 , rotated_y1 , rotated_x2 , rotated_y2 = postprocess .rotate_facial_area (
278
+ (x , y , x + w , y + h ), rotate_angle , rotate_direction , img .shape
292
279
)
293
- facial_img = facial_img [y1 :y2 , x1 :x2 ]
280
+ facial_img = aligned_img [
281
+ int (rotated_y1 ) : int (rotated_y2 ), int (rotated_x1 ) : int (rotated_x2 )
282
+ ]
294
283
295
284
resp .append (facial_img [:, :, ::- 1 ])
296
285
0 commit comments