Ask Your Question
1

Trying to Detect Face

asked 2014-11-06 17:14:21 -0600

Nimble_Ninja gravatar image

updated 2014-11-07 12:13:13 -0600

I am trying to detect faces but cascade file will not load: I have set path correctly and in addition added files to all folders related to C++ program I also tried adding the files in Visual Studio but that did not work either... I am running windows 8.1, VS2013, OpenCV249 and 64bit configuration. Code:(copied from http://docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html#cascade-classifier)

#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 );

 /** 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 );
 }

I have searched this issues for over a week and can not find anything that actually solves this problem....most mention to correctly reference the path and that does not work. Any help is appreciated

Screen capture of error image description zoomed in image description

and updated code

/** Global variables */
String face_cascade_name = "C:\\OpenCV\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "C:\\OpenCV\\sources\\data\\haarcascades\\haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2014-11-07 06:33:52 -0600

thdrksdfthmn gravatar image

updated 2014-11-07 06:38:28 -0600

Is there a "haarcascade_frontalface_alt.xml" or "haarcascade_eye_tree_eyeglasses.xml" where your .exe is, or in your project folder?

Just try to change the two lines with the right path:

String face_cascade_name = "/path/to/the/haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "/path/to/the/haarcascade_eye_tree_eyeglasses.xml";

or (if you are on Windows)

String face_cascade_name = "path\\to\\the\\haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "path\\to\\the\\haarcascade_eye_tree_eyeglasses.xml";

If you do not know what "/path/to/the/" means, it is something like "/home/ninja/opencv/data/haarcascades/", or "C:\\opencv\\data\\haarcascades\\" for Windows


Another thing what you can do is to post the error code you are getting, or the result of the comportment of the application. I supposed that you have not put the right path, because in your code is not posted.

edit flag offensive delete link more

Comments

Thanks for the response!

I tried the correct path and it did not work as well... below is the code I changed: String face_cascade_name = "C:\OpenCV\sources\data\haarcascades\haarcascade_frontalface_alt.xml"; String eyes_cascade_name = "C:\OpenCV\sources\data\haarcascades\haarcascade_eye_tree_eyeglasses.xml";

The error I get is in the command window and is the text from the code where it states "Error loading" for the face cascade

Nimble_Ninja gravatar imageNimble_Ninja ( 2014-11-07 11:57:37 -0600 )edit
1

try "C:\OpenCV\sources\data\haarcascades\haarcascade_frontalface_alt.xml" or put haarcascade_frontalface_alt.xml to folder with *.exe and use "haarcascade_frontalface_alt.xml". If this will not work, check that you use release lib with release in VS (or debug with debug), then reinstall OpenCV :)

fedor gravatar imagefedor ( 2014-11-07 12:16:35 -0600 )edit

"use release lib with release in VS (or debug with debug), then reinstall OpenCV"... not sure I understand what this means. I have run other applications using opencv files and they have worked, but never anything from the cascade files. BTW, I am a ChemE and not a programmer so please excuse me if I am stating something that makes no sense. I did try the path you mentioned and then pasted a copy of the two xml files in the folder containing the application file(the .exe file). But that did not work either.

Nimble_Ninja gravatar imageNimble_Ninja ( 2014-11-07 15:50:02 -0600 )edit
1

Ok...got it past load errors!!! Thank you fedor, it was the lib files... I added them to a global property sheet and not on an individual one for the debug. Code still not finding the face however...but I can figure this part out by reading some more... Thank you ALL for helping me get opencv running!!!

Nimble_Ninja gravatar imageNimble_Ninja ( 2014-11-08 10:21:29 -0600 )edit

Question Tools

Stats

Asked: 2014-11-06 17:14:21 -0600

Seen: 3,081 times

Last updated: Nov 07 '14