C++ OpenCV (CvHaarClassifierCascade*) cvLoad 不加载,无法加载 xml 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7158039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:32:44  来源:igfitidea点击:

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

c++opencvface-detection

提问by TripleS

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.

我正在尝试使用 OpenCv 2.3 进行人脸检测。我试图在我的项目中加载“haarcascade_frontalface_alt_tree.xml”,我总是无法加载 xml 文件。

    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.

我曾尝试在 cvLoad 命令之前加载图像,但没有帮助。

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

我正在使用 OpenCV 2.3 ,使我的配置就像在本教程中一样

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

我正在使用这些库(我认为我的配置是正确的,文件存在并且可以使用 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")

回答by SSteve

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

为了缩小问题的范围,在致电之前cvLoad您应该检查该文件是否存在。这是一种方法:

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>

你需要 #include <sys/stat.h>



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

在我的系统 (OS X 10.6.8/OpenCV 2.3) 上,当我尝试加载haarcascade_frontalface_alt_tree.xmlhaarcascade_frontalface_alt.xml出现异常时:

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 tutorialinstead. This code from that tutorial works for me:

我认为您使用的是过时的 OpenCV 1 教程,该教程不适用于当前版本的haarcascade_frontalface_alt_tree.xml. 试试这个 OpenCV 2 教程。该教程中的这段代码对我有用:

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

cout << "Loaded face_cascade" << endl;

回答by Guillermo Gutiérrez

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.

OpenCV 有两个不同的库 *d.lib 和 *.lib,d 表示调试。

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.

调试时为 d.lib,发布时为 .lib。

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.

我的 vs2009 中的这个设置可以在属性、链接器、输入、附加依赖项下找到。

Best regards

此致

回答by Brian Goulter

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

检查具有“haarcascade_frontalface_alt.xml”文件名的字符串是否正确。我遇到了这个问题,并且无法识别目录分隔符。我将 '\' 字符更改为 '/' 并且教程有效。为了您的信息,我使用的是运行 Parallels 的 MacOS 10.8.3 与 Windows 7、Visual Studio 2012 和 opencv 2.44 - 我使用的是教程的第 2 版