使用opencv dnn人脸检测器检测检测到的人脸图像内的人脸界标

时间:2019-07-23 12:29:37

标签: python opencv deep-learning face-detection facial-identification

我试图检测人脸的68个面部标志。我使用https://www.pyimagesearch.com/2018/02/26/face-detection-with-opencv-and-deep-learning/中的OpenCV dnn人脸检测器检测到人脸 人脸检测过程成功完成,这是我的代码:

# import the necessary packages
import numpy as np
import argparse
import cv2
import dlib

ap = argparse.ArgumentParser()
ap.add_argument("-c", "--confidence", type=float, default=0.5,
                help="minimum probability to filter weak detections")
args = vars(ap.parse_args())

# load our serialized model from disk
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe("D:\deep-learning-face-detection\deploy.prototxt.txt",
                               r"D:\deep-learning-face-detection\res10_300x300_ssd_iter_140000.caffemodel")

image = cv2.imread("image\path\jpg")
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0,
                             (300, 300), (104.0, 177.0, 123.0))

print("[INFO] computing object detections...")
net.setInput(blob)
detections = net.forward()

# loop over the detections
for i in range(0, detections.shape[2]):
    # extract the confidence (i.e., probability) associated with the
    # prediction
    confidence = detections[0, 0, i, 2]

    # filter out weak detections by ensuring the `confidence` is
    # greater than the minimum confidence
    if confidence > args["confidence"]:
        # compute the (x, y)-coordinates of the bounding box for the
        # object
        box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
        (startX, startY, endX, endY) = box.astype("int")

        # draw the bounding box of the face along with the associated
        # probability
        text = "Face#{}".format(i)
        y = startY - 10 if startY - 10 > 10 else startY + 10
        cv2.rectangle(image, (startX, startY), (endX, endY),
                      (0, 0, 255), 2)
        cv2.putText(image, text, (startX, y),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)

    # show the output image
cv2.imshow("Output", image)
cv2.waitKey(0)

但是当我尝试检测面部内部的面部标志时,如下所示:

predictor = dlib.shape_predictor("D:\shape_predictor_68_face_landmarks.dat")
shape = predictor(image, detections)
vec = []
for i in range(68):
    v = shape.part(i)
    vec.append(v)
print(vec)

我收到以下错误消息

  

形状=预测变量(图像,检测物)TypeError:调用():   不兼容的函数参数。以下参数类型是   支持的:       1。((自身:dlib.shape_predictor,图像:数组,框:dlib.rectangle)-> dlib.full_object_detection

     

调用了:,   数组([[[0,0,0],           [0,0,0],           [0,0,0],           ...,

当我使用OpenCV dnn脸部检测器和MTCNN来回dlib脸部检测器时出现错误消息,但使用Haar级联脸部检测器无法显示该错误消息,并且成功检测到了脸部界标。我想用上述代码在OpenCV dnn面部检测器中检测面部界标,因为它的准确性很高,因为高遮挡一致性,Haar级联面部检测器对我的面部图像不利。谁能帮我。

3 个答案:

答案 0 :(得分:2)

这将解决问题:

shape = predictor(image,dlib.rectangle(startX, startY, endX, endY))

答案 1 :(得分:0)

如果您想要这样的话: example 您可以使用面部识别library中的face_recognition.face_landmarks(image)函数,它比dnn检测器容易使用。 希望我能帮上忙。

答案 2 :(得分:0)

作为参考shape_predictor,输入应为图像和一个框。看来您投入的不只一个。

您可以尝试:

  1. 检查检测尺寸> 0 =>如果是,则转到步骤2,否则未检测到脸部。
  2. 尝试
  

形状=预测变量(图像,检测次数[0])

=>获取第一张脸的地标