[英]Java OpenCV haarcascade_frontalface_default.xml
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_objdetect.*;
public class FaceDetection{
public static final String XML_FILE =
"resources/haarcascade_frontalface_default.xml";
public static void main(String[] args){
IplImage img = cvLoadImage("resources/lena.jpg");
detect(img);
}
public static void detect(IplImage src){
CvHaarClassifierCascade cascade = new
CvHaarClassifierCascade(cvLoad(XML_FILE));
CvMemStorage storage = CvMemStorage.create();
CvSeq sign = cvHaarDetectObjects(
src,
cascade,
storage,
1.5,
3,
CV_HAAR_DO_CANNY_PRUNING);
cvClearMemStorage(storage);
int total_Faces = sign.total();
for(int i = 0; i < total_Faces; i++){
CvRect r = new CvRect(cvGetSeqElem(sign, i));
cvRectangle (
src,
cvPoint(r.x(), r.y()),
cvPoint(r.width() + r.x(), r.height() + r.y()),
CvScalar.RED,
2,
CV_AA,
0);
}
cvShowImage("Result", src);
cvWaitKey(0);
}
Exception 例外
OpenCV Error: Unspecified error (The node does not represent a user
object (unknown type?)) in cvRead, file src\persistence.cpp, line 4976
Exception in thread "main" java.lang.RuntimeException:
src\persistence.cpp:4976: error: (-2) The node does not represent a
user object (unknown type?) in function cvRead
Anyone know how to fix this? 有人知道怎么修这个东西吗?
As far as I understood, you are trying to pass OpenCV haarcascades XML resource file into cvLoad function, which is not designed to work with Java resource files, since it is a C++ function, which has no idea about this notion. 据我所知,您正在尝试将OpenCV haarcascades XML资源文件传递给cvLoad函数,该函数不适用于Java资源文件,因为它是一个C ++函数,它不知道这个概念。
I had the same problem and the only workaround I found was copying this xml file from Java resource into temporary directory, feed it to cvLoad function and then delete it. 我遇到了同样的问题,我发现的唯一解决方法是将这个xml文件从Java资源复制到临时目录中,将其提供给cvLoad函数然后将其删除。 It worked. 有效。 And moreover there is a special OpenCV function for it, which does exactly all that. 此外,还有一个特殊的OpenCV功能,它可以完成所有这些功能。
I used an example from https://github.com/bytedeco/javacv/blob/master/samples/FaceApplet.java 我使用了https://github.com/bytedeco/javacv/blob/master/samples/FaceApplet.java中的示例
String classiferName = "haarcascade_frontalface_alt.xml";
// copying xml file into temp directory
File classifierFile = Loader.extractResource(classiferName, null, "classifier", ".xml");
if (classifierFile == null || classifierFile.length() <= 0) {
throw new IOException("Could not extract \"" + classiferName + "\" from Java resources.");
}
// Preload the opencv_objdetect module to work around a known bug.
Loader.load(opencv_objdetect.class);
classifier = new CvHaarClassifierCascade(cvLoad(classifierFile.getAbsolutePath()));
// deleting temp file
classifierFile.delete();
if (classifier.isNull()) {
throw new IOException("Could not load the classifier file.");
}
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.