Pregunta

I've written a code for face and eye detection that worked fine previously but now due to unknwon reasons, the program is having problems and a message is displayed that Your Project has stopped working... Please have a look at the following code and the commented line number has the problem and the program stops there

#include <opencv2/core/core.hpp>
#include "opencv2/objdetect/objdetect.hpp"
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;
int main()
{

    /*Mat image=imread("im.jpg");
    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.
    //imwrite("im2.jpg",image);// write the image stored in object image as im2.jpg
    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;*/

    /*VideoCapture capture;  
    Mat frame;
    capture.open(0);
    if(capture.isOpened())
    {
        cout<<"success"<<endl;
        while(1)
        {
            bool flag=capture.read(frame); 
            namedWindow( "Display window", WINDOW_AUTOSIZE );
            if(flag)
                imshow( "Display window", frame );
            else
                cout<<"failed";
            waitKey(300);
        }

    }*/
    //face detection


    cout<<"i'm here";
    CascadeClassifier face_cascade("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
    CascadeClassifier eye_cascade("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_eye.xml");
    Mat image=imread("subject3.jpg");
    cout<<"i'm here";
    Mat res;
    resize(image, res, Size(800, 600), 0, 0, INTER_LINEAR);
    Mat gray;
    cvtColor(res,gray,COLOR_BGR2GRAY);
    equalizeHist(gray, gray);
    std::vector<Rect> faces;
    std::vector<Rect> eyes;
    face_cascade.detectMultiScale(gray,faces, 1.1, 2,0 | CASCADE_SCALE_IMAGE, Size(30, 30));//problem
    Rect roi;
    cout<<faces.size();
    Mat crop;
    Mat grayEye;
    for(int i=0;i<faces.size();i++)
    {
        cout<<"flag2";
        roi.x=faces[i].x;
        roi.y=faces[i].y;
        roi.width=faces[i].width;
        roi.height=faces[i].height;
        Point pt1(faces[i].x, faces[i].y); 
        Point pt2((faces[i].x + faces[i].height), (faces[i].y + faces[i].width));
        crop=res(roi);
        resize(crop, crop, Size(300, 300), 0, 0, INTER_LINEAR);
        cvtColor(crop, grayEye, CV_BGR2GRAY);
        equalizeHist(grayEye, grayEye);
        eye_cascade.detectMultiScale(grayEye,eyes, 1.1, 2,0 | CASCADE_SCALE_IMAGE, Size(30, 30));
        cout<<endl<<eyes.size();
        for(int j=0;j<eyes.size();j++)
        {
            Point pnt1(eyes[j].x, eyes[j].y); 
            Point pnt2((eyes[j].x + eyes[j].height), (eyes[j].y + eyes[j].width));
            rectangle(crop, pnt1, pnt2, Scalar(0, 255, 0), 2);
        }
        rectangle(res, pt1, pt2, Scalar(0, 255, 0), 2);
    }
    namedWindow( "detected", WINDOW_AUTOSIZE );
    imshow("detected",res);
    waitKey(300);
    imshow("detected",crop);
    waitKey(0);


}
¿Fue útil?

Solución

The following modifications made the program to work correctly (strange) Previous Code :

CascadeClassifier face_cascade("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");


Replaced with :

CascadeClassifier face_cascade;
face_cascade.load("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");

I was loading the xml file in constructor previously (still don't know why it happened) but it is working now.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top