I am trying to run the program and is building well but not able to debug. Capturing first frame and then giving below error.
I tried to check in debug mode and could figure out that
imshow(window_name, frame);
Error !!!
Here is the working code that is copied from OpenCV blog.
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/video/video.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);
/** Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);
/** #function main */
int main(int argc, const char** argv)
{
CvCapture* capture;
Mat frame;
//-- 1. Load the cascades
if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading\n"); return -1; };
if (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Error loading\n"); return -1; };
//-- 2. Read the video stream
capture = cvCaptureFromCAM(CV_CAP_ANY);
if (capture)
{
while (true)
{
frame = cvQueryFrame(capture);
//-- 3. Apply the classifier to the frame
if (!frame.empty())
{
detectAndDisplay(frame);
}
else
{
printf(" --(!) No captured frame -- Break!");
break;
}
int c = waitKey(10);
if ((char)c == 'c') { break; }
}
}
return 0;
}
/** #function detectAndDisplay */
void detectAndDisplay(Mat frame)
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor(frame, frame_gray, CV_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
//-- Detect faces
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (size_t i = 0; i < faces.size(); i++)
{
Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
ellipse(frame, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
Mat faceROI = frame_gray(faces[i]);
std::vector<Rect> eyes;
//-- In each face, detect eyes
eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (size_t j = 0; j < eyes.size(); j++)
{
Point center(faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5);
int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
circle(frame, center, radius, Scalar(255, 0, 0), 4, 8, 0);
}
}
//-- Show what you got
imshow(window_name, frame);
}
Error when value chosen in
capture = cvCaptureFromCAM(-1)
is -1.
I am getting GRey window with no Error.
imshow with Grey Window, No Error
I changed the Platform Toolset from Visual Studio 2015(v140) to Visual Studio 2013(v120) as in the figure attached. I do not know why but it worked.
Visual Studio 2015(v140)
Visual Studio 2013(v120)
It may happen, if your face ROI, out of frame bounds.
Because you trying to copy this region you getting memory error.
if(faces[i].x>=0 && faces[i].y >= 0 && faces[i].x+faces[i].width<frame_gray.cols && faces[i].y+faces[i].height < frame_gray.rows)
{
faceROI=frame_gray(faces[i]);
}
Related
I found this code from opencv website and tried it out. I got this problem where detectAndDisplay shows an error saying incomplete type is not allowed, CascadeClassifier is not a type name, CvCapture is undefined and so on... How do I solve these errors
#include "C:\opencv\build\include\opencv\objdetect.hpp"
#include "C:\opencv\build\include\opencv2\highgui.hpp"
#include "C:\opencv\build\include\opencv2\imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
/** Function Headers */
void detectAndDisplay ("Mat frame");
/** Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);
/** #function main */
int main( int argc, const char** argv )
{
CvCapture* capture;
Mat frame;
//-- 1. Load the cascades
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
//-- 2. Read the video stream
capture = cvCaptureFromCAM( -1 );
if( capture )
{
while( true )
{
frame = cvQueryFrame( capture );
//-- 3. Apply the classifier to the frame
if( !frame.empty() )
{ detectAndDisplay( frame ); }
else
{ printf(" --(!) No captured frame -- Break!"); break; }
int c = waitKey(10);
if( (char)c == 'c' ) { break; }
}
}
return 0;
}
/** #function detectAndDisplay */
void detectAndDisplay( Mat frame )
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
//-- Detect faces
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( size_t i = 0; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes;
//-- In each face, detect eyes
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( size_t j = 0; j < eyes.size(); j++ )
{
Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
}
}
//-- Show what you got
imshow( window_name, frame );
}
Are you using Windows?
If you are the first line should be #include "C:\opencv\build\include\opencv2//objdetect.hpp" I think (with double slashes before objdetect.hpp). This should be able to solve the problem with CascadeClassifier.
As for the CvCapture problem, which version of OpenCV are you using? I'm pretty sure it's no longer supported if you are using the latest versions (3.0 or above) and I'm not sure if it's still available in the older versions. See this What is the difference between the CvCapture structure and the VideoCapture structure?.
I'd suggest using VideoCapture instead http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture (looks like OpenCV 2.4 is not supporting CvCapture as well). They're pretty much the same thing and I'm using VideoCapture right now so it should work well!
I was trying to run the facial detection with OpenCV3.0 sample c++ code under macOS Sierria version 10.12.5. However, I failed the load the ".xml" file. I tried using relative path as well as absolute path to "haarcascade_frontalface_alt.xml" and "haarcascade_eye_tree_eyeglasses.xml" run it, but it did not solve the problem for me. I compiled the code successfully but got error message:
--(!)Error loading face cascade
I think there's some problem to do with the load method of the CascadeClassifier. I read about some threads. But none gives specific instructions on solving this matter. I beg for specific instructions on solving this. Thank you all so much!
Here is the sample code:
#include "opencv2/objdetect.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
/** Function Headers */
void detectAndDisplay( Mat frame );
/** Global variables */
String face_cascade_name = "/my/path/to/haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "/my/path/to/haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
String window_name = "Capture - Face detection";
/** #function main */
int main( int argc, const char** argv )
{
// CommandLineParser parser(argc, argv,
// "{help h||}"
// "{face_cascade|../../data/haarcascades/haarcascade_frontalface_alt.xml|}"
// "{eyes_cascade|../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml|}");
// cout << "\nThis program demonstrates using the cv::CascadeClassifier class to detect objects (Face + eyes) in a video stream.\n"
// "You can use Haar or LBP features.\n\n";
// parser.printMessage();
// face_cascade_name = parser.get<string>("face_cascade");
// eyes_cascade_name = parser.get<string>("eyes_cascade");
VideoCapture capture;
Mat frame;
//-- 1. Load the cascades
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade\n"); return -1; };
if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading eyes cascade\n"); return -1; };
//-- 2. Read the video stream
capture.open( 0 );
if ( ! capture.isOpened() ) { printf("--(!)Error opening video capture\n"); return -1; }
while ( capture.read(frame) )
{
if( frame.empty() )
{
printf(" --(!) No captured frame -- Break!");
break;
}
//-- 3. Apply the classifier to the frame
detectAndDisplay( frame );
char c = (char)waitKey(10);
if( c == 27 ) { break; } // escape
}
return 0;
}
/** #function detectAndDisplay */
void detectAndDisplay( Mat frame )
{
std::vector<Rect> faces;
Mat frame_gray;
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(30, 30) );
for ( size_t i = 0; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 );
ellipse( frame, center, Size( faces[i].width/2, faces[i].height/2 ), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes;
//-- In each face, detect eyes
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CASCADE_SCALE_IMAGE, Size(30, 30) );
for ( size_t j = 0; j < eyes.size(); j++ )
{
Point eye_center( faces[i].x + eyes[j].x + eyes[j].width/2, faces[i].y + eyes[j].y + eyes[j].height/2 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, eye_center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
}
}
//-- Show what you got
imshow( window_name, frame );
}
In the following code I'm trying to detect face and eye. from a video.
My problem is that I'm trying to set ROI to detect eyes . but I think there an error in cvSetImageROI funcition .
this error is displayed
error C2664: 'cvSetImageROI' : cannot convert parameter 1 from 'cv::Mat' to 'IplImage *'
Thanks for helping me
#include<stdio.h>
#include<math.h>
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<opencv2\objdetect\objdetect.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<vector>
using namespace cv;
using namespace std;
int main()
{
CascadeClassifier face_cascade, eye_cascade;
if(!face_cascade.load("haarcascade_frontalface_alt2.xml")) {
printf("Error loading cascade file for face");
return 1;
}
if(!eye_cascade.load("haarcascade_eye.xml")) {
printf("Error loading cascade file for eye");
return 1;
}
VideoCapture capture("w.mp4"); //-1, 0, 1 device id
if(!capture.isOpened())
{
printf("error to initialize camera");
return 1;
}
Mat cap_img,gray_img;
vector<Rect> faces, eyes;
while(1)
{
capture >> cap_img;
waitKey(10);
cvtColor(cap_img, gray_img, CV_BGR2GRAY);
cv::equalizeHist(gray_img,gray_img);
face_cascade.detectMultiScale(gray_img, faces, 1.1, 10, CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING, cvSize(0,0), cvSize(300,300));
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);
Mat faceROI = gray_img(faces[i]);
cvSetImageROI(faceROI, cvRect(faces->x,faces->y + (faces->height)/5,faces->width, (faces->height)/3 );
eye_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30,30));
for(size_t j=0; j< eyes.size(); j++)
{
//Point center(faces[i].x+eyes[j].x+eyes[j].width*0.5, faces[i].y+eyes[j].y+eyes[j].height*0.5);
Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
int radius = cvRound((eyes[j].width+eyes[j].height)*0.25);
circle(cap_img, center, radius, Scalar(255,0,0), 2, 8, 0);
}
rectangle(cap_img, pt1, pt2, cvScalar(0,255,0), 2, 8, 0);
}
imshow("Result", cap_img);
waitKey(3);
char c = waitKey(3);
if(c == 27)
break;
}
return 0;
}
The problem is that you are trying to use old openCV method. Your image is in the Mat format and cvSetImageROI cannot take Mat image as an argument.
Suggestion:
Rect region_of_interest = Rect(x, y, w, h);
Mat image_roi = image(region_of_interest);
I try to create a program on opencv for eyes tracking. However, the program often crashes for no apparent reason, especially when I cover my right eye with my hand.
I use code blocks and opencv 2.4.5
using namespace std;
using namespace cv;
int lar,hau;
Rect rect;
Mat copie_frame;
Mat carre;
Mat YCR;
Mat eye(lar,hau,CV_8UC1);
/** Function Headers */
void detectAndDisplay( Mat frame );
void eyestreatment (Mat carre);
/** Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);
/** #function main */
int main( int argc, const char** argv )
{
CvCapture* capture;
Mat frame;
//-- 1. Load the cascades
if( !face_cascade.load( face_cascade_name ) ){
printf("--(!)Error loading\n");
return -1;
}
if( !eyes_cascade.load( eyes_cascade_name ) ){
printf("--(!)Error loading\n");
return -1;
}
//-- 2. Read the video stream
capture = cvCaptureFromCAM( -1 );
if( capture )
{
while( true )
{
frame = cvQueryFrame( capture );
//-- 3. Apply the classifier to the frame
if( !frame.empty() ){
detectAndDisplay( frame );
}
else {
printf(" --(!) No captured frame -- Break!");
break;
}
int c = waitKey(10);
if( (char)c == 'c' ) { break; }
}
}
return 0;
}
/** #function detectAndDisplay */
void detectAndDisplay( Mat frame )
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
//-- Detect faces
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( int i = 0; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
Point haut((center.x+faces[i].width*0.5),(center.y+faces[i].height*0.5));
Point bas((center.x-faces[i].width*0.5),(center.y-faces[i].height*0.5));
Point pt=bas;
int X=pt.x;
int Y=pt.y;
int L=haut.x-X;
int H=haut.y-Y;
rectangle(frame,haut,bas,Scalar( 255, 0, 0 ),1,8,0);
Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes;
//-- In each face, detect eyes
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( int j = 0; j < eyes.size(); j++ )
{
Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
Point haut((center.x+eyes[i].width*0.5),(center.y+eyes[i].height*0.5));
Point bas((center.x-eyes[i].width*0.5),(center.y-eyes[i].height*0.5));
pt=bas;
int X=pt.x;
int Y=pt.y;
int L=haut.x-X;
int H=haut.y-Y;
rectangle(frame,haut,bas,Scalar( 0, 255, 0 ),1,8,0);
rect = Rect (bas.x,bas.y,L,H);
carre= frame(rect);
lar=carre.rows;
hau=carre.cols;
eyestreatment(carre);
}
}
imshow( window_name, frame );
}
/** #function eyestreatment */
void eyestreatment( Mat carre )
{
cvtColor(carre,YCR,CV_BGR2YCrCb);
inRange ( YCR, Scalar (20, 130, 70) , Scalar (255, 170, 130) , eye );
imshow("carre",carre);
imshow("YCR",eye);
eye.release();
}
You have a mistake in your nested loop in function detectAndDisplay. You have variable i looping over faces, and variable j looping over eyes. However, you have the lines:
Point haut((center.x+eyes[i].width*0.5),(center.y+eyes[i].height*0.5));
Point bas((center.x-eyes[i].width*0.5),(center.y-eyes[i].height*0.5));
Thus, if you ever have less eyes than you have faces, it'll crash on your access to eyes[i], as you are indexing into your eyes with your face counter. Of course, even when it's not crashing the code is incorrect as a consequence of this.
I'm using OpenCV on a 64-bit Win 7, with VS2010 (C++).
I've tried so many source codes for detecting faces, they compile and run, but no detection takes place.
I'll give two examples:
1) In this first example, I'm using source code from:
http://www.bsd-noobz.com/opencv-guide/60-3-face-detection
I get the picture, but not the squares.
2) In this second example, I'm using code that I had downloaded some time ago (can't remember where from). This one shows the stream from the webcam, and again no detection is happening.
#include "stdafx.h"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
/** Function Headers */
void detectAndDisplay2( Mat frame );
/** Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
//RNG rng(12345);
/** #function main */
int main()
{
CvCapture* capture;
Mat frame;
//-- 1. Load the cascades
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade\n"); return -1; };
if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading eye cascade\n"); return -1; };
//-- 2. Read the video stream
capture = cvCaptureFromCAM( 1 );
if( capture )
{
while( true )
{
frame = cvQueryFrame( capture );
//-- 3. Apply the classifier to the frame
if( !frame.empty() )
{ detectAndDisplay2( frame ); }
else
{ printf(" --(!) No captured frame -- Break!"); break; }
int c = waitKey(10);
if( (char)c == 'c' ) { break; }
}
}
cvReleaseCapture(&capture);
cvDestroyWindow("Capture - Face detection");
return 0;
}
/** #function detectAndDisplay */
void detectAndDisplay2( Mat frame )
{
vector<Rect> faces;
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
//-- Detect faces
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( int i = 0; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
Mat faceROI = frame_gray( faces[i] );
vector<Rect> eyes;
//-- In each face, detect eyes
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( int j = 0; j < eyes.size(); j++ )
{
Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
}
}
//-- Show what you got
imshow( "Processed", frame_gray );
imshow( "Capture - Face detection", frame );
}
I'd be really grateful if someone can help.
This is a opencv project in github. This error stems from two issues that I can see.
1) Make sure you’ve installed the correct OpenCV version. I suggest this install guide… It worked well for me.
2) Then, make sure you have added haarcascade_frontalface_alt.xml to inside of the executable file – which should be in a folder like ../build/bin/haarcascade_frontalface_alt.xml when you built the project).