如何使用 Java 改进 OpenCV 中的人脸检测

Posted

技术标签:

【中文标题】如何使用 Java 改进 OpenCV 中的人脸检测【英文标题】:How to improve profile face detection in OpenCV using Java 【发布时间】:2020-10-16 10:22:11 【问题描述】:

我目前正在使用 OpenCV 来检测图像中的人脸。而且我也在尝试检测这张脸是否是侧面姿势的脸。基本上我只需要得到直脸图像。所以我使用haarcascade_profileface.xml 进行人脸检测。以下是我正在使用的代码,

try 
    faceDetector.load("haarcascade_profileface.xml");

    Mat image = Imgcodecs.imread(imageName);

    MatOfRect faceDetections = new MatOfRect();

    faceDetector.detectMultiScale(image, faceDetections);

    // Creating a rectangular box showing faces detected
    for (Rect rect: faceDetections.toArray()) 
        Imgproc.rectangle(image, new Point(rect.x, rect.y),
            new Point(rect.x + rect.width, rect.y + rect.height),
            new Scalar(0, 0, 255), 3);
    

    if (faceDetections.toArray().length == 0) 
        Core.flip(image, image, 1);

        faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image, faceDetections);

        // Creating a rectangular box showing faces detected
        for (Rect rect: faceDetections.toArray()) 
            Imgproc.rectangle(image, new Point(rect.x, rect.y),
                new Point(rect.x + rect.width, rect.y + rect.height),
                new Scalar(0, 0, 255), 3);
        

        return faceDetections.toArray().length == 0;
    
 catch (Exception ex) 
    ex.printStackTrace();

所以我通过翻转图像来检查图像中的脸是左侧还是右侧。我面临的问题是此轮廓人脸检测的准确性。因为我看到一些好的直脸图像也被检测为侧面。所以我错过了好的图像。如何提高此检测的准确性?

【问题讨论】:

【参考方案1】:

您可以检查脸部是否有 2 只眼睛。 如果它有一个它的配置文件。您只需要下载haarcascade_eye.xml。 接下来使用新的CascadeClassifier 对象作为眼睛,您可以找到它们。类似的东西:

CascadeClassifier eyeDetector = new CascadeClassifier(FaceDetector.class.getResource("haarcascade_eye.xml").getPath());
MatOfRect eyeDetections = new MatOfRect();
//Result list
eyeDetector.detectMultiScale(image, eyeDetections);
eyeDetections.toArray();
int noEyes = eyeDetections[0].length;
if (noEyes < 2)
    //profile


https://pythonprogramming.net/haar-cascade-face-eye-detection-python-opencv-tutorial/ 参考这个链接。

【讨论】:

这个答案可能比链接的内容更长。您能否编辑您对“为链接提供上下文”的答案(请参阅***.com/help/how-to-answer) 感谢您的提示,如您所见,我是新手。 感谢更新

以上是关于如何使用 Java 改进 OpenCV 中的人脸检测的主要内容,如果未能解决你的问题,请参考以下文章

人脸检測中几种框框大小的选择~

人脸检測流程及正负样本下载

从零学习openCVIOS7人脸识别实战

人脸检测实战进阶:使用 OpenCV 进行活体检测

尝试使用 OpenCV JAVA 检测图像中的人脸时出错

如何开发Java动态人脸识别