简体   繁体   English

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

[英]Error trying to detect faces in an image with OpenCV JAVA

I have used the code from this tutorial: http://opencvlover.blogspot.co.uk/2012/11/face-detection-in-javacv-using-haar.html 我使用了本教程中的代码: http : //opencvlover.blogspot.co.uk/2012/11/face-detection-in-javacv-using-haar.html

It has been slightly modified to read a different image, and display this image before attempting face detection (line 14 ). 对其进行了稍微修改,以读取其他图像,并在尝试面部检测之前显示此图像(第14行)。 Through this I can confirm that the image is being loaded correctly. 通过此操作,我可以确认图像已正确加载。

The error occurs later at line 23 . 该错误稍后在第23行发生。 Here is the complete error code: 这是完整的错误代码:

OpenCV Error: Null pointer (Invalid classifier cascade) in cvHaarDetectObjectsForROC,file ..\..\..\..\opencv\modules\objdetect\src\haar.cpp, line 1514 
Exception in thread "main" java.lang.RuntimeException: ..\..\..\..\opencv\modules\objdetect\src\haar.cpp:1514: error: (-27) Invalid classifier cascade in function cvHaarDetectObjectsForROC

at com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects(Native Method)
at com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects(opencv_objdetect.java:238)
at FaceDetection.detect(FaceDetection.java:23)
at FaceDetection.main(FaceDetection.java:15)

Here is my complete program: 这是我完整的程序:

import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_objdetect.*;

public class FaceDetection{

public static final String XML_FILE = 
        "resources/haarcascade_frontalface_default.xml";

public static void main(String[] args){

    IplImage img = cvLoadImage("pic.jpg");      
    cvShowImage("",img);cvWaitKey(0);
    detect(img);        
}   

public static void detect(IplImage src){

    CvHaarClassifierCascade cascade = new 
            CvHaarClassifierCascade(cvLoad(XML_FILE));
    CvMemStorage storage = CvMemStorage.create();
    CvSeq sign = cvHaarDetectObjects(
            src,
            cascade,
            storage,
            1.5,
            3,
            CV_HAAR_DO_CANNY_PRUNING);

    cvClearMemStorage(storage);

    int total_Faces = sign.total();     

    for(int i = 0; i < total_Faces; i++){
        CvRect r = new CvRect(cvGetSeqElem(sign, i));
        cvRectangle (
                src,
                cvPoint(r.x(), r.y()),
                cvPoint(r.width() + r.x(), r.height() + r.y()),
                CvScalar.RED,
                2,
                CV_AA,
                0);

    }

    cvShowImage("Result", src);
    cvWaitKey(0);

    }           
}

Anybody know what is causing this error, or how it can be fixed? 有人知道导致此错误的原因是什么,或者如何解决该错误? Thanks! 谢谢!

Solved! 解决了!

I googled the "haarcascade_frontalface_default.xml", downloaded it and stuck it in my folder in the workspace, took /resources/ off of the filename in the XML string and it works. 我用Google搜索“ haarcascade_frontalface_default.xml”,将其下载并粘贴到工作区的文件夹中,从XML字符串中的文件名中删除/ resources /,并且可以正常工作。

Congrats on solving it. 恭喜解决。 However to progress and learn, you must understand what went wrong. 但是,要进步和学习,您必须了解出了什么问题。

The error occurred because the program cannot find the cascade classifier. 发生错误是因为程序找不到级联分类器。 I thought you declared the location of the classifier wrongly, but turns out you didn't have the classifier in the first place. 我以为您错误地声明了分类器的位置,但事实证明您根本没有分类器。 You solved that by downloading a sample classifier and using it. 您可以通过下载示例分类器并使用它来解决此问题。

You do not necessarily have to put the classifier in the folder containing the program. 您不必将分类器放在包含程序的文件夹中。 You can also put it somewhere else and state the path of where the classifier is located. 您也可以将其放在其他位置,并说明分类器所在的路径。

I would also recommend you to train your own haar-classifier if you are really into object detection. 如果您真的很喜欢物体检测,我还建议您训练自己的haar分类器。 This will help you understand better how cascade classifier works. 这将帮助您更好地了解级联分类器的工作方式。

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

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