繁体   English   中英

使用opencv进行人脸检测给出异常

[英]Face detection using opencv giving an exception

我试图在 java 中使用 opencv 进行人脸检测。我的代码是:

            String CASCADE_FILE ="C:/Users/admin/Desktop/javacv/src/javacv/lbpcascade_frontalface.xml";
            String OUT_FILE = "markedFaces.jpg";

            IplImage origImg = cvLoadImage("C:/Users/admin/Desktop/javacv/src/javacv/lena.png", 1);
            //IplImage origImg = cvLoadImage(args[0]);

            // convert to grayscale
            IplImage grayImg = IplImage.create(origImg.width(),origImg.height(), IPL_DEPTH_8U, 1);
            cvCvtColor(origImg, grayImg, CV_BGR2GRAY);

            // scale the grayscale (to speed up face detection)
            IplImage smallImg = IplImage.create(grayImg.width()/SCALE,grayImg.height()/SCALE, IPL_DEPTH_8U, 1);
            cvResize(grayImg, smallImg, CV_INTER_LINEAR);

            // equalize the small grayscale
            IplImage equImg = IplImage.create(smallImg.width(),smallImg.height(), IPL_DEPTH_8U, 1);
            cvEqualizeHist(smallImg, equImg);

            // create temp storage, used during object detection
            CvMemStorage storage = CvMemStorage.create();

            // instantiate a classifier cascade for face detection

            CvHaarClassifierCascade cascade =new CvHaarClassifierCascade(cvLoad(CASCADE_FILE));
            System.out.println("Detecting faces...");

            CvSeq faces = cvHaarDetectObjects(equImg, cascade, storage,1.1, 3, CV_HAAR_DO_CANNY_PRUNING);

            cvClearMemStorage(storage);

            // draw thick yellow rectangles around all the faces
            int total = faces.total();
            System.out.println("Found " + total + " face(s)");

            for (int i = 0; i < total; i++) {

                    CvRect r = new CvRect(cvGetSeqElem(faces, i));
                    cvRectangle(origImg, cvPoint( r.x()*SCALE, r.y()*SCALE ),cvPoint( (r.x() + r.width())*SCALE,(r.y() + r.height())*SCALE ),CvScalar.RED, 6, CV_AA, 0);

                    String strRect = String.format("CvRect(%d,%d,%d,%d)", r.x(), r.y(), r.width(), r.height());

                    System.out.println(strRect);
                    //undo image scaling when calculating rect coordinates
            }

            if (total > 0) {
                    System.out.println("Saving marked-faces version of " + " in " + OUT_FILE);

                    cvSaveImage(OUT_FILE, origImg);
            }

我将 xml 和 origImg 保存在指定位置的地方。但它给出了一个例外:

OpenCV Error: Unspecified error (The node does not represent a user object (unknown type?)) in cvRead, file ..\..\..\..\opencv\modules\core\src\persistence.cpp, line 4991
Exception in thread "main" java.lang.RuntimeException: ..\..\..\..\opencv\modules\core\src\persistence.cpp:4991: error: (-2) The node does not represent a user object (unknown type?) in function cvRead

如何克服它,因为我找不到任何解决方案。请帮忙

试试这个代码

private static final String CASCADE_FILE = "./others/haarcascade_frontalface_alt.xml";

public FaceDetection(String inputFile, String outputFile) throws Exception {


    // Load the original image.
    IplImage originalImage = cvLoadImage(inputFile, 1);
    // We need a grayscale image in order to do the recognition, so we create a new    image of the same size as the original one.
    IplImage grayImage = IplImage.create(originalImage.width(),originalImage.height(), IPL_DEPTH_8U, 1);
    // We convert the original image to grayscale.
    cvCvtColor(originalImage, grayImage, CV_BGR2GRAY);
    CvMemStorage storage = CvMemStorage.create();
    // We instantiate a classifier cascade to be used for detection, using the cascade  definition
    CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(cvLoad(CASCADE_FILE));
    // We detect the faces.
    CvSeq faces = cvHaarDetectObjects(grayImage, cascade, storage, 1.1, 1, 0);
    // We iterate over the discovered faces and draw yellow rectangles around them.
    for (int i = 0; i < faces.total(); i++) {
        CvRect r = new CvRect(cvGetSeqElem(faces, i));
        cvRectangle(originalImage, cvPoint(r.x(), r.y()), cvPoint(r.x() + r.width(),   r.y() + r.height()), CvScalar.RED, 1, CV_AA, 0);
    }

    // Save the image to a new file.
    cvSaveImage(outputFile, originalImage);
}

当我尝试制作一个检测人脸的应用程序时,我也遇到了同样的问题。 我使用了bytedeco/javacv 解析 XML 模型时出现问题。 我使用了这个haarcascade_frontalface_alt.xml模型。 然后我工作得很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM