简体   繁体   English

Java OpenCV haarcascade_frontalface_default.xml

[英]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.

相关问题 haarcascade_frontalface_alt.xml无效 - haarcascade_frontalface_alt.xml not working 在定义“ \\\\ haarcascade_frontalface_alt.xml”的路径时,在人脸检测中获取空指针异常 - getting null pointer exception in face detection while defining the path of “\\haarcascade_frontalface_alt.xml” 如何使用OpenCV在同一个Java程序中运行许多haarcascade xml文件? - How to run many haarcascade xml files in the same Java program using OpenCV? Java和haarcascade面部和嘴部检测 - 嘴巴作为鼻子 - Java and haarcascade face and mouth detection - mouth as the nose 如何在XML中使用Spring Java属性默认值 - How to use Spring Java properties default value in XML 有没有一种简单的方法可以解析Java中的XML而无需添加默认编码? - Is there an easy way to parse XML in Java without adding default encoding? 如何从 Java 或 Xpath 中的 XML 中提取默认命名空间值? - How to extract default namespace value from XML in Java or Xpath? Android(Java)中的国际化,在布局(XML)中使用 xliff:g 的默认值 - Internationalization in Android (Java), use default values of xliff:g in layouts (XML) 如何在JAVA中编码默认名称空间和带有单个XML元素前缀的名称空间 - How to code a default namespace and a namespace with a prefix for single XML element in JAVA 在JAVA SAX中解析XML,而无需填充默认属性 - Parsing XML in JAVA SAX, without populating default attributes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM