简体   繁体   English

使用 opencv dnn 人脸检测器检测检测到的人脸图像中的人脸标志

[英]Detect facial landmarks inside a detected face image using opencv dnn face detector

I trying to detect the 68 facial landmarks of human face.我试图检测人脸的 68 个面部标志。 I detected the face using OpenCV dnn face detector as in https://www.pyimagesearch.com/2018/02/26/face-detection-with-opencv-and-deep-learning/ The face detection process done successfully, this is my code:我使用 OpenCV dnn 人脸检测器检测到人脸,如https://www.pyimagesearch.com/2018/02/26/face-detection-with-opencv-and-deep-learning/ 人脸检测过程成功完成,这是我的代码:

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

but when I trying to detect the facial landmarks inside the face, as the following:但是当我尝试检测面部内部的面部标志时,如下所示:

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)

I get the following error message我收到以下错误消息

shape = predictor(image, detections) TypeError: call (): incompatible function arguments. shape = predictor(image, detections) TypeError: call (): 不兼容的函数参数。 The following argument types are supported: 1. (self: dlib.shape_predictor, image: array, box: dlib.rectangle) -> dlib.full_object_detection支持以下参数类型: 1. (self: dlib.shape_predictor, image: array, box: dlib.rectangle) -> dlib.full_object_detection

Invoked with: , array([[[ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0], ...,调用: , array([[[ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0], ...,

The error message appeared when I'm using OpenCV dnn face detector, and MTCNN fro dlib face detector, but it cannot be appeared with Haar cascade face detector and the facial landmarks detected successfully.当我使用 OpenCV dnn 人脸检测器和来自 dlib 人脸检测器的 MTCNN 时出现错误消息,但它不能出现在 Haar 级联人脸检测器和成功检测到人脸地标的情况下。 I want to detect the facial landmarks within the OpenCV dnn face detector as the above code due to its accurate where the Haar cascade face detector don't benefit with my face image due to high occlusions consistence.我想检测 OpenCV dnn 人脸检测器中的人脸标志作为上面的代码,因为它是准确的,由于高遮挡一致性,Haar 级联人脸检测器不会从我的人脸图像中受益。 Can anyone please help me.谁能帮帮我吗。

这将解决问题:

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

If you want something like this: example You can use face_recognition.face_landmarks(image) function from face recognition library , is much easier to use than the dnn detector.如果你想要这样的东西:示例你可以使用人脸识别库中的face_recognition.face_landmarks(image) 函数,它比 dnn 检测器更容易使用。 I hope I've helped.我希望我有所帮助。

As the reference shape_predictor , the input should be image and a single box.作为参考shape_predictor ,输入应该是图像和单个框。 It seems you are putting more than one.看来你放了不止一个。

You can try:你可以试试:

  1. Check if size of detections > 0 => if true go to step 2, else there is no face detected.检查检测大小是否 > 0 => 如果为真,则转到步骤 2,否则没有检测到人脸。
  2. Try尝试

shape = predictor(image, detections[0])形状 = 预测器(图像,检测 [0])

=> get landmarks of the first face => 获取第一张脸的地标

First of all note that for 68 facial landmarks : 首先请注意,对于68个面部标志:

https://github.com/davisking/dlib-models https://github.com/davisking/dlib-models

Also note that this model file is designed for use with dlib's HOG face detector. 另请注意,此模型文件旨在与dlib的HOG人脸检测器配合使用。 That is, it expects the bounding boxes from the face detector to be aligned a certain way, the way dlib's HOG face detector does it. 也就是说,它期望面部检测器的边界框以某种方式对齐,这就是dlib的HOG面部检测器的方式。 It won't work as well when used with a face detector that produces differently aligned boxes, such as the CNN based mmod_human_face_detector.dat face detector 当与会产生不同对齐框的面部检测器(例如基于CNN的mmod_human_face_detector.dat面部检测器)一起使用时,它将无法正常工作

For your purpose you need openface/nn4.small2.v1.t7 model instead of shape_predictor_68_face_landmarks.dat 为了您的目的,您需要openface/nn4.small2.v1.t7模型,而不是shape_predictor_68_face_landmarks.dat

My suggestion : 我的建议 :

The drawback of OpenCV dnn face detector is that the input image must be resized to 300*300 so, for small faces the face landmark features diminish and it would be somehow useless to use face feature extractors for face recognition . OpenCV dnn人脸检测器的缺点是必须将输入图像的大小调整为300*300因此,对于小的人脸,人脸地标特征会减少,因此使用人脸特征提取器进行人脸识别将毫无用处。

For robust and powerful face recognition use dlib's Hog or CNN (if you are in GPU-CUDA business the first one goes fast and the latter needs a poweful Nvidia ) then resize your input video or image not less than 640*480 ( for real-time recognition , preferably not more than that since it will devour your CPU or GPU beyond recognition :) then you can use one of those face features extractors like dlib . 要获得强大而强大的face recognition使用dlib的HogCNN (如果您从事的是GPU-CUDA业务,那么前一个要快速进行,而后者需要强大的Nvidia ),然后将输入视频或图像的大小调整为不小于640*480 (对于实时间识别,最好不要超过此时间,因为它会吞噬您的CPU或GPU使其无法识别:)然后您可以使用诸如dlib之类的面部特征提取器dlib See also : 也可以看看 :

https://openface-api.readthedocs.io/en/latest/openface.html#openface-aligndlib-class https://openface-api.readthedocs.io/zh-CN/latest/openface.html#openface-aligndlib-class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM