【问题标题】:How do I align a face for preprocessing?如何对齐面部以进行预处理?
【发布时间】:2017-08-13 22:38:31
【问题描述】:

在将我的脸输入神经网络之前,我正在尝试编写一个人脸对齐器作为预处理步骤。我通过dlibVahid Kazemi and Josephine Sullivan's 回归树集合的实现找到了(使用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)
    

这是 2 张图像的输入和输出:

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

【问题讨论】:

标签: opencv dlib


【解决方案1】:

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()

【讨论】:

    猜你喜欢
    • 2017-11-01
    • 2016-03-10
    • 1970-01-01
    • 2023-03-03
    • 2020-05-22
    • 2017-07-08
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    相关资源
    最近更新 更多