source: level2/competencies/FaceTracking/src/Main.cpp @ 203

Revision 203, 5.8 KB checked in by dave, 10 years ago (diff)

added ExpressionRecog?

Line 
1// AUTHOR: Ginevra Castellano
2// Queen Mary University of London
3// DATE: 10/2009
4// VERSION: 1.0
5
6// Copyright (C) 2009 Ginevra Castellano
7
8// This file is part of the FaceTracking program
9
10// FaceTracking is free software: you can redistribute it and/or modify
11// it under the terms of the GNU Lesser General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18// GNU Lesser General Public License for more details.
19
20// You should have received a copy of the GNU Lesser General Public License
21// along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23// FaceTracking uses the OpenCV library
24// Copyright (C) 2000-2006, Intel Corporation, all rights reserved.
25// Third party copyrights are property of their respective owners.
26// See OpenCV_license.txt, in the program folder, for details.
27
28// FaceTracking uses the Camshift wrapper program (see camshift_wrapper.cpp and camshift_wrapper.h)
29// Copyright (c) 2007, Robin Hewitt (http://www.robin-hewitt.com)
30// See License.txt, in the Camshift wrapper folder, for details.
31
32#ifdef WIN32
33#include "stdafx.h"
34#endif
35
36#include "cv.h"
37#include "highgui.h"
38
39#include <stdio.h>
40
41#include <stdlib.h>
42#include <string.h>
43#include <assert.h>
44#include <math.h>
45#include <float.h>
46#include <limits.h>
47#include <time.h>
48#include <ctype.h>
49
50#include <vector>
51
52#include <camshift_wrapper.h>
53
54#include "CaptureFrame.h"
55#include "FaceDetection.h"
56
57
58#ifdef _EiC
59#define WIN32
60#endif
61
62
63// Function definition
64CvRect* waitForFaceDetect(FaceDetection* fD, CaptureFrame* cF);
65
66
67
68int main( int argc, char** argv )
69{
70        // Store the first detected face
71        CvRect *pFaceRect = NULL;
72
73        // Store the tracked face
74        CvRect nextFaceRect;
75        //CvBox2D faceBox;
76
77        // Points to draw the face rectangle
78        CvPoint pt1 = cvPoint(0,0);
79        CvPoint pt2 = cvPoint(0,0);
80       
81        char c = 0; 
82
83        // Object faceDetection of the class "FaceDetection"
84    FaceDetection faceDetection;
85
86        // Object captureFrame of the class "CaptureFrame"
87        CaptureFrame captureFrame;
88
89        // Create a new window
90    cvNamedWindow("tracked face", 1);
91
92        printf("\nPress r to re-initialise tracking");
93
94        // Capture from the camera
95        captureFrame.StartCapture();
96
97        bool finished = captureFrame.CaptureNextFrame(); // capture into frameCopy
98        if (finished) // if video is finished
99         {
100           captureFrame.DeallocateFrames();
101           releaseTracker();
102           cvDestroyWindow("tracked face");
103           return 0;
104         }
105                     
106    // Create the tracker
107    if(!createTracker(captureFrame.getFrameCopy()))
108           fprintf( stderr, "ERROR: tracking initialisation\n" );
109
110        // Set Camshift parameters
111        setVmin(30);
112        setSmin(20);
113
114        // Capture video until a face is detected
115        pFaceRect = waitForFaceDetect(&faceDetection, &captureFrame);
116        // Start tracking
117        if (pFaceRect == NULL)
118        {
119         captureFrame.DeallocateFrames();
120         releaseTracker();
121         // Destroy the window previously created
122         cvDestroyWindow("tracked face");
123         return 0;
124        }
125        // Start tracking
126        startTracking(captureFrame.getFrameCopy(), pFaceRect);
127
128
129        // Track the detected face using CamShift
130        while(1)
131        {
132                finished = captureFrame.CaptureNextFrame(); //capture to frameCopy
133               
134                if (finished)
135                {
136              captureFrame.DeallocateFrames();
137                  releaseTracker();
138                  cvDestroyWindow("tracked face");
139                  return 0;
140                }
141                         
142                // Track the face in the new video frame
143                nextFaceRect = track(captureFrame.getFrameCopy());
144                //faceBox = track(captureFrame.getFrameCopy());
145
146                pt1.x = nextFaceRect.x;
147        pt1.y = nextFaceRect.y;
148        pt2.x = pt1.x + nextFaceRect.width;
149        pt2.y = pt1.y + nextFaceRect.height;
150
151                // Draw face rectangle
152                cvRectangle(captureFrame.getFrameCopy(), pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
153
154                // Draw face ellipse
155                //cvEllipseBox(captureFrame.getFrameCopy(), faceBox,
156                             //CV_RGB(255,0,0), 3, CV_AA, 0 );
157
158                cvShowImage("tracked face", captureFrame.getFrameCopy());
159                       
160                c = cvWaitKey(100);
161                switch(c)
162                {               
163                        case 27: break;
164                                break;
165                        case 'r': printf("\nKey pressed for re-initialisation");
166                                // Capture video until a face is detected
167                                pFaceRect = waitForFaceDetect(&faceDetection, &captureFrame);
168                               
169                                if (pFaceRect == NULL)
170                                {
171                                 captureFrame.DeallocateFrames();
172                                 releaseTracker();
173                                 // Destroy the window previously created
174                                 cvDestroyWindow("tracked face");
175                             return 0;
176                                }
177                                releaseTracker();
178                                // Start tracking
179                                startTracking(captureFrame.getFrameCopy(), pFaceRect);
180                                break;
181                }
182        }
183
184        // Release the image and tracker
185        captureFrame.DeallocateFrames();
186    releaseTracker();
187
188    // Destroy the window previously created
189    cvDestroyWindow("tracked face");
190    return 0;
191}
192
193
194
195//////////////////////
196//waitForFaceDetect
197/////////////////////
198
199CvRect* waitForFaceDetect(FaceDetection* fD, CaptureFrame* cF)
200{
201        CvRect *pFaceRect = NULL;
202        bool finished = false;
203
204        // Load face cascades
205        fD->InitFaceDetection();
206       
207        while(1)
208        {
209          finished = cF->CaptureNextFrame(); //capture to frameCopy
210          if (finished)
211      {
212        cF->DeallocateFrames();
213        releaseTracker();
214        cvDestroyWindow("tracked face");
215        return NULL;
216      }
217         
218          // Detect face
219          pFaceRect = fD->detectFace(cF->getFrameCopy());                       
220
221          // Wait for a while before proceeding to the next frame
222      if(cvWaitKey(10) >= 0);
223
224      // When a face is found, quit the loop
225      if(pFaceRect)
226          {
227            printf("\nFound a face rectangle data %d, %d", pFaceRect->height, pFaceRect->width);
228                return pFaceRect;
229          }
230        }
231}
Note: See TracBrowser for help on using the repository browser.