I am trying to write a simple face detection code using openCV. The program runs and the webcam lights up as though it is about to open but after about 5 seconds program closes before the frame window even opens.
At first I thought I wasn't loading the xml file properly but then I noticed that this only happens when I include the cascade classifier line:
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 3,0 | CASCADE_SCALE_IMAGE, Size(30, 30));
When I comment out this line the program seems to run fine in the sense that the webcam window opens. I believe that I have filled out the parameters correctly so I am not sure why I am having this issue.
Here is my code:
#include <iostream>
#include <string>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/objdetect/objdetect.hpp"
using namespace cv;
using namespace std;
//Global Variables
String face_cascade_name = "C:/Users/Administrator/Desktop/mirrorOS/External
Libraries/OpenCV/etc/haarcascades/haarcascade_frontalface_alt.xml";
CascadeClassifier face_cascade;
int main() {
face_cascade.load(face_cascade_name);
if (face_cascade.load(face_cascade_name)) {
cout << "Success" << endl;
}
Mat webcam; //create a mat object stores the current frame
Mat frame_gray; //gray frame
VideoCapture cap(0); //captures video from webcam
if (!cap.isOpened()) {
return -1;
}
while (cap.read(webcam)) {
cvtColor(webcam, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
vector<Rect> faces;
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 3,0 | CASCADE_SCALE_IMAGE, Size(30, 30));
for (size_t i = 0; i < faces.size(); i++) {
Rect face_i = faces[i];
rectangle(webcam, face_i, CV_RGB(0, 255, 0), 3);
}
imshow("Webcam", webcam);
waitKey(1);
destroyAllWindows();
}
return 0;
}
I've tried changing the parameter values for the cascadeClassifier but nothing seems to work so I am not quite sure what I'm doing wrong.
Looking through the debugger I have an Unhandled exception at this line specifically:
Unhandled exception at 0x00007FFE3488BBF2 (opencv_world343d.dll) in faceRecognition.exe: 0xC0000005: Access violation reading location 0x0000022B0608F000.