@@ -212,7 +212,6 @@ def extract_faces(
212
212
align : bool = True ,
213
213
allow_upscaling : bool = True ,
214
214
expand_face_area : int = 0 ,
215
- align_first : bool = False ,
216
215
) -> list :
217
216
"""
218
217
Extract detected and aligned faces
@@ -223,8 +222,6 @@ def extract_faces(
223
222
align (bool): enable or disable alignment
224
223
allow_upscaling (bool): allowing up-scaling
225
224
expand_face_area (int): expand detected facial area with a percentage
226
- align_first (bool): set this True to align first and detect second
227
- this can be applied only if input image has just one face
228
225
"""
229
226
resp = []
230
227
@@ -238,12 +235,6 @@ def extract_faces(
238
235
img_path = img , threshold = threshold , model = model , allow_upscaling = allow_upscaling
239
236
)
240
237
241
- if align_first is True and len (obj ) > 1 :
242
- logger .warn (
243
- f"Even though align_first is set to True, there are { len (obj )} faces in input image."
244
- "Align first functionality can be applied only if there is single face in the input"
245
- )
246
-
247
238
if not isinstance (obj , dict ):
248
239
return resp
249
240
@@ -263,10 +254,7 @@ def extract_faces(
263
254
x2 = min (img .shape [1 ], w + int ((w * expand_face_area ) / 100 )) # expand right
264
255
y2 = min (img .shape [0 ], h + int ((h * expand_face_area ) / 100 )) # expand bottom
265
256
266
- if align_first is False or (align_first is True and len (obj ) > 1 ):
267
- facial_img = img [y1 :y2 , x1 :x2 ]
268
- else :
269
- facial_img = img .copy ()
257
+ facial_img = img [y1 :y2 , x1 :x2 ]
270
258
271
259
if align is True :
272
260
landmarks = identity ["landmarks" ]
@@ -275,19 +263,33 @@ def extract_faces(
275
263
nose = landmarks ["nose" ]
276
264
# mouth_right = landmarks["mouth_right"]
277
265
# mouth_left = landmarks["mouth_left"]
266
+
267
+ # notice that left eye of one is seen on the right from your perspective
278
268
facial_img , rotate_angle , rotate_direction = postprocess .alignment_procedure (
279
- facial_img , right_eye , left_eye , nose
269
+ img = img , left_eye = right_eye , right_eye = left_eye , nose = nose
280
270
)
281
271
282
- if align_first is True and len ( obj ) == 1 :
283
- facial_area = postprocess .rotate_facial_area (
272
+ # find new facial area coordinates after alignment
273
+ projected_facial_area = postprocess .rotate_facial_area (
284
274
facial_area , rotate_angle , rotate_direction , img .shape
285
275
)
286
276
# Expand the facial area to be extracted and stay within img.shape limits
287
- x1 = max (0 , facial_area [0 ] - int ((facial_area [2 ] * expand_face_area ) / 100 ))
288
- y1 = max (0 , facial_area [1 ] - int ((facial_area [3 ] * expand_face_area ) / 100 ))
289
- x2 = min (img .shape [1 ], facial_area [2 ] + int ((facial_area [2 ] * expand_face_area ) / 100 ))
290
- y2 = min (img .shape [0 ], facial_area [3 ] + int ((facial_area [3 ] * expand_face_area ) / 100 ))
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 ),
292
+ )
291
293
facial_img = facial_img [y1 :y2 , x1 :x2 ]
292
294
293
295
resp .append (facial_img [:, :, ::- 1 ])
0 commit comments