如何对齐面部以进行预处理?

时间:2017-03-22 01:18:49

标签: opencv dlib

我正在尝试编写一个面部对齐器作为预处理步骤,然后将我的脸部进入神经网络。我通过dlib的{​​{3}}回归树集合实现了(使用Python)面部的地标来预测面部地标估计。我尝试了许多不同类型的转换无济于事。这是我的基本渠道:

  1. 在图像中找到面部并裁剪图像,使其只有脸部:

    image = <some_image_with_one_face>
    face_detector = dlib.get_frontal_face_detector()
    detected_face = face_detector(image)
    w,h = 160 # so that facenet can use it 
    for face_rect in detected_face:
    
        # First crop the face
        left = face_rect.left()
        top = face_rect.top()
        right = face_rect.right()
        bottom = face_rect.bottom()
    
        new_face_rect = dlib.rectangle(0, 0, right-left, bottom-top)
        cropped_image = image[top:bottom, left:right, :].copy()
    
        # Get the the face's pose
        pose_landmarks = face_pose_predictor(cropped_image, new_face_rect)`
    
  2. 找到地标(get_landmark_points返回68x2数组

    landmarks = get_landmark_points(pose_landmarks, N_LANDMARKS, dlib_point=False)
    
  3. 定义一些srcdst点:

    top_of_nose = landmarks[27]
    left_eye = landmarks[36]
    right_eye = landmarks[45]
    bottom_lip = landmarks[57]
    
    src = [top_of_nose, left_eye, right_eye, bottom_lip]
    dst = [[np.int(0.5 * w), np.int(h/3)],\
           [np.int(0.3 * w), np.int(h / 3)],\
           [np.int(0.7 * w), np.int(h / 3)],\
           [np.int(0.5 * w), np.int(h * (2.0/3))]]
    
  4. 估算变换并转换剩余的地标

    transformed_crop = cv2.warpAffine(cropped_image, transform, (w, h))
    
    # Get the transformed landmarks
    transformed_landmarks = np.reshape(cv2.transform(np.expand_dims(landmarks, 1),\
                            transform), (N_LANDMARKS, 2)).astype(np.float32)
    
    # Add the boundary points
    transformed_landmarks = np.append(transformed_landmarks, boundary_points, axis = 0)
    
  5. 这是2张图片的输入和输出: Vahid Kazemi and Josephine Sullivan's

    尽管它有点有效,但它并不完美,因为虽然两个图像中的眼睛和嘴唇的高度相同,但它们的x位置却不同。我想知道是否有某种方法可以改善它,或者我是否使用了错误的技术?如果有人指出我正确的方向,那将会很有帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

deepface封装了dlib和许多其他人脸检测器,并提供了开箱即用的功能。只需调用检测人脸功能即可。

#!pip install deepface
from deepface import DeepFace
import matplotlib.pyplot as plt
backend = 'dlib' opencv, ssd, dlib or mtcnn
img = DeepFace.detectFace("img.jpg", detector_backend = backend)
plt.imshow(img); plt.show()