【问题标题】:Opencv 3.0 Error loading face cascadeOpencv 3.0 加载人脸级联时出错
【发布时间】:2015-07-04 11:43:53
【问题描述】:

您好,我已经实现了 opencv 库。它适用于某些代码,例如视频捕获或从文件运行视频。但是当我实现面部检测或物体检测或运动检测程序的代码时。目前我已经实现了这个程序。

#include "opencv2/objdetect.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/opencv.hpp"

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

/** Function Headers */
void detectAndDisplay(Mat frame);

/** Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
String window_name = "Capture - Face detection";

/** @function main */
int main(void)
{
    VideoCapture capture;
    Mat frame;

    //-- 1. Load the cascades
    if (!face_cascade.load(face_cascade_name)){ printf("--(!)Error loading face cascade\n"); return -1; };
    if (!eyes_cascade.load(eyes_cascade_name)){ printf("--(!)Error loading eyes cascade\n"); return -1; };

    //-- 2. Read the video stream
    capture.open(-1);
    if (!capture.isOpened()) { printf("--(!)Error opening video capture\n"); return -1; }

    while (capture.read(frame))
    {
        if (frame.empty())
        {
            printf(" --(!) No captured frame -- Break!");
            break;
        }

        //-- 3. Apply the classifier to the frame
        detectAndDisplay(frame);

        int c = waitKey(10);
        if ((char)c == 27) { break; } // escape
    }
    return 0;
}

/** @function detectAndDisplay */
void detectAndDisplay(Mat frame)
{
    std::vector<Rect> faces;
    Mat frame_gray;

    cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
    equalizeHist(frame_gray, frame_gray);

    //-- Detect faces
    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));

    for (size_t i = 0; i < faces.size(); i++)
    {
        Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
        ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);

        Mat faceROI = frame_gray(faces[i]);
        std::vector<Rect> eyes;

        //-- In each face, detect eyes
        eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));

        for (size_t j = 0; j < eyes.size(); j++)
        {
            Point eye_center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2);
            int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
            circle(frame, eye_center, radius, Scalar(255, 0, 0), 4, 8, 0);
        }
    }
    //-- Show what you got
    imshow(window_name, frame);
}

当我尝试调试时它给了我错误程序'[7912] ConsoleApplication1.exe' has exited with code -1 (0xffffffff).

当我尝试在不调试的情况下启动时,它给我一个错误 Error loading face cascade。

我还发现了一件事,调试时的一条警告消息是 C:\Users\rushikesh\Documents\Visual Studio 2013\Projects\ConsoleApplication1\x64\Debug\opencv_world300d.dll'。无法找到或打开 PDB 文件。

但我检查了 world300d.dll。 opencv 3.0.0 的一些程序正在运行,所以我想我已经正确配置了它,但是很少有程序特别是跟踪对象或运动或检测到面部没有运行并给我同样的错误。

编辑 按照@srslynow 的建议尝试后,出现以下错误。

【问题讨论】:

    标签: c++ debugging opencv visual-c++ visual-studio-2013


    【解决方案1】:

    您的程序找不到 .xml 文件。请注意,从 Visual Studio IDE 运行程序时的默认工作目录不是 .exe 所在的位置。它是您项目的根目录。

    可能的解决方案:

    • 将 xml 文件移动到项目根目录
    • 将工作目录(在项目 > 属性 > 调试下)更改为 $(SolutionDir)$(Platform)\$(Configuration)\

    编辑:

    捕获默认视频设备是通过使用 capture.open(0);这可能是程序以 -1 状态退出的原因,我假设您的机器上确实有网络摄像头?

    【讨论】:

    • 您附加的图片没有提供任何信息。您可以通过在调试模式下启动程序来找到代码崩溃的行,在程序的早期设置断点,然后使用内置的“step over”和“step into”函数逐行遍历它视觉工作室。
    【解决方案2】:

    与 haar 级联有关的一个建议。我找到的最好的检测器文件是这个:

    haarcascade_frontalface_alt2.xml

    我做了数千次测试,这是最好的文件。

    【讨论】:

      【解决方案3】:

      如果您使用的是 Visual Studio,问题也可能是您的应用程序和 opencv 二进制文件之间的编译器版本。 例如:如果您使用的是与编译器“vc120”相对应的 VS 2013,但您将 opencv 二进制构建与 Visual Studio 2010(“vs100”)链接,则可能会出现该错误。
      在这种情况下,转到项目的属性:项目-> 属性-> 在“配置属性”下选择左侧的常规部分,在右侧将属性“平台工具集”更改为“Visual Studio 2010 (v100)” .
      这应该可行!

      【讨论】:

        【解决方案4】:

        也许你应该使用包含 XML 文件的绝对路径。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-10-26
          • 2013-10-23
          • 2015-01-08
          • 2014-09-09
          • 1970-01-01
          • 2017-11-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多