Java -OpenCV Error: Assertion failed while running FaceRecognition Program
Java -OpenCV Error: Assertion failed while running FaceRecognition Program
我对 OpenCV 非常陌生。从这个 link 我得到了 JAVA 人脸检测示例程序。我在安装 opencv 3.0 版本时遇到了一些问题,该代码适用于 2.4.6 版本。无论如何,当我将代码更改为
时,错误已解决
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);
但不幸的是 运行 时出现异常。堆栈跟踪如下:
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)
请帮忙。
您好,这已更正,因为尝试提供绝对路径并从类路径中删除 xml 文件和图像。
CascadeClassifier faceDetector = new CascadeClassifier("C:/OpenCV/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml");
而不是
CascadeClassifier faceDetector = new CascadeClassifier(FaceDetection.class.getResource("/resources/xmls/haarcascade_frontalface_alt.xml").getPath());
还有:
垫子图像 = Imgcodecs.imread("C:/XYZ/PQR/FaceTest.jpg");
我对 OpenCV 非常陌生。从这个 link 我得到了 JAVA 人脸检测示例程序。我在安装 opencv 3.0 版本时遇到了一些问题,该代码适用于 2.4.6 版本。无论如何,当我将代码更改为
时,错误已解决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);
但不幸的是 运行 时出现异常。堆栈跟踪如下:
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)
请帮忙。
您好,这已更正,因为尝试提供绝对路径并从类路径中删除 xml 文件和图像。
CascadeClassifier faceDetector = new CascadeClassifier("C:/OpenCV/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml");
而不是
CascadeClassifier faceDetector = new CascadeClassifier(FaceDetection.class.getResource("/resources/xmls/haarcascade_frontalface_alt.xml").getPath());
还有: 垫子图像 = Imgcodecs.imread("C:/XYZ/PQR/FaceTest.jpg");