source: proiecte/PPPP/eigenface/TrackFaces.c @ 28

Last change on this file since 28 was 28, checked in by (none), 14 years ago

first try at eigenface

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