Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV Error: Assertion failed while running FaceRecognition Program- Java

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)
click to hide/show revision 2
No.2 Revision

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

berak gravatar image

OpenCV Error: Assertion failed while running FaceRecognition FaceDetection Program- Java

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)