0

我有以下代码:

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。有谁知道为什么这个错误仍然出现?

4

1 回答 1

0

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)

你也可以看看教程

于 2020-12-10T23:44:39.707 回答