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

Revision 158, 5.8 KB checked in by ginevra, 10 years ago (diff)
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
33#include "stdafx.h"
34
35#include "cv.h"
36#include "highgui.h"
37
38#include <stdio.h>
39
40#include <stdlib.h>
41#include <string.h>
42#include <assert.h>
43#include <math.h>
44#include <float.h>
45#include <limits.h>
46#include <time.h>
47#include <ctype.h>
48
49#include <vector>
50
51#include "camshift_wrapper.h"
52
53#include "CaptureFrame.h"
54#include "FaceDetection.h"
55
56
57#ifdef _EiC
58#define WIN32
59#endif
60
61
62// Function definition
63CvRect* waitForFaceDetect(FaceDetection* fD, CaptureFrame* cF);
64
65
66
67int main( int argc, char** argv )
68{
69        // Store the first detected face
70        CvRect *pFaceRect = NULL;
71
72        // Store the tracked face
73        CvRect nextFaceRect;
74        //CvBox2D faceBox;
75
76        // Points to draw the face rectangle
77        CvPoint pt1 = cvPoint(0,0);
78        CvPoint pt2 = cvPoint(0,0);
79       
80        char c = 0; 
81
82        // Object faceDetection of the class "FaceDetection"
83    FaceDetection faceDetection;
84
85        // Object captureFrame of the class "CaptureFrame"
86        CaptureFrame captureFrame;
87
88        // Create a new window
89    cvNamedWindow("tracked face", 1);
90
91        printf("\nPress r to re-initialise tracking");
92
93        // Capture from the camera
94        captureFrame.StartCapture();
95
96        bool finished = captureFrame.CaptureNextFrame(); // capture into frameCopy
97        if (finished) // if video is finished
98         {
99           captureFrame.DeallocateFrames();
100           releaseTracker();
101           cvDestroyWindow("tracked face");
102           return 0;
103         }
104                     
105    // Create the tracker
106    if(!createTracker(captureFrame.getFrameCopy()))
107           fprintf( stderr, "ERROR: tracking initialisation\n" );
108
109        // Set Camshift parameters
110        setVmin(30);
111        setSmin(20);
112
113        // Capture video until a face is detected
114        pFaceRect = waitForFaceDetect(&faceDetection, &captureFrame);
115        // Start tracking
116        if (pFaceRect == NULL)
117        {
118         captureFrame.DeallocateFrames();
119         releaseTracker();
120         // Destroy the window previously created
121         cvDestroyWindow("tracked face");
122         return 0;
123        }
124        // Start tracking
125        startTracking(captureFrame.getFrameCopy(), pFaceRect);
126
127
128        // Track the detected face using CamShift
129        while(1)
130        {
131                finished = captureFrame.CaptureNextFrame(); //capture to frameCopy
132               
133                if (finished)
134                {
135              captureFrame.DeallocateFrames();
136                  releaseTracker();
137                  cvDestroyWindow("tracked face");
138                  return 0;
139                }
140                         
141                // Track the face in the new video frame
142                nextFaceRect = track(captureFrame.getFrameCopy());
143                //faceBox = track(captureFrame.getFrameCopy());
144
145                pt1.x = nextFaceRect.x;
146        pt1.y = nextFaceRect.y;
147        pt2.x = pt1.x + nextFaceRect.width;
148        pt2.y = pt1.y + nextFaceRect.height;
149
150                // Draw face rectangle
151                cvRectangle(captureFrame.getFrameCopy(), pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
152
153                // Draw face ellipse
154                //cvEllipseBox(captureFrame.getFrameCopy(), faceBox,
155                             //CV_RGB(255,0,0), 3, CV_AA, 0 );
156
157                cvShowImage("tracked face", captureFrame.getFrameCopy());
158                       
159                c = cvWaitKey(100);
160                switch(c)
161                {               
162                        case 27: break;
163                                break;
164                        case 'r': printf("\nKey pressed for re-initialisation");
165                                // Capture video until a face is detected
166                                pFaceRect = waitForFaceDetect(&faceDetection, &captureFrame);
167                               
168                                if (pFaceRect == NULL)
169                                {
170                                 captureFrame.DeallocateFrames();
171                                 releaseTracker();
172                                 // Destroy the window previously created
173                                 cvDestroyWindow("tracked face");
174                             return 0;
175                                }
176                                releaseTracker();
177                                // Start tracking
178                                startTracking(captureFrame.getFrameCopy(), pFaceRect);
179                                break;
180                }
181        }
182
183        // Release the image and tracker
184        captureFrame.DeallocateFrames();
185    releaseTracker();
186
187    // Destroy the window previously created
188    cvDestroyWindow("tracked face");
189    return 0;
190}
191
192
193
194//////////////////////
195//waitForFaceDetect
196/////////////////////
197
198CvRect* waitForFaceDetect(FaceDetection* fD, CaptureFrame* cF)
199{
200        CvRect *pFaceRect = NULL;
201        bool finished = false;
202
203        // Load face cascades
204        fD->InitFaceDetection();
205       
206        while(1)
207        {
208          finished = cF->CaptureNextFrame(); //capture to frameCopy
209          if (finished)
210      {
211        cF->DeallocateFrames();
212        releaseTracker();
213        cvDestroyWindow("tracked face");
214        return NULL;
215      }
216         
217          // Detect face
218          pFaceRect = fD->detectFace(cF->getFrameCopy());                       
219
220          // Wait for a while before proceeding to the next frame
221      if(cvWaitKey(10) >= 0);
222
223      // When a face is found, quit the loop
224      if(pFaceRect)
225          {
226            printf("\nFound a face rectangle data %d, %d", pFaceRect->height, pFaceRect->width);
227                return pFaceRect;
228          }
229        }
230}
Note: See TracBrowser for help on using the repository browser.