OpenCV Error: Assertion failed while running FaceDetection Program- Java

asked 2015-05-12 05:30:55 -0600

Vishnu gravatar image

updated 2015-05-12 08:27:12 -0600

berak gravatar image

I am very much new to OpenCV. From this link I got JAVA sample program for face detection . I encountered some problems as I was installed opencv 3.0 version. Anyway errors where solved as I changed my code as

 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        System.out.println("\nRunning FaceDetector");


        CascadeClassifier faceDetector = new CascadeClassifier(FaceDetection.class.getResource("/resources/xmls/haarcascade_frontalface_alt.xml").getPath());
        Mat image = Imgcodecs.imread(FaceDetection.class.getResource("/resources/testimages/FaceTest.jpg").getPath());

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

        System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

        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, 255, 0));


        }

        String filename = "/resources/testimages/FaceRecognised.jpg";
        System.out.println(String.format("Writing %s", filename));
        Imgcodecs.imwrite(filename, image);

But unfortunately there occuring exception while running. Stacktrace is as below:

Running FaceDetector
OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\objdetect\src\cascadedetect.cpp, line 1595
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\objdetect\src\cascadedetect.cpp:1595: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale
]
    at org.opencv.objdetect.CascadeClassifier.detectMultiScale_1(Native Method)
    at org.opencv.objdetect.CascadeClassifier.detectMultiScale(CascadeClassifier.java:103)
    at com.bq.opencv.FaceDetection.main(FaceDetection.java:31)
edit retag flag offensive close merge delete

Comments

1

well, it could not load your xml-cascade. are your 'resorces' in a jar file ? (opencv can't read from there)

try an absolute path to it, instead of the FaceDetection.class.getResource(...)

berak gravatar imageberak ( 2015-05-12 05:35:02 -0600 )edit

No xml files are not inside jar file. I got null pointer exception while giving a absolute path. That was healed as I went so as in this link.

Vishnu gravatar imageVishnu ( 2015-05-12 05:53:56 -0600 )edit

@berak I was mistaken. That error was solved as I tried absolute path. Really thanks for that. But unfortunately required new image file was not generated, even if no exceptions were found.

Vishnu gravatar imageVishnu ( 2015-05-12 06:02:13 -0600 )edit