serengil / deepface

A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python
https://www.youtube.com/watch?v=WnUVYQP4h44&list=PLsS_1RYmYQQFdWqxQggXHynP1rqaYXv_E&index=1
MIT License
11.01k stars 1.93k forks source link

[FEATURE]: extract_faces return face center point info or stop further enlargement if face center point can't be kept at image center #1207

Closed deepfree2023 closed 2 months ago

deepfree2023 commented 2 months ago

Description

otherwise a large expand_percentage value will lead to bad results, face will not be centered and stay at one side of the result.

Additional Info

No response

serengil commented 2 months ago

extract_faces is already returning facial area as

face_objs = DeepFace.extract_faces(img)
for face_obj in face_objs:
   x = ["facial_area"]["x"]
   y = ["facial_area"]["y"]
   w = ["facial_area"]["w"]
   h = ["facial_area"]["h"]

then, face center point is

face_center = (int(y + h/2), int(x + w/2))
serengil commented 2 months ago

expand_area is also an optional argument and its default value is 0.

i do not think this is a necessary change.

deepfree2023 commented 2 months ago

It's right that face center point can be easily obtained in normal cases.

But not in the case I've described (real scenario includes face swapping, if more environmental info is required, expand_percentage need to be a large value). In this case, the calculated face center point using x,y,w,h obtained from extract_faces will not be the same as the real face center point before expanding.

Because in the simple expanding algorithm, if one side reaches image edge, the expanding area will stop increase at that side, but still increase in the opposite side. Thus the face center point will keep on drifting towards the opposite side.

deepfree2023 commented 2 months ago

The problem can be resolved by using trial and error method to set a valid expand_percentage value. But in batch processing scenario, it's not possible to adjust expand_percentage for each run.

serengil commented 2 months ago

I limited the facial area with boundaries if it exceeds in purpose. Because I can monitor that using this approach has an impact on facial recognition accuracy. I prefer not to terminate expanding if it exceeds the boundaries.

deepfree2023 commented 2 months ago

I see. But I've got a lot of result images from extract_faces with head + neck and chest, even more body part, othertimes maybe small head under lots of sky... Since I have to generate square image from the result images for next step processing, without real face center info, I don't know how to achieve that.

Currently I modified deepface source code to return extra real face center info before expanding, it works quite well.

deepfree2023 commented 2 months ago

For different image size, head size, head position, the largest expand_percentage value without changing the face center position is different, this makes single-expand_percentage-for-all method not working.

serengil commented 2 months ago

Customization the source code is fine in your case. But again I do not think that change is required in the source code of the repo.