Program crashes giving unhelpful exit codes

asked 2016-08-20 08:39:14 -0600

Sachin Bhatia gravatar image

updated 2016-08-24 14:54:07 -0600

I'm building a face and eye detecting program using OpenCV 3.1 using Visual Studio 2015. I've changed the sample code provided to suit my purposes. The function definition is given below.

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

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

      using namespace std;
      using namespace cv;

      // Function Headers
      void detectAndDisplay(Mat frame);
      void CallBackFunc(int event, int x, int y, int flags, void* userdata);

      // Global variable
      string face_cascade_name = "haarcascade_frontalface_alt.xml";
      CascadeClassifier face_cascade;
      string eye_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
      CascadeClassifier eye_cascade;

      string window_name = "Capture - Face detection";

  // Function main
      int main(void)
            {
                VideoCapture capture(0);

                if (!capture.isOpened())  // check if we succeeded
                    return -1;

                // Load the cascades
                if (!face_cascade.load(face_cascade_name)){ printf("--(!)Error loading\n"); return (-1); }
                if (!eye_cascade.load(eye_cascade_name)){ printf("--(!)Error loading\n"); return (-1); }

                // Read the video stream
                Mat frame;

                for (;;)
                {
                    capture >> frame;

                    if (!frame.empty())
                        detectAndDisplay(frame);

                    else
                    {
                        printf(" --(!) No captured frame -- Break!");
                        break;
                    }

                    int c = waitKey(10);

                    if (27 == char(c))
                        break;
                }

                return 0;
            }

void detectAndDisplay(Mat frame)
{
   std::vector<Rect> faces, eyes;

   Mat frame_gray, crop, gray;
   string text; 
   stringstream sstm;          

   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(40, 40));

    Point cen_win(frame.size().width / 2, frame.size().height / 2);
    circle(frame, cen_win, 1, Scalar(0, 0, 255), CV_FILLED);

    size_t ic = 0; // ic is index of current element
    unsigned long ac = 0; // ac is area of current element

    size_t ib = 0; // ib is index of biggest element
    unsigned long ab = 0; // ab is area of biggest element

     for (ic = 0; ic < faces.size(); ic++) // Iterate through all detected faces
           {
                ac = faces[ic].area();
                ab = faces[ib].area();

                if (ac > ab)
                    ib = ic; 
           }

      if(faces.size())
          {
               Point pt1(faces[ib].x, faces[ib].y);
               Point pt2((faces[ib].x + faces[ib].width), (faces[ib].y + faces[ib].height));
               rectangle(frame, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0); 

               crop = frame(faces[ib]);
               cvtColor(crop, gray, COLOR_BGR2GRAY);
               equalizeHist(gray, gray);
               eye_cascade.detectMultiScale(gray, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(10, 10));


               if(eyes.size())  
                      {
                           Point Ept1, Ept2;        

                           for (int i = 0; i < eyes.size(); ++i)
                             {
                              Ept1.x = eyes[i].x; 
                              Ept1.y = eyes[i].y;

                                  Ept2.x = eyes[i].x + eyes[i].width; 
                              Ept2.y = eyes[i].y + eyes[i].height;

                              rectangle(frame, Ept1, Ept2, Scalar(0, 255, 0), 2, 8, 0);
                                }
                       } 

                 Point cen_rec(faces[ib].x + faces[ib].width / 2, faces[ib].y + faces[ib].height / 2);
                 circle(frame, cen_rec, 1, Scalar(0, 0, 255), CV_FILLED); //Marks the center of detected face.                                          

                // Output text
                sstm << "Crop area size: " << faces[ib].width << "x" << faces[ib].height << " Size of window: " << frame.size().width << '*' << frame.size().height;
                text = sstm.str();
                putText(frame, text, cvPoint(30, 30), FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(0, 0, 255), 1, CV_AA);
                sstm.clear();
                sstm.str(string());

                double percentage;
                percentage = ((double)ab) / (frame.size().width * frame.size().height) * 100;
                sstm << "Percentage: " << percentage << " Tilt: " << (cen_rec.y - cen_win.y)/10 << " Pan: " << (cen_rec.x - cen_win ...
(more)
edit retag flag offensive close merge delete

Comments

1

if it does not find any faces, then you have a problem here: crop = frame(faces[ib]);

please try to build & run the DEBUG version or your program, there should be some more helpful error messages there.

berak gravatar imageberak ( 2016-08-20 08:47:26 -0600 )edit
1

@berak - I made some edits to the code as I was posting the question. Left out some parts I considered irrelevant. Sorry for making you comment look out of context.

Sachin Bhatia gravatar imageSachin Bhatia ( 2016-08-20 09:42:25 -0600 )edit

ok, that's invalidating my first idea on it.

berak gravatar imageberak ( 2016-08-20 09:54:50 -0600 )edit

"No features have been detected by the program at this point. "

then please, check, if your cascades loaded at all, e.g. check: face_cascades.empty()

berak gravatar imageberak ( 2016-08-20 09:57:08 -0600 )edit

what can go wrong here ?

  • image invalid
  • Classifier not loaded (check path again)
  • no face or eyes found
berak gravatar imageberak ( 2016-08-20 09:58:54 -0600 )edit

@berak - I have worked through these possibilities. As I mentioned before the program worked fine before I added the eye detection feature. If you can not detect anything amiss from the function definition, I'll add the code I left out.

Sachin Bhatia gravatar imageSachin Bhatia ( 2016-08-23 01:28:14 -0600 )edit

hmm, let's get paranoid ;)

maybe it's not the code, but your project. can you go, and check the linker settings, and make sure, that you're using opencv DEBUG libs in DEBUG mode, and RELEASE libs in RELEASE mode strictly ?

berak gravatar imageberak ( 2016-08-23 01:31:53 -0600 )edit
1

I've checked again as you asked and I can confirm that the linker is configured correctly. link text

Sachin Bhatia gravatar imageSachin Bhatia ( 2016-08-23 03:39:33 -0600 )edit

@berak - the sample objectdetection2.cpp program doesn't work as well. All I get is a single frame after which the program crashes. However I had the task manager window open and under disk, percentage used was 100%. 60% of memory is used when the program is supposedly running. Are these observations of any utility?

Sachin Bhatia gravatar imageSachin Bhatia ( 2016-08-23 06:56:45 -0600 )edit

@berak - I set a breakpoint just before the eye detection function is called and stepped into the function call given below, executing a statement at a time.

eye_cascade.detectMultiScale(gray, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(10, 10));

The function call ends with with the control shifting to the console window and control does not return to code regardless of anything I press. What do you suggest I do to uncover information that would help identify what the problem is?

I've had someone review my code and they tried catching for exceptions but did not find any.

Sachin Bhatia gravatar imageSachin Bhatia ( 2016-08-25 05:09:05 -0600 )edit