繁体   English   中英

使用 OpenCV 的面部和眼睛检测无法正常工作

[英]Face and Eye Detection using OpenCV does not work correctly

我创建了一个基于 OpenCV 的小型库,但眼睛和面部检测已关闭。 我直接从 OpenCV 的 ZBF215181B5140522137B3D4F6B7354 temp.4AZ 下载了 haarcascade_eye.xml 和 haarcascade_profileface.xml,并将其复制到文件夹

可悲的是,结果令人作呕且不正确。 两年前,当我做类似的事情时,OpenCV 的结果要好得多。 我基本上使用基础图像并使用detectMultiScale应用两个分类器。 我不缩放图像,无论我使用灰度版本还是彩色版本都没有效果。

看看这个(和其他测试图像),我会说这是一个不及格的成绩。 所以最后我必须在这里错过一些东西,如果 OpenCV 失败了,今天的面部/眼睛/检测需求有哪些替代方案?

The OpenCV version is 4.5.1-2 (org.openpnp:opencv:4.5.1-2) but I manually loaded both xml classifier specs from github two days ago.

流行的 AI 生成的人脸图像,带有检测到的人脸(红色)和眼睛(绿色)

流行的 AI 生成的人脸图像,带有检测到的人脸(红色)和眼睛(绿色)

对于人脸检测,我鼓励您使用 OpenCV 的 DNN 人脸检测器。 使用 Viola-Jones 的误报更少,而且非常易于使用。 在下一个链接中找到一个很好的比较: https://learnopencv.com/face-detection-opencv-dlib-and-deep-learning-c-python/

在下面找到一个粗略的想法,如何在 python 中调用它。 但在 C++ 中非常相似:

初始化:

print("  >> Loading DNN_TF_OCV model for face detection...")
modelFile = "models/opencv_face_detector_uint8.pb"
configFile = "models/opencv_face_detector.pbtxt"
detector = cv2.dnn.readNetFromTensorflow(modelFile, configFile)

检测:

 def __detectFaceOpenCVDnnTF(self, frame):
        frameOpencv = frame.copy()
        frameHeight = frameOpencv.shape[0]
        frameWidth = frameOpencv.shape[1]
        blob = cv2.dnn.blobFromImage(frameOpencv, 1.0, (300, 300), [104, 117, 123], False, False)

        self.detector.setInput(blob)
        detections = self.detector.forward()
        bboxes = []
        for i in range(detections.shape[2]):
            confidence = detections[0, 0, i, 2]
            if confidence > self.conf_threshold:
                x1 = int(detections[0, 0, i, 3] * frameWidth)
                y1 = int(detections[0, 0, i, 4] * frameHeight)
                x2 = int(detections[0, 0, i, 5] * frameWidth)
                y2 = int(detections[0, 0, i, 6] * frameHeight)
                bboxes.append([x1, y1, x2 - x1, y2 - y1])

        return bboxes

对于眼睛检测,我建议@Quang Hoang 建议的相同想法。 使用面部标志检测。 OpenCV 的 LBF 实现非常快速且易于使用。 您还可以使用 Dlib 中的 ERT model。

干杯

暂无
暂无

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

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