2

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!

rockinfresh
  • 2,068
  • 4
  • 28
  • 46
dahui
  • 2,128
  • 2
  • 21
  • 40
  • 1
    have you checked if you imported your haar-classifier in yet? Sounds like a path error. – rockinfresh Feb 10 '14 at 15:12
  • No, how do I achieve this? I've got the openCV library included into the project, will I need to import parts in seperately? – dahui Feb 10 '14 at 17:31
  • Do I simply need to check for the presence of a file, include something separately into the project, or is it a forgotten declaration? – dahui Feb 10 '14 at 18:18
  • Sorry for late reply. Please view my answer for more details. – rockinfresh Feb 11 '14 at 04:15

2 Answers2

2

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.

dahui
  • 2,128
  • 2
  • 21
  • 40
  • 2
    +1 - Since your problem solved, you should accept one of the answers. It helps to mark this session 'solved' which is really helpful to others. – Abid Rahman K May 20 '14 at 19:03
  • 1
    Ah yeah my bad. I remember I went to set it as solved on the very day but it said I had to wait 24 hours or something, I must have forgotten the next day! – dahui May 21 '14 at 19:56
2

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.

rockinfresh
  • 2,068
  • 4
  • 28
  • 46
  • Thanks for the further tips. I am only using this as a side feature of a larger, unrelated software project, so it is unlikely I will be looking into custom cascade classifiers for now! – dahui Feb 11 '14 at 06:32
  • I do actually have another mini question though if you have time. The face detection is actually quite awful with my webcam so I was looking for ways to boost the performance, do you think increasing the brightness/contrast of the image could help it find a face pattern? – dahui Feb 11 '14 at 06:42
  • It might help a little. You can read this article for more info. They did a comparison with darker and and brighter images: http://www.gts.tsc.uvigo.es/AREA/papers/ICPR_2010a.pdf – rockinfresh Feb 11 '14 at 06:51
  • face detection quite awful is not unexpected, as I stated, the face classifier is likely to be a sample. Maybe you can search online for more of such or better face classifier. I myself built my own face classifier. – rockinfresh Feb 11 '14 at 06:53
  • Other factor would be the camera resolution too. What's the current resolution you using? – rockinfresh Feb 11 '14 at 06:54
  • I'm not sure, standard in built for a toshiba satellite l775, not particularly great but not terrible, I will have a look for other classifiers and give them a test, thanks – dahui Feb 11 '14 at 07:31