【发布时间】:2016-08-02 02:59:20
【问题描述】:
我正在尝试在 OpenCV 中测试面部识别 API。我已经导入了提供的.jar,它正确加载了 DLL。 imageLibIit() 函数将加载 DLL。我还在一个目录中提供了 XML 文件:
src\main\resources\opencv
public boolean faceDetect(String inputFilename, String outputFilename){
if(!loaded){
if(!imageLibInit()){ // initializes and loads the dynamic libraries
return false;
}
}
//TODO @Austin fix image resource issues
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String xmlResource = loader.getResource("opencv/haarcascade_frontalface_alt.xml").getPath();
CascadeClassifier faceDetector = new CascadeClassifier(xmlResource);
Mat inputImage = Imgcodecs.imread(inputFilename);
ErrorUtils.println(xmlResource);
if(inputImage.empty()){
ErrorUtils.println("image is empty");
return false;
}
MatOfRect facesDetected = new MatOfRect();
faceDetector.detectMultiScale(inputImage, facesDetected);
Rect[] detections = facesDetected.toArray();
ErrorUtils.println("Faces detected in '" + inputFilename + "': " + detections.length);
for(Rect detection : detections){
Imgproc.rectangle(
inputImage,
new Point(detection.x, detection.y),
new Point(detection.x + detection.width, detection.y + detection.height),
new Scalar(0, 0, 255)
);
}
Imgcodecs.imwrite(outputFilename, inputImage);
return true;
}
我仍然收到错误:
OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\objdetect\src\cascadedetect.cpp, line 1639
我研究过这个错误,每次解决方案似乎都与资源有关。这很可能是一个非常简单的问题,但我现在被困住了。
【问题讨论】:
-
看起来你的分类器永远不会被初始化。
xmlResource变量在加载资源之后和构建级联分类器之前的内容是什么? -
@DanMašek ErrorUtils 类是一个简单的与调试相关的类,尽管您可能已经推断出这一点。无论如何,它会打印出 XML 文件的路径:
"/D:/Programming/workspaces/github/project1/HomeServer/target/classes/opencv/haarcascade_frontalface_alt.xml" -
啊哈。我相信问题是路径开头的第一个正斜杠。 OpenCV 内部使用
FileStorage类来加载数据。此类使用fopen打开文件。我只是在 Visual C++ 中做了一个小测试,它无法打开具有这样路径的文件。删除前导斜杠应该可以解决问题。 -
查看answer,了解如何以独立于平台的方式解决此问题。
-
工作就像一个魅力,我的朋友。除非您愿意,否则我会发布答案。