Program crashes giving unhelpful exit codes
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 ...
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 - 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.
ok, that's invalidating my first idea on it.
"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()
what can go wrong here ?
@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.
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 ?
I've checked again as you asked and I can confirm that the linker is configured correctly. link text
@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?
@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.
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.