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
14.34k stars 2.2k forks source link

Face alignment is working correctly? #390

Closed ojrandom1 closed 2 years ago

ojrandom1 commented 2 years ago

Is this a bug or a wrong usage of deepface?

The aligned image (img_aligned) does not show up as aligned in

from deepface import DeepFace
from deepface.detectors import FaceDetector
import cv2

detector_name = 'mtcnn'
detector = FaceDetector.build_model('mtcnn')

img = cv2.imread("tests/dataset/img31.jpg")
img_aligned = cv2.imread("tests/dataset/aligned.jpg")
try:
    faces = FaceDetector.detect_faces(detector, detector_name, img, align=True)
except:
    print("error")

for face, (x, y, w, h) in faces:
    region = [x, y, w, h]
    custom_face = img[y:y+h, x:x+w]
    cv2.imwrite("test/aligned.jpg", custom_face)
serengil commented 2 years ago

could you share the aligned version here?

serengil commented 2 years ago

Try this:

img = DeepFace.detectFace("tests/dataset/img31.jpg")
cv2.imwrite("base_image.jpg", img*255)
ojrandom1 commented 2 years ago

Yes,

detector_name = 'mtcnn' detector = FaceDetector.build_model('mtcnn')

img = DeepFace.detectFace("tests/dataset/img31.jpg") cv2.imwrite("base_image.jpg", img*255)

works as expected and produces an aligned image, see

base_image

What is the recommended solution to have have aligned faces for the face representations for example using an approach like this.

    detector_name = 'mtcnn'
    detector = FaceDetector.build_model('mtcnn')

    model = DeepFace.build_model('ArcFace')

    img = cv2.imread(path)
    try:
        faces = FaceDetector.detect_faces(detector, detector_name, img, align=True)
    except:
        print("error")

    for face, (x, y, w, h) in faces:
        if w < 100
            continue
        region = [x, y, w, h]
        custom_face = img[y:y+h, x:x+w]

        input_shape = functions.find_input_shape(model)
        input_shape_x = input_shape[0]
        input_shape_y = input_shape[1]

        custom_face = functions.preprocess_face(
            img=custom_face,
            target_size=(input_shape_y, input_shape_x),
            enforce_detection=False,
            detector_backend=detector_name,
            align = True)
        representation = []
        if custom_face.shape[1:3] == input_shape:
            representation = self.model.predict(custom_face)[0, :]
        else:
            continue