/
FaceDetect.cpp
209 lines (186 loc) · 5.51 KB
/
FaceDetect.cpp
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
/*
* FaceDetect.cpp
*
* Created on: Nov 25, 2009
* Author: sushil
*/
#include "FaceDetect.h"
#include "X11/Xlib.h"
static CvHaarClassifierCascade* haarcascade;
CvRect oldFaceRect;
bool isFirst=true;
FaceDetect::FaceDetect() {
// TODO Auto-generated constructor stub
}
void FaceDetect::extractFace(IplImage* img){
/**
* Detect Faces, extract ROI -> largest face and return it
* Call EyeExtract??(instead of EyeDetect, rename EyeDetect->EyeExtract)
* Call EyeTracking
*/
////////////
int radius=0,height=0,x=0,y=0;
/////////
int scale = 1;
// Create two points to represent the face locations
CvPoint pt1, pt2;
int i;
CvMemStorage* storage = cvCreateMemStorage(0);
// Clear the memory storage which was used before
// cvClearMemStorage( storage );
CvRect* r1=0;
CvRect* largestRect;
int nFaces;
// Find whether the cascade is loaded, to find the faces. If yes, then:
if( haarcascade ) {
// There can be more than one face in an image. So create a growable sequence of faces.
// Detect the objects and store them in the sequence
CvSeq* faces = cvHaarDetectObjects( img, haarcascade, storage, 1.1, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(40, 40) );
int largeArea=0;
// Loop the number of faces found.
nFaces=faces->total;
for( i = 0; i < (faces ? faces->total : 0); i++ ){
// Create a new rectangle for drawing the face
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
r1=r;
// Find the dimensions of the face,and scale it if necessary
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
// Draw the rectangle in the input image
int area=(pt2.x-pt1.x)*(pt2.y-pt1.y);
if(area>largeArea){
largestRect=r;
}
}
pt1.x = largestRect->x*scale;
pt2.x = (largestRect->x+largestRect->width)*scale;
pt1.y = largestRect->y*scale;
pt2.y = (largestRect->y+largestRect->height)*scale;
//cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
//cout<<"FAceDEteCted";
}
//cvShowImage("detected face",img);
//cvWaitKey(0);
if(nFaces==0){
cvReleaseMemStorage(&storage);
return;
}
CvRect faceRect;
faceRect.x=largestRect->x;
faceRect.y=largestRect->y;
faceRect.height=largestRect->height;
faceRect.width=largestRect->width;
/**
* If we remove this block Memory leak is significantly reduced !!! or none at all
*/
// IplImage* temp=cvCloneImage(img);
// if(largestRect){
// ExtractROI extractROI;
// temp=extractROI.extractROI(img,faceRect);
// }
if(largestRect){
if(isFirst){
oldFaceRect=faceRect;
isFirst=false;
}else{
if(abs(oldFaceRect.width-faceRect.width) < 3 || abs(oldFaceRect.x-faceRect.x) < 3){
// img=0;
// return;
faceRect=oldFaceRect;
}
}
cvSetImageROI(img,faceRect);
}else{
img=0;
}
cvReleaseMemStorage(&storage);
}
CvSeq* FaceDetect::detectFaces(IplImage* img){
CvMemStorage* storage=cvCreateMemStorage(0);
cvClearMemStorage(storage);
// Clear the memory storage which was used before
cvClearMemStorage( storage );
CvSeq* faces;
if( haarcascade ) {
// There can be more than one face in an image. So create a growable sequence of faces.
// Detect the objects and store them in the sequence
faces = cvHaarDetectObjects( img, haarcascade, storage,
1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
cvSize(40, 40) );
}else{
faces->total=0;
}
return faces;
}
CvRect* FaceDetect::getLargestFaceCoordinates(CvSeq* faces,IplImage* img){
// Loop the number of faces found.
CvRect r2;
CvRect* r[faces->total];
CvPoint pt1, pt2;
int scale = 1;
int largest=0;
int largeArea=0;
for( int i = 0; i < (faces ? faces->total : 0); i++ ){
// Create a new rectangle for drawing the face
r[i] = (CvRect*)cvGetSeqElem( faces, i );
r2=*r[i];
// Find the dimensions of the face,and scale it if necessary
pt1.x = r[i]->x*scale;
pt2.x = (r[i]->x+r[i]->width)*scale;
pt1.y = r[i]->y*scale;
pt2.y = (r[i]->y+r[i]->height)*scale;
int area=(pt2.x-pt1.x)*(pt2.y-pt1.y);
if(area>largeArea){
largest=i;
}
}
return r[largest];
}
CvRect** FaceDetect::getFaceCoordinates(CvSeq* faces,IplImage* img){
// Loop the number of faces found.
CvRect r2;
CvRect* r[faces->total];
CvPoint pt1, pt2;
int scale = 1;
for( int i = 0; i < (faces ? faces->total : 0); i++ )
{
// Create a new rectangle for drawing the face
r[i] = (CvRect*)cvGetSeqElem( faces, i );
r2=*r[i];
// Find the dimensions of the face,and scale it if necessary
pt1.x = r[i]->x*scale;
pt2.x = (r[i]->x+r[i]->width)*scale;
pt1.y = r[i]->y*scale;
pt2.y = (r[i]->y+r[i]->height)*scale;
// Draw the rectangle in the input image
// cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
}
/*
if(r){
r2.y=r2.y+r2.height/4;
r2.width=r2.width/2;
r2.height=r2.height/4;
r2.x=r2.x+r2.width/8;
}
*/
return r;
}
void FaceDetect::drawRectangleAroundFace(IplImage* img,CvRect** r,int numberOfFaces){
for(int i=0;i<numberOfFaces;i++){
// Draw the rectangle in the input image
CvPoint pt1,pt2;
pt1.x = r[i]->x;
pt2.x = (r[i]->x+r[i]->width);
pt1.y = r[i]->y;
pt2.y = (r[i]->y+r[i]->height);
cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 2, 8, 0 );
}
}
void FaceDetect::setHaarClassifier(CvHaarClassifierCascade* haarcascade1){
haarcascade=haarcascade1;
}
FaceDetect::~FaceDetect() {
// TODO Auto-generated destructor stub
}