Search code examples
copencvcoordinateseye-tracking

Printing Eye Coordinates through OpenCV tracking


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;   

}


Solution

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