-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTrackFaces.c
274 lines (209 loc) · 6.84 KB
/
TrackFaces.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//Developed By Yash Shah
//TrackFaces.c
//
///////////////////////////////////////////////////////////////////////////////
// An example program that It detects a face in live video, and automatically
// begins tracking it, using the Simple Camshift Wrapper.
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include "capture.c"
#include "facedet.c"
#include "camshift_wrapper.c"
#include <time.h>
#include <map>
#include <string>
using namespace cv;
using namespace std;
//// Constants
const char * DISPLAY_WINDOW = "CroppedFace";
#define OPENCV_ROOT "/usr/share/kde4/apps/libkface/haarcascades/"
//// Global variables
IplImage * pVideoFrameCopy = 0;
IplImage * mouthImage = 0;
int speakingSequence = 0;
//// Function definitions
int initAll();
void exitProgram(int code);
void captureVideoFrame();
/*Mat rotateImage(const Mat& source, double angle)*/
/*{*/
/* Point2f src_center(source.cols/2.0F, source.rows/2.0F);*/
/* Mat rot_mat = getRotationMatrix2D(src_center, angle, 1.0);*/
/* Mat dst;*/
/* warpAffine(source, dst, rot_mat, source.size());*/
/* return dst;*/
/*}*/
map<string, string> cascadeMap;
void drawElements(IplImage* img2) {
float vthresh = img2->height*(1-1/1.6);
float vfactor = img2->height/9;
float hthreshL = img2->width/4;
float hthreshR = img2->width*6/9;
float hfactor = img2->width/6;
float mvthresh = img2->height*(1-1/3.6);
float mvfactor = img2->height/8;
float mhthresh = img2->width/2;
float mhfactor = img2->width/4;
cvRectangle(img2,cvPoint(hthreshL-hfactor, vthresh-vfactor), cvPoint(hthreshL+hfactor,vthresh+vfactor), CV_RGB(255,0,0), 3, 8, 0);
cvRectangle(img2,cvPoint(hthreshR-hfactor, vthresh-vfactor), cvPoint(hthreshR+hfactor,vthresh+vfactor), CV_RGB(255,0,0), 3, 8, 0);
cvRectangle(img2,cvPoint(mhthresh-mhfactor, mvthresh-mvfactor), cvPoint(mhthresh+mhfactor,mvthresh+mvfactor), CV_RGB(0,0,255), 3, 8, 0);
}
void isSpeaking(IplImage* img) {
IplImage *img2 = cvCreateImage(cvSize(400,400),img->depth,img->nChannels);
cvResize(img, img2);
int mvthresh = img2->height*(1-1/4.6);
int mvfactor = img2->height/8;
int mhthresh = img2->width/2;
int mhfactor = img2->width/4;
CvRect rect = cvRect(mhthresh-mhfactor, mvthresh-mvfactor,2*mhfactor,2*mvfactor);
cvSetImageROI( img2, rect);
IplImage *mouth = cvCreateImage(cvGetSize(img2),img2->depth,img2->nChannels);
cvCopy(img2, mouth, NULL);
IplImage *diff = cvCreateImage(cvGetSize(img2),img2->depth,img2->nChannels);
if(!mouthImage){
mouthImage = cvCreateImage(cvGetSize(img2),img2->depth,img2->nChannels);
}
cvAbsDiff(mouth, mouthImage, diff);
cvNamedWindow("Mouth");
cvShowImage("Mouth",diff);
int height = diff->height;
int width = diff->width;
int step = diff->widthStep;
int channels = diff->nChannels;
uchar* data= (uchar *)diff->imageData;
int sum = 0;
for(int i=0;i<height;i++) {
for(int j=0;j<width;j++) {
int gray = 0;
for(int k=0;k<channels;k++) {
gray += data[i*step+j*channels+k];
}
sum+=gray/3;
}
}
if(sum/1000>200) {
speakingSequence++;
printf("%d: Speaking: TRUE\n", speakingSequence);
}
cvCopy(mouth, mouthImage, NULL);
cvResetImageROI(img2);
}
//////////////////////////////////
// main()
//
int main( int argc, char** argv )
{
CvRect * pFaceRect = 0;
CvRect * pMouthRect = 0;
if( !initAll() ) exitProgram(-1);
time_t start;
// Capture and display video frames until a face
// is detected
bool second = false;
bool knownFace = false;
bool flag=false;
cvNamedWindow("Live");
while(1){
captureVideoFrame();
cvShowImage("Live",pVideoFrameCopy);
start = time (NULL);
if(start%2==0&&flag)
second = true;
if(start%2!=0)
flag=true;
if(second) {
second=false;
flag=false;
pFaceRect = detectObject(pVideoFrameCopy, "face");
if(pFaceRect !=0) knownFace=true;
else knownFace = false;
}
if(knownFace) {
// Show the display image
if( (char)27==cvWaitKey(1) ) exitProgram(0);
// initialize tracking
startTracking(pVideoFrameCopy, pFaceRect);
CvBox2D faceBox;
// track the face in the new video frame
faceBox = track(pVideoFrameCopy);
// outline face ellipse
//cvEllipseBox(pVideoFrameCopy, faceBox, CV_RGB(255,0,0), 3, CV_AA, 0 );
CvRect fastRect = cv::RotatedRect(faceBox).boundingRect();
cvSetImageROI( pVideoFrameCopy, fastRect);
IplImage *img2 = cvCreateImage(cvGetSize(pVideoFrameCopy),pVideoFrameCopy->depth,pVideoFrameCopy->nChannels);
cvCopy(pVideoFrameCopy, img2, NULL);
isSpeaking(img2);
drawElements(img2);
cvShowImage( DISPLAY_WINDOW, img2 );
cvResetImageROI(pVideoFrameCopy);
cvReleaseImage(&img2);
if( (char)27==cvWaitKey(1) ) break;
}
}
exitProgram(0);
return 0;
}
//////////////////////////////////
// initAll()
//
int initAll()
{
cascadeMap["face"] = "haarcascade_frontalface_alt.xml";
cascadeMap["mouth"] ="haarcascade_mcs_mouth.xml";
cascadeMap["leftEye"] = "haarcascade_mcs_lefteye.xml";
cascadeMap["rightEye"] = "haarcascade_mcs_righteye.xml";
if( !initCapture() ) return 0;
if( !initFaceDet(OPENCV_ROOT,
cascadeMap))
return 0;
// Startup message tells user how to begin and how to exit
printf( "\n********************************************\n"
"To exit, click inside the video display,\n"
"then press the ESC key\n\n"
"Press <ENTER> to begin"
"\n********************************************\n" );
fgetc(stdin);
// Create the display window
cvNamedWindow( DISPLAY_WINDOW, 1 );
// Initialize tracker
captureVideoFrame();
if( !createTracker(pVideoFrameCopy) ) return 0;
// Set Camshift parameters
setVmin(60);
setSmin(50);
return 1;
}
//////////////////////////////////
// exitProgram()
//
void exitProgram(int code)
{
// Release resources allocated in this file
cvDestroyWindow( DISPLAY_WINDOW );
cvReleaseImage( &pVideoFrameCopy );
// Release resources allocated in other project files
closeCapture();
closeFaceDet();
releaseTracker();
exit(code);
}
//////////////////////////////////
// captureVideoFrame()
//
void captureVideoFrame()
{
// Capture the next frame
IplImage * pVideoFrame = nextVideoFrame();
if( !pVideoFrame ) exitProgram(-1);
// Copy it to the display image, inverting it if needed
if( !pVideoFrameCopy )
pVideoFrameCopy = cvCreateImage( cvGetSize(pVideoFrame), 8, 3 );
cvCopy( pVideoFrame, pVideoFrameCopy, 0 );
pVideoFrameCopy->origin = pVideoFrame->origin;
if( 1 == pVideoFrameCopy->origin ) // 1 means the image is inverted
{
cvFlip( pVideoFrameCopy, 0, 0 );
pVideoFrameCopy->origin = 0;
}
}