OpenCV Error: Assertion failed while running 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)
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(...)
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.
@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.