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