1

I have managed to perform some simple face and eye detection/tracking. It's not too accurate but it works. I was wondering if there was some way in the OpenCV library where I could extract the coordinates of the eyes and face as it moves and print it out in the console in real time. Or maybe even save these coordinates in an output file.

UPDATE (Code for face and eye detection):

int detect( IplImage* img, const char* cascade_name ) {

static CvMemStorage* storage = 0;

static CvHaarClassifierCascade* cascade = 0;

int scale = 1;
int i;
IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );

//Load Cascade

cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );

if( !cascade )
{
    fprintf( stderr, "ERROR: Could not load classifier cascade!\n" );
    return 0;
}


storage = cvCreateMemStorage(0);
cvClearMemStorage( storage );
int faceDetected = 0;
if( cascade )
{
    //In case there is more than one face
    CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,
                                       1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
                                       cvSize(40, 40) );

    faceDetected = (faces ? faces->total : 0);

    for( i = 0; i < (faces ? faces->total : 0); i++ )
    {

        CvRect* r = (CvRect*)cvGetSeqElem( faces, i );

//Maybe this is where I get the coordinates?

        pt1.x = r->x*scale;
        pt2.x = (r->x+r->width)*scale;
        pt1.y = r->y*scale;
        pt2.y = (r->y+r->height)*scale;

//Draw rectangle over face

        cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );

    }
}
cvReleaseImage( &temp );   
return faceDetected;   

}

Barshan Das
  • 3,677
  • 4
  • 32
  • 46
Alex Burgos
  • 179
  • 4
  • 15

2 Answers2

3

Possibly this is what you want :

for( i=0 ; i< faces->total; i++ )
{
 CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
 printf("( %d %d ) , ( %d %d) ", r->x, r->y, r->x + r->width, r->y + r->height ); 
}
Barshan Das
  • 3,677
  • 4
  • 32
  • 46
  • Awesome. This will surely help. I have another question regarding OpenCV. I've been able to do everything real time. But sometimes it gets really slow, my computer is not that powerful when it comes to handling high resolution video displayed on screen real time. Is there a way I can perform the tracking on an input video, can someone guide me through the documentation on how to do it? Is it as efficient? Thanks! – Alex Burgos Apr 04 '13 at 04:59
0

This is a simple face detection built in opencv. They detect eyes within the faces with another CascadeClassifier. You can download list of existing CascadeClassifiers from opencv repository and use them for your needs. If neither of them satisfy your requirements, you can always train your own classifier, follow this tutorial, for example.

Safir
  • 902
  • 7
  • 9