JavaCV中的人脸检测

时间:2013-10-28 10:21:35

标签: opencv javacv

我有以下代码来检测面部。

public class FaceDetection {


    public static final String XML_FILE = 
                    " C:/opencv/data/haarcascades/haarcascade_frontalface_default.xml";


            public static void main(String[] args){

                IplImage img = cvLoadImage("C:/Users/Ioanna/Desktop/lena.png");       
                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);

            }           

}

运行时返回以下错误

OpenCV错误:未知函数中的空指针(无效分类器级联),文件...... \ src \ opencv \ modules \ objdetect \ src \ haar.cpp,第1514行 线程“main”中的异常java.lang.RuntimeException:...... \ src \ opencv \ modules \ objdetect \ src \ haar.cpp:1514:错误:( - 27)无效的分类器级联

at com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects(Native Method)
at com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects(opencv_objdetect.java:238)
at FaceDetection.FaceDetection.detect(FaceDetection.java:32)
at FaceDetection.FaceDetection.main(FaceDetection.java:24)

有谁知道这是什么问题?

由于

1 个答案:

答案 0 :(得分:0)

复制链接C:/Users/Ioanna/Desktop/lena.png 并将其粘贴到类的文件中并复制链接C:/opencv/data/haarcascades/haarcascade_frontalface_default.xml 并将其粘贴到类

的文件中

enter image description here

public static final String XML_FILE = "haarcascade_frontalface_default.xml";
IplImage img = cvLoadImage("lena.jpg");