[108] | 1 | // TrackFaces.c - by Robin Hewitt, 2007 |
---|
| 2 | // http://www.cognotics.com/opencv/downloads/camshift_wrapper |
---|
| 3 | // This is free software. See License.txt, in the download |
---|
| 4 | // package, for details. |
---|
| 5 | // |
---|
| 6 | |
---|
| 7 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 8 | // An example program that It detects a face in live video, and automatically |
---|
| 9 | // begins tracking it, using the Simple Camshift Wrapper. |
---|
| 10 | |
---|
| 11 | #include "cv.h" |
---|
| 12 | #include "highgui.h" |
---|
| 13 | #include <stdio.h> |
---|
| 14 | #include "capture.h" |
---|
| 15 | #include "facedet.h" |
---|
| 16 | #include "pca.h" |
---|
[133] | 17 | #include <omp.h> |
---|
[108] | 18 | |
---|
| 19 | //// Constants |
---|
| 20 | const char * DISPLAY_WINDOW = "DisplayWindow"; |
---|
| 21 | const char * DISPLAY_WINDOW2 = "FaceWindow"; |
---|
| 22 | #define OPENCV_ROOT "/home/marius/OpenCV-2.0.0" |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | //// Global variables |
---|
| 26 | IplImage * pVideoFrameCopy = 0; |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | //// Function definitions |
---|
| 30 | int initAll(); |
---|
| 31 | void exitProgram(int code); |
---|
| 32 | void captureVideoFrame(); |
---|
| 33 | IplImage* convert_grayscale(IplImage *img); |
---|
| 34 | |
---|
| 35 | ////////////////////////////////// |
---|
| 36 | // main() |
---|
| 37 | // |
---|
| 38 | void main( int argc, char** argv ) |
---|
| 39 | { |
---|
| 40 | CvRect * pFaceRect = 0; |
---|
| 41 | double start=0, end=0, t1=0; |
---|
| 42 | // validate that an input was specified |
---|
| 43 | if( argc != 2 ) |
---|
| 44 | { |
---|
| 45 | printUsage(); |
---|
| 46 | return; |
---|
| 47 | } |
---|
| 48 | |
---|
[133] | 49 | printf("%d\n", omp_get_num_threads()); |
---|
| 50 | if( !strcmp(argv[1], "train") ) |
---|
| 51 | { |
---|
| 52 | start= (double)cvGetTickCount(); |
---|
| 53 | learn("train.txt"); |
---|
| 54 | end= (double)cvGetTickCount(); |
---|
| 55 | t1= (end-start)/((double)cvGetTickFrequency()*1000.); |
---|
| 56 | printf( "Recognition time = %g ms\n", t1 ); |
---|
| 57 | } |
---|
[108] | 58 | else if( !strcmp(argv[1], "test") ) |
---|
| 59 | { |
---|
| 60 | if( !initAll() ) exitProgram(-1); |
---|
| 61 | |
---|
| 62 | // Capture and display video frames until a face |
---|
| 63 | // is detected |
---|
| 64 | while( 1 ) |
---|
| 65 | { |
---|
| 66 | // Look for a face in the next video frame |
---|
| 67 | captureVideoFrame(); |
---|
| 68 | pFaceRect = detectFace(pVideoFrameCopy); |
---|
| 69 | |
---|
| 70 | // Show the display image |
---|
| 71 | cvShowImage( DISPLAY_WINDOW, pVideoFrameCopy ); |
---|
| 72 | if( (char)27==cvWaitKey(20) ) exitProgram(0); |
---|
| 73 | |
---|
| 74 | // exit loop when a face is detected |
---|
| 75 | if(pFaceRect) break; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | while( 1 ) |
---|
| 79 | { |
---|
| 80 | CvBox2D faceBox; |
---|
| 81 | CvRect r; |
---|
| 82 | // get the next video frame |
---|
| 83 | //captureVideoFrame(); |
---|
| 84 | |
---|
| 85 | // track the face in the new video frame |
---|
| 86 | r = *pFaceRect; |
---|
| 87 | cvSetImageROI(pVideoFrameCopy, r); |
---|
| 88 | IplImage * pFaceImg = |
---|
| 89 | cvCreateImage( cvSize(92,112), pVideoFrameCopy->depth, pVideoFrameCopy->nChannels ); |
---|
| 90 | cvResize(pVideoFrameCopy, pFaceImg, CV_INTER_AREA ); |
---|
| 91 | cvResetImageROI(pVideoFrameCopy); |
---|
| 92 | pFaceImg = convert_grayscale(pFaceImg); |
---|
| 93 | |
---|
| 94 | CvPoint pt1 = { r.x, r.y }; |
---|
| 95 | CvPoint pt2 = { r.x + r.width, r.y + r.height }; |
---|
| 96 | cvRectangle(pVideoFrameCopy, pt1, pt2, CV_RGB(0,255,0), 3, 4, 0); |
---|
| 97 | |
---|
| 98 | start= (double)cvGetTickCount(); |
---|
| 99 | recognize(pFaceImg); |
---|
| 100 | end= (double)cvGetTickCount(); |
---|
| 101 | t1= (end-start)/((double)cvGetTickFrequency()*1000.); |
---|
| 102 | printf( "Recognition time = %g ms\n", t1 ); |
---|
| 103 | |
---|
| 104 | // faceBox = track(pVideoFrameCopy); |
---|
| 105 | // |
---|
| 106 | // // outline face ellipse |
---|
| 107 | // cvEllipseBox(pVideoFrameCopy, faceBox, |
---|
| 108 | // CV_RGB(255,0,0), 3, CV_AA, 0 ); |
---|
| 109 | cvShowImage( DISPLAY_WINDOW, pVideoFrameCopy ); |
---|
| 110 | cvShowImage( DISPLAY_WINDOW2, pFaceImg ); |
---|
| 111 | if( (char)27==cvWaitKey(20) ) break; |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | else |
---|
| 115 | { |
---|
| 116 | printf("Unknown command: %s\n", argv[1]); |
---|
| 117 | printUsage(); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | exitProgram(0); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | IplImage* convert_grayscale(IplImage *img) |
---|
| 124 | { |
---|
| 125 | IplImage *gray_image,*gray_img0; |
---|
| 126 | gray_image=cvCloneImage(img); |
---|
| 127 | |
---|
| 128 | //check image is it gray or not if nor convert it to the gray |
---|
| 129 | if(img->nChannels!=1){ |
---|
| 130 | //convert original image to gray_scale image |
---|
| 131 | gray_img0 = cvCreateImage(cvSize( gray_image->width, |
---|
| 132 | gray_image->height), 8, 1 ); |
---|
| 133 | cvCvtColor(gray_image, gray_img0, CV_RGB2GRAY ); |
---|
| 134 | gray_image = cvCloneImage( gray_img0 ); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | return gray_image; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | ////////////////////////////////// |
---|
| 141 | // initAll() |
---|
| 142 | // |
---|
| 143 | int initAll() |
---|
| 144 | { |
---|
| 145 | if( !initCapture() ) return 0; |
---|
| 146 | if( !initFaceDet(OPENCV_ROOT |
---|
| 147 | "/data/haarcascades/haarcascade_frontalface_default.xml")) |
---|
| 148 | return 0; |
---|
| 149 | |
---|
| 150 | // Startup message tells user how to begin and how to exit |
---|
| 151 | printf( "\n********************************************\n" |
---|
| 152 | "To exit, click inside the video display,\n" |
---|
| 153 | "then press the ESC key\n\n" |
---|
| 154 | "Press <ENTER> to begin" |
---|
| 155 | "\n********************************************\n" ); |
---|
| 156 | fgetc(stdin); |
---|
| 157 | |
---|
| 158 | // Create the display window |
---|
| 159 | cvNamedWindow( DISPLAY_WINDOW, 1 ); |
---|
| 160 | cvNamedWindow( DISPLAY_WINDOW2, 1 ); |
---|
| 161 | //cvSetMouseCallback( DISPLAY_WINDOW, on_mouse, 0 ); |
---|
| 162 | // Initialize tracker |
---|
| 163 | captureVideoFrame(); |
---|
| 164 | //if( !createTracker(pVideoFrameCopy) ) return 0; |
---|
| 165 | |
---|
| 166 | return 1; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | ////////////////////////////////// |
---|
| 171 | // exitProgram() |
---|
| 172 | // |
---|
| 173 | void exitProgram(int code) |
---|
| 174 | { |
---|
| 175 | // Release resources allocated in this file |
---|
| 176 | cvDestroyWindow( DISPLAY_WINDOW ); |
---|
| 177 | cvReleaseImage( &pVideoFrameCopy ); |
---|
| 178 | |
---|
| 179 | // Release resources allocated in other project files |
---|
| 180 | closeCapture(); |
---|
| 181 | closeFaceDet(); |
---|
| 182 | //releaseTracker(); |
---|
| 183 | |
---|
| 184 | exit(code); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | ////////////////////////////////// |
---|
| 189 | // captureVideoFrame() |
---|
| 190 | // |
---|
| 191 | void captureVideoFrame() |
---|
| 192 | { |
---|
| 193 | // Capture the next frame |
---|
| 194 | IplImage * pVideoFrame = nextVideoFrame(); |
---|
| 195 | if( !pVideoFrame ) exitProgram(-1); |
---|
| 196 | |
---|
| 197 | // Copy it to the display image, inverting it if needed |
---|
| 198 | if( !pVideoFrameCopy ) |
---|
| 199 | pVideoFrameCopy = cvCreateImage( cvGetSize(pVideoFrame), 8, 3 ); |
---|
| 200 | cvCopy( pVideoFrame, pVideoFrameCopy, 0 ); |
---|
| 201 | pVideoFrameCopy->origin = pVideoFrame->origin; |
---|
| 202 | |
---|
| 203 | if( 1 == pVideoFrameCopy->origin ) // 1 means the image is inverted |
---|
| 204 | { |
---|
| 205 | cvFlip( pVideoFrameCopy, 0, 0 ); |
---|
| 206 | pVideoFrameCopy->origin = 0; |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|