Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

c++ - OpenCV (CvHaarClassifierCascade*) cvLoad doesn't load, unable to load xml file

I'm trying face detection using OpenCv 2.3. My trying to load "haarcascade_frontalface_alt_tree.xml" on my project, I'm constantly unable to load the xml file.

    CvHaarClassifierCascade * pCascade = 0;  // the face detector   
const char* file ="C:OpenCV2.3opencvdatahaarcascadeshaarcascade_frontalface_alt_tree.xml" ; 
pCascade = (CvHaarClassifierCascade*) cvLoad(file , NULL, NULL, NULL);
    if (!pCascade)   { 
        exit(-1);    // unable to load xml 
    }

I believe that I'm experiencing the same problem as this problem.

I have tried to load an image before the cvLoad command, but it didn't help.

I'm using OpenCV 2.3, made my configuration just like in this tutorial.

I'm using those libraries (I presume my configurations are correct, the file exist and can be open using Notepad++).

    #include <stdio.h>
#include "opencv2opencv.hpp"
#include "cv.h"
#include "highgui.h"
//#include "cvaux.h"

using namespace cv;


#pragma comment(lib, "opencv_core230d.lib")
#pragma comment(lib, "opencv_highgui230d.lib")
//#pragma comment(lib, "opencv_contrib230d.lib")
//#pragma comment(lib, "opencv_calib3d230d.lib")
//#pragma comment(lib, "opencv_features2d230d.lib")
//#pragma comment(lib, "opencv_flann230d.lib")
//#pragma comment(lib, "opencv_gpu230d.lib")
#pragma comment(lib, "opencv_haartraining_engined.lib")
#pragma comment(lib, "opencv_imgproc230d.lib")
//#pragma comment(lib, "opencv_legacy230d.lib")
//#pragma comment(lib, "opencv_ml230d.lib")
//#pragma comment(lib, "opencv_objdetect230d.lib")
//#pragma comment(lib, "opencv_video230d.lib")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To narrow down the issue, before calling cvLoad you should check to see if the file exists. Here's one way:

struct stat buf;
int statResult = stat(file,&buf);
if (statResult || buf.st_ino < 0) {
    cout << "File not found: " << file << endl;
    exit(-2);
}

You'll need to #include <sys/stat.h>


On my system (OS X 10.6.8/OpenCV 2.3), when I attempt to load haarcascade_frontalface_alt_tree.xml or haarcascade_frontalface_alt.xml I get an exception:

OpenCV Error: Unspecified error (The node does not represent a user object (unknown type?)) in cvRead, file /Users/steve/Development/opencv2/opencv/modules/core/src/persistence.cpp, line 4857

I think you are using an outdated OpenCV 1 tutorial that doesn't work with the current version of haarcascade_frontalface_alt_tree.xml. Try this OpenCV 2 tutorial instead. This code from that tutorial works for me:

CascadeClassifier face_cascade;
if (!face_cascade.load( file) ) { 
    cout << "Couldn't load face_cascade" << endl;
    exit(-1); 
}

cout << "Loaded face_cascade" << endl;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...