Ask Your Question
1

Unhandled exception after run in face detection

asked 2013-04-09 12:43:57 -0500

updated 2013-04-09 13:50:07 -0500

hi all,

i am using the following code for face detection:

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.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( "D:/Visual Studio 2010/Projects/newfaceDetection/haarcascade_frontalface_alt.xml" ) ){ printf("--(!)Error loading\n"); return -1; }
   if( !eyes_cascade.load( "D:/Visual Studio 2010/Projects/newfaceDetection/haarcascade_eye_tree_eyeglasses.xml" ) ){ printf("--(!)Error loading\n"); return -1; }

   frame=imread("d:/1.jpg");
   imshow( "test image", frame );
   if( !frame.empty() )
       { detectAndDisplay( frame ); }
   cvWaitKey(0);

   return 0;
 }

/** @function detectAndDisplay */
void detectAndDisplay( Mat frame )
{
  std::vector<Rect> faces;
    //Mat 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(40, 40) );

  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] );
    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 );
       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 );
 }

after I run the code i get the following error

(First-chance exception at 0x0fd761cf in newfaceDetection.exe: 0xC0000005: Access violation writing location 0x00000000. Unhandled exception at 0x0fd761cf in newfaceDetection.exe: 0xC0000005: Access violation writing location 0x00000000.)

if I comment out the following line

( `face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(40, 40) );`  )

the program will run without any errors but the face detection doesn't work. It will only show the image.

What do i need to do? I have studied many resources like previous forum questions but I don't reach any positive result yet. please help me , thanks in advance

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-04-09 13:54:34 -0500

Basically what you doing wrong is using the standard project path of visual studio. Windows allows folders to be named with spacings, but this screws up your code. So change the following lines, to contain simple locations:

if( !face_cascade.load( "D:/Visual Studio 2010/Projects/newfaceDetection/haarcascade_frontalface_alt.xml" ) ){ printf("--(!)Error loading\n"); return -1; }
if( !eyes_cascade.load( "D:/Visual Studio 2010/Projects/newfaceDetection/haarcascade_eye_tree_eyeglasses.xml" ) ){ printf("--(!)Error loading\n"); return -1; }

into for example

if( !face_cascade.load( "D:/models/haarcascade_frontalface_alt.xml" ) ){ printf("--(!)Error loading\n"); return -1; }
if( !eyes_cascade.load( "D:/models/haarcascade_eye_tree_eyeglasses.xml" ) ){ printf("--(!)Error loading\n"); return -1; }

Next try to start with the basic detector parametes and leave the more specialized ones for later. Change the following line

face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(40, 40) )

into

face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2)
edit flag offensive delete link more

Comments

hi, i do all things you suggest me. but unfortunately the problem is exist. First-chance exception at 0x559c61cf in newfaceDetection.exe: 0xC0000005: Access violation writing location 0x00000000. Unhandled exception at 0x559c61cf in newfaceDetection.exe: 0xC0000005: Access violation writing location 0x00000000.

kaveh_dad gravatar imagekaveh_dad ( 2013-04-09 23:54:58 -0500 )edit

Run your project in debug mode, go step by step and indicate please on which line this code is producing the error. I cannot read hexadecimal errors myself :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-10 01:20:07 -0500 )edit

Same error with you, I think the problem is at eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(40, 40)); But, why is works when I commented the code for eye detection, as it work fine for only face detection.

bruceyo gravatar imagebruceyo ( 2015-04-15 03:34:25 -0500 )edit

Did you check whether the eye model is loaded correctly. Which OpenCV version did you use?

StevenPuttemans gravatar imageStevenPuttemans ( 2015-04-15 06:59:44 -0500 )edit

I am using the Ceemple (OpenCV3.0). It loaded correctly. Only haarcascade_frontalface_alt.xml classifier works fine. All the others has the same problem. I am trying to test it on OpenCV2.4.10. Grateful you replying me. Thanks!

bruceyo gravatar imagebruceyo ( 2015-04-15 08:23:06 -0500 )edit

Hmm then my biggest guess is that Ceemple has not yet implemented the tons of fixes of last month to the models. Take a look at this bug issue :) --> http://code.opencv.org/issues/4218. Since Ceemple is not an official OpenCV distribution, you should contact them to get this fixed I guess.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-04-15 08:26:12 -0500 )edit

Is that mean the OpenCV3.0 has that bug? I have just tried the 2.4.10 version, works fine... So, I have to build the OpenCV from the GitHub by myself as there is only beta verison now. Do you know when will the real version released? And are another approach could detect the head more quickly? Many thanks for your efficient reply. Warm regards!

bruceyo gravatar imagebruceyo ( 2015-04-15 08:57:31 -0500 )edit
1

Here are some of the answers:

  1. Detecting faster heads with other method = NO
  2. OpenCV3.0 is under constant development, always best to use the latest branch instead of the stable releases for that one. Next release was scheduled near summer I think.
  3. So yes github version is the way to go to see if the error still occurs!
StevenPuttemans gravatar imageStevenPuttemans ( 2015-04-15 09:06:59 -0500 )edit
1

Very appreciate your reply. That helped me. You expert in OpenCV! May I email you sometime when faced with some tricky problems? Best regards!

bruceyo gravatar imagebruceyo ( 2015-04-15 09:10:13 -0500 )edit

I prefer you making a question on this forum, adding me to it by placing @StevenPuttemans in the text. This enables me to answer it as touroughly as possible, with the use of other experienced users, and to make sure that other users benefit the input also. Btw it is not because i am a moderator and active user of the forum, that i am an expert. Only used OpenCV for 3 years now!

StevenPuttemans gravatar imageStevenPuttemans ( 2015-04-15 09:15:48 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2013-04-09 12:43:57 -0500

Seen: 2,512 times

Last updated: Apr 09 '13