// TrackFaces.c - by Robin Hewitt, 2007 // http://www.cognotics.com/opencv/downloads/camshift_wrapper // This is free software. See License.txt, in the download // package, for details. // /////////////////////////////////////////////////////////////////////////////// // An example program that It detects a face in live video, and automatically // begins tracking it, using the Simple Camshift Wrapper. #include "cv.h" #include "highgui.h" #include #include "capture.h" #include "facedet.h" #include "pca.h" //// Constants const char * DISPLAY_WINDOW = "DisplayWindow"; const char * DISPLAY_WINDOW2 = "FaceWindow"; #define OPENCV_ROOT "/home/marius/OpenCV-2.0.0" //// Global variables IplImage * pVideoFrameCopy = 0; //// Function definitions int initAll(); void exitProgram(int code); void captureVideoFrame(); IplImage* convert_grayscale(IplImage *img); ////////////////////////////////// // main() // void main( int argc, char** argv ) { CvRect * pFaceRect = 0; double start=0, end=0, t1=0; // validate that an input was specified if( argc != 2 ) { printUsage(); return; } printf("%d", cvGetNumThreads()); if( !strcmp(argv[1], "train") ) learn("train.txt"); else if( !strcmp(argv[1], "test") ) { if( !initAll() ) exitProgram(-1); // Capture and display video frames until a face // is detected while( 1 ) { // Look for a face in the next video frame captureVideoFrame(); pFaceRect = detectFace(pVideoFrameCopy); // Show the display image cvShowImage( DISPLAY_WINDOW, pVideoFrameCopy ); if( (char)27==cvWaitKey(20) ) exitProgram(0); // exit loop when a face is detected if(pFaceRect) break; } while( 1 ) { CvBox2D faceBox; CvRect r; // get the next video frame //captureVideoFrame(); // track the face in the new video frame r = *pFaceRect; cvSetImageROI(pVideoFrameCopy, r); IplImage * pFaceImg = cvCreateImage( cvSize(92,112), pVideoFrameCopy->depth, pVideoFrameCopy->nChannels ); cvResize(pVideoFrameCopy, pFaceImg, CV_INTER_AREA ); cvResetImageROI(pVideoFrameCopy); pFaceImg = convert_grayscale(pFaceImg); CvPoint pt1 = { r.x, r.y }; CvPoint pt2 = { r.x + r.width, r.y + r.height }; cvRectangle(pVideoFrameCopy, pt1, pt2, CV_RGB(0,255,0), 3, 4, 0); start= (double)cvGetTickCount(); recognize(pFaceImg); end= (double)cvGetTickCount(); t1= (end-start)/((double)cvGetTickFrequency()*1000.); printf( "Recognition time = %g ms\n", t1 ); // faceBox = track(pVideoFrameCopy); // // // outline face ellipse // cvEllipseBox(pVideoFrameCopy, faceBox, // CV_RGB(255,0,0), 3, CV_AA, 0 ); cvShowImage( DISPLAY_WINDOW, pVideoFrameCopy ); cvShowImage( DISPLAY_WINDOW2, pFaceImg ); if( (char)27==cvWaitKey(20) ) break; } } else { printf("Unknown command: %s\n", argv[1]); printUsage(); } exitProgram(0); } IplImage* convert_grayscale(IplImage *img) { IplImage *gray_image,*gray_img0; gray_image=cvCloneImage(img); //check image is it gray or not if nor convert it to the gray if(img->nChannels!=1){ //convert original image to gray_scale image gray_img0 = cvCreateImage(cvSize( gray_image->width, gray_image->height), 8, 1 ); cvCvtColor(gray_image, gray_img0, CV_RGB2GRAY ); gray_image = cvCloneImage( gray_img0 ); } return gray_image; } ////////////////////////////////// // initAll() // int initAll() { if( !initCapture() ) return 0; if( !initFaceDet(OPENCV_ROOT "/data/haarcascades/haarcascade_frontalface_default.xml")) return 0; // Startup message tells user how to begin and how to exit printf( "\n********************************************\n" "To exit, click inside the video display,\n" "then press the ESC key\n\n" "Press to begin" "\n********************************************\n" ); fgetc(stdin); // Create the display window cvNamedWindow( DISPLAY_WINDOW, 1 ); cvNamedWindow( DISPLAY_WINDOW2, 1 ); //cvSetMouseCallback( DISPLAY_WINDOW, on_mouse, 0 ); // Initialize tracker captureVideoFrame(); //if( !createTracker(pVideoFrameCopy) ) return 0; return 1; } ////////////////////////////////// // exitProgram() // void exitProgram(int code) { // Release resources allocated in this file cvDestroyWindow( DISPLAY_WINDOW ); cvReleaseImage( &pVideoFrameCopy ); // Release resources allocated in other project files closeCapture(); closeFaceDet(); //releaseTracker(); exit(code); } ////////////////////////////////// // captureVideoFrame() // void captureVideoFrame() { // Capture the next frame IplImage * pVideoFrame = nextVideoFrame(); if( !pVideoFrame ) exitProgram(-1); // Copy it to the display image, inverting it if needed if( !pVideoFrameCopy ) pVideoFrameCopy = cvCreateImage( cvGetSize(pVideoFrame), 8, 3 ); cvCopy( pVideoFrame, pVideoFrameCopy, 0 ); pVideoFrameCopy->origin = pVideoFrame->origin; if( 1 == pVideoFrameCopy->origin ) // 1 means the image is inverted { cvFlip( pVideoFrameCopy, 0, 0 ); pVideoFrameCopy->origin = 0; } }