简体   繁体   中英

Face detection using opencv giving an exception

I was trying for face detection using opencv in java .My code to do it is :

            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);
            }

Where i kept the xml and the origImg on their specified locations.But it gives an exception :

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

How to get over it as i cant find any solution to do so.Please help

Try this code

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);
}

I also had the same problem when I try to make an application to detect faces. I used bytedeco/javacv . The problem occurred when parsing the XML model. I used this haarcascade_frontalface_alt.xml model. Then I worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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