0

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.

Command Line Picture

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.

markalex
  • 8,623
  • 2
  • 7
  • 32
Greg
  • 3
  • 2
  • Step through it in the debugger so you see what's going on. | (Oh, and [do yourself a favour and stop using `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)) – Dan Mašek Nov 05 '18 at 22:16
  • Oh, and `destroyAllWindows();` on every iteration might be a bit counter-productive, especially with such a short wait. As long as the window name used in `imshow` stays the same, it should reuse the window (just update the image it shows) without needing to close and open a new one for every frame. – Dan Mašek Nov 05 '18 at 22:25
  • @DanMašek Thanks for the response. 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. – Greg Nov 06 '18 at 17:14
  • @DanMašek I understand that it means I am trying to access a memory location with no value in it but I'm not quite sure as to why this is happening. – Greg Nov 06 '18 at 17:18
  • Which version of MSVS? I presume this is happening in a Debug build? – Dan Mašek Nov 06 '18 at 17:21
  • 1
    @DanMašek Community 2017 Version 15.8.9. When I compile and build, the program runs but them closes abruptly without opening a window or producing an error. In the Debug build it points to the "detectMultiScale" line as the issue. – Greg Nov 06 '18 at 17:45
  • OK. Unfortunately that's not one of the versions of MSVS I've got installed here. However, I cannot reproduce that with the standard OpenCV 3.4.3 binaries + MSVS 2015. I don't really see a reason why you should be getting such error, but I've seen similar issues arise when there are incompatibilities between the C++ runtime used to build OpenCV and one used to build the application. Even though this generally happens between major releases of MSVS, the version in question is 2 months younger than OpenCV 3.4.3... so there is some possibility. I'd suggest filing bug report on OpenCV github page. – Dan Mašek Nov 06 '18 at 21:23
  • @DanMašek Thank you for the help and the suggestion. It definitely seems to be an issue with the build. I transferred over to my mac and it worked flawlessly. – Greg Nov 07 '18 at 15:57

0 Answers0