【问题标题】:Face landmarks detection with dlib使用 dlib 进行人脸地标检测
【发布时间】:2021-03-22 09:24:19
【问题描述】:

我有以下代码:

image_1 = cv2.imread('headshot13-14-2.jpg')
image_1_rgb = cv2.cvtColor(image_1, cv2.COLOR_BGR2RGB)
image_1_gray = cv2.cvtColor(image_1_rgb, cv2.COLOR_BGR2GRAY)
p = "shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(p)

face = detector(image_1_gray)
face_landmarks = predictor(image_1_gray, face)

我收到face = predictor(image_1_gray, face) 行的以下错误:

TypeError: __call__(): incompatible function arguments. The following argument types are supported:
    1. (self: dlib.shape_predictor, image: array, box: dlib.rectangle) -> dlib.full_object_detection

但是,我检查了人脸的类型(它是 dlib.rectangles),并且 image_1_gray 是一个 numpy ndarray。 有谁知道为什么这个错误仍然出现?

【问题讨论】:

    标签: python opencv computer-vision dlib facial-landmark-alignment


    【解决方案1】:

    face 变量可能包含多个值,因此您需要为每个值使用predictor

    例如:

    for (i, rect) in enumerate(face):
        face_landmarks = predictor(image_1_gray, rect)
    

    在脸上显示检测到的值:

    shp = face_utils.shape_to_np(face_landmarks)
    

    要使用face_utils,需要安装imutils

    很可能您的shp 可变大小是(68, 2)。其中68 是人脸检测点,2(x, y) 坐标元组。

    现在,在图像上绘制检测到的人脸:

    • 首先,获取坐标

      • x = rect.left()
        y = rect.top()
        w = rect.right() - x
        h = rect.bottom() - y
        
    • 绘制坐标:

      • cv2.rectangle(image_1, (x, y), (x + w, y + h), (0, 255, 0), 2)
        
    • 图像中可能有多个人脸,因此您可能需要标记它们

      • cv2.putText(image_1, "Face #{}".format(i + 1), (x - 10, y - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
        
    • 现在在脸上画出 68 个点:

      • for (x, y) in shp:
            cv2.circle(image_1, (x, y), 1, (0, 0, 255), -1)
        

    结果:

    代码:


    for (i, rect) in enumerate(face):
        face_landmarks = predictor(image_1_gray, rect)
        shp = face_utils.shape_to_np(face_landmarks)
        x = rect.left()
        y = rect.top()
        w = rect.right() - x
        h = rect.bottom() - y
        cv2.rectangle(image_1, (x, y), (x + w, y + h), (0, 255, 0), 2)
        cv2.putText(image_1, "Face #{}".format(i + 1), (x - 10, y - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
        for (x, y) in shp:
            cv2.circle(image_1, (x, y), 1, (0, 0, 255), -1)
    
    cv2.imshow("face", image_1)
    cv2.waitKey(0)
    

    你也可以看看tutorial

    【讨论】:

      猜你喜欢
      • 2017-10-18
      • 2016-07-16
      • 2017-06-01
      • 2016-07-18
      • 2017-10-11
      • 2017-07-19
      • 2018-06-09
      • 2017-01-18
      • 2016-07-09
      相关资源
      最近更新 更多