Question

I have used the code from this tutorial: 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). Through this I can confirm that the image is being loaded correctly.

The error occurs later at line 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!

Was it helpful?

Solution

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.

OTHER TIPS

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. This will help you understand better how cascade classifier works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top