简体   繁体   中英

opencv face detect crashes computer

Hello everyone I have been having this issue with my opencv 3.1 program that I am programming in visual studio 2015 C++. It can use the camera just fine and display images just fine, but I try out a face detect example from the documentation my computer freezes up. Here is the code:

Here are the Two Functions where it gets hung up on: but mainly stops around the faceDetect function, I will do a break point to know specifically where.

void CoreVision::init()
{
    face_cascade_name = "C:\\PAD\\opencv\\haarcascades\\haarcascade_frontalface_alt.xml";
    eyes_cascade_name = "C:\\PAD\\opencv\\haarcascades\\haarcascade_eye_tree_eyeglasses.xml";

    if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading face cascade\n"); };
    if (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Error loading eyes cascade\n"); };

    std::cout << "face_dir: " << face_cascade_name << std::endl;
    std::cout << "eye_dir: " << eyes_cascade_name << std::endl;
}

cv::Mat CoreVision::detectFace(cv::Mat main)
{
    cv::Mat nuller;
    try
    {
        faces.clear();
        cv::Mat frame_gray;

        cv::cvtColor(main, frame_gray, cv::COLOR_BGR2GRAY);
    cv::equalizeHist(frame_gray, frame_gray);

    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30));
    for (size_t i = 0; i < faces.size(); i++)
    {
        cv::Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
        cv::ellipse(main, center, cv::Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, cv::Scalar(255, 0, 255), 4, 8, 0);

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

        eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30));

        for (size_t j = 0; j < eyes.size(); j++)
        {
            cv::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);
            cv::circle(main, eye_center, radius, cv::Scalar(255, 0, 0), 4, 8, 0);
        }
    }

    return main;
}
catch (...)
{
    std::cout << "Exception: have issue with processing face detection.";
    return nuller;
}

Here is the main function:

int main()
{
    CoreVision _core;
    _core.init();
    while (true)
    {
        cv::Mat img = _core.captureImage(-1);
        img = _core.detectFace(img);
        _core.displayImage(img, "parker");

    }

}

And finally the .h file for the two functions:

#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <opencv2\opencv.hpp>
#include <opencv2\world.hpp>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
class CoreVision
{
public:
    CoreVision();
    ~CoreVision();
    cv::Mat loadImage(const char* image_name, int type);
    void saveImage(cv::Mat img, const char* filename);
    cv::Mat captureImage(int cam);
    cv::Mat detectFace(cv::Mat main);
    int convertImage(cv::Mat img, int type);
    void displayImage(cv::Mat, const char* window_name);
    void init(void);

    char cCurrentPath[FILENAME_MAX];
    cv::String face_cascade_name;
    cv::String eyes_cascade_name;
    cv::CascadeClassifier face_cascade;
    cv::CascadeClassifier eyes_cascade;
    std::vector<cv::Rect> faces;
};

Thank you in advance for your help, I am not for sure if it is a driver issues, but I have tried it on multiple computers and the same freezing happens.

you don't seem to be storing the faces vector. You have it in public std::vector faces; but where are you using it? eyes and faces are 2 different sets of data. Your also mixing two styles of code c style and c++ style opencv. You should stick with c++ since your using the latest opencv version.

   faces.clear(); // under here  
   std::vector<Rect>faces; // see if this helps  

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    void saveImage(cv::Mat img, const char* filename); //should be 
    void imwrite(img, const char* filename);// etc.  

    cv::Mat img = _core.captureImage(-1);
    img = _core.detectFace(img);
    _core.displayImage(img, "parker"); // should be 
    _core.displayImage(Mat frame); 

this proably isn't going to work asuming core.captureImage(-1); is your camera. also what does int convertImage(cv::Mat img, int type); do? int convertImage(cv::Mat img, int type); do? are you sure it takes an int value?

c++ convention is

Videocapture cap(0);
Mat frame: 
cap >> frame; 
namedWindow("video");
imshow(video, frame); //etc   

I had exactly the same issue. I figured out that the line containing eyes_cascade.detectMultiScale was the one causing my driver to crash. I have the Intel HD graphics 4400. The code started working perfectly after I completely uninstalled the Intel HD graphics drivers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM