#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;
int main(int argc, const char** argv)
{
//create the cascade classifier object used for the face detection
CascadeClassifier face_cascade;
CascadeClassifier face_cascade2;
CascadeClassifier face_cascade3;
//use the xml files for features detection
face_cascade.load("C:\\opencv247\\sources\\data\\haarcascades\\eyes.xml"); //haarcascade_eye_tree_eyeglasses.xml Eyes only capture straight foroward eyes
face_cascade2.load("C:\\opencv247\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml");
face_cascade3.load("C:\\opencv247\\sources\\data\\haarcascades\\newnose.xml");
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
//setup image files used in the capture process
Mat gray_image;
//create a window to present the results
namedWindow("outputCapture", 1);
//create a loop to capture and find faces
while(1)
{
//Capture
Mat image;
waitKey(40);
cap >> image; // get a new frame from camera
//Convert image to grayscale
cvtColor(image, gray_image, CV_BGR2GRAY);
//create a vector array to store the features found
std::vector<Rect> eyes;
std::vector<Rect> faces;
std::vector<Rect> nose;
//find features and store them in the vector array
face_cascade.detectMultiScale(gray_image, eyes, 1.2, 3, CV_HAAR_FIND_BIGGEST_OBJECT|CV_HAAR_SCALE_IMAGE, Size(30,30));
face_cascade2.detectMultiScale(gray_image, faces, 1.2, 3, CV_HAAR_FIND_BIGGEST_OBJECT|CV_HAAR_SCALE_IMAGE, Size(30,30));
face_cascade3.detectMultiScale(gray_image, nose, 1.2, 6, CV_HAAR_FIND_BIGGEST_OBJECT|CV_HAAR_SCALE_IMAGE, Size(30,30));
//draw a rectangle for all found the Eyes in the vector array on the original image
//Faces
for(int i = 0; i < faces.size(); i++)
{
Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
Point pt2(faces[i].x, faces[i].y);
rectangle(image, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0);
//eyes
for(int j = 0; j < eyes.size(); j++)
{
Point pt1(eyes[i].x + eyes[j].width, eyes[i].y + eyes[j].height);
Point pt2(eyes[i].x, eyes[i].y);
rectangle(image, pt1, pt2, cvScalar(0, 100, 0, 0), 1, 8, 0);
}
//Nose
for(int z = 0; z < nose.size(); z++)
{
Point pt1(nose[i].x + nose[z].width, nose[i].y + nose[z].height);
Point pt2(nose[i].x, nose[i].y);
rectangle(image, pt1, pt2, cvScalar(100, 200, 0, 0), 1, 8, 0);
}
}
//print the output
imshow("outputCapture", image);
//pause for 33ms
waitKey(33);
}
if(cap.isOpened())
{
image.release();
gray_image.release();
cap.release();
}
return 0;
}
The problem is now It sometimes run but sometime it gives me that memory exception error , in my humbled programing opinion the pointers used or something must be destroyed or the image is getting declared more than one time , I am just stuck and its infuriating because it sometimes ACTUALLY work , but then it crashes... I do understand the code is a bit untidy and i apologize for that.
EDIT: Now it works only when I remove the camera and plug it back in the USB , but on the second run it gives the same error.
EDIT2: After walking behind some debugs, I discovered that when it crashes its right after the line when I call the cvtcolor function , so the problem might be with the allocations of the images not being deconstructed properly.
EDIT3 : Added the part at the end of the code where I try to release the cap and the images but the problem is that I realized it never reaches that line of code