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

馋奶兔 提交于 2019-12-18 05:26:09

问题


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.3\opencv\data\haarcascades\haarcascade_frontalface_alt_tree.xml" ; 
pCascade = (CvHaarClassifierCascade*) cvLoad(file , NULL, NULL, NULL);
    if (!pCascade)   { 
        exit(-1);    // unable to load xml 
    }

I believe that I'm expeiancing 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 "opencv2\opencv.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")

回答1:


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;



回答2:


It happens also to me but finally I think I found the problem.

OpenCV has two different libraries *d.lib and *.lib the d means debug.

The problem is you need to setup the proper libraries to your environment (vs in my case) in the proper mode.

d.lib when you are in debug and .lib when you are in release.

Also in my project I need to run it in Release mode to make it work :)

This setup in my vs2009 could be found under Properties, Linker, Input, Additional dependencies.

Best regards




回答3:


Check that the string with the "haarcascade_frontalface_alt.xml" file name is correct. I had this problem and the directory separater was not being recognised. I changed the '\' character to '/' and the tutorial worked. For your info, I was using MacOS 10.8.3 running Parallels with Windows 7, Visual Studio 2012 and opencv 2.44 - I was using the version 2 of the tutorial



来源:https://stackoverflow.com/questions/7158039/opencv-cvhaarclassifiercascade-cvload-doesnt-load-unable-to-load-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!