forked from yulequan/face-alignment-in-3000fps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Facedetect.cpp
executable file
·210 lines (191 loc) · 7.12 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
210
//
// Facedetect.cpp
// myopencv
//
// Created by lequan on 1/24/15.
// Copyright (c) 2015 lequan. All rights reserved.
//
#include "LBFRegressor.h"
using namespace std;
using namespace cv;
int save_count=0;
void detectAndDraw(Mat& img,
CascadeClassifier& nestedCascade, LBFRegressor& regressor,
double scale, bool tryflip );
int FaceDetectionAndAlignment(const char* inputname){
extern string cascadeName;
string inputName;
CvCapture* capture = 0;
Mat frame, frameCopy, image;
bool tryflip = false;
double scale = 1.3;
CascadeClassifier cascade;
if (inputname!=NULL){
inputName.assign(inputname);
}
// name is empty or a number
if( inputName.empty() || (isdigit(inputName.c_str()[0]) && inputName.c_str()[1] == '\0') ){
capture = cvCaptureFromCAM( inputName.empty() ? 0 : inputName.c_str()[0] - '0' );
int c = inputName.empty() ? 0 : inputName.c_str()[0] - '0' ;
if(!capture){
cout << "Capture from CAM " << c << " didn't work" << endl;
return -1;
}
}
// name is not empty
else if( inputName.size() ){
if (inputName.find(".jpg")!=string::npos||inputName.find(".png")!=string::npos
||inputName.find(".bmp")!=string::npos){
image = imread( inputName, 1 );
if (image.empty()){
cout << "Read Image fail" << endl;
return -1;
}
}
else if(inputName.find(".mp4")!=string::npos||inputName.find(".avi")!=string::npos
||inputName.find(".wmv")!=string::npos){
capture = cvCaptureFromAVI( inputName.c_str() );
if(!capture) cout << "Capture from AVI didn't work" << endl;
return -1;
}
}
// -- 0. Load LBF model
LBFRegressor regressor;
regressor.Load(modelPath+"LBF.model");
// -- 1. Load the cascades
if( !cascade.load( cascadeName ) ){
cerr << "ERROR: Could not load classifier cascade" << endl;
return -1;
}
// cvNamedWindow( "result", 1 );
// -- 2. Read the video stream
if( capture ){
cout << "In capture ..." << endl;
for(;;){
IplImage* iplImg = cvQueryFrame( capture );
frame = iplImg;
if( frame.empty() )
break;
if( iplImg->origin == IPL_ORIGIN_TL )
frame.copyTo( frameCopy );
else
flip( frame, frameCopy, 0 );
detectAndDraw( frameCopy, cascade,regressor, scale, tryflip );
if( waitKey( 10 ) >= 0 )
goto _cleanup_;
}
waitKey(0);
_cleanup_:
cvReleaseCapture( &capture );
}
else{
if( !image.empty() ){
cout << "In image read" << endl;
detectAndDraw( image, cascade,regressor, scale, tryflip );
waitKey(0);
}
else if( !inputName.empty() ){
/* assume it is a text file containing the
list of the image filenames to be processed - one per line */
cout << "In image set model" << endl;
FILE* f = fopen( inputName.c_str(), "rt" );
if( f ){
char buf[1000+1];
while( fgets( buf, 1000, f ) ){
int len = (int)strlen(buf), c;
while( len > 0 && isspace(buf[len-1]) )
len--;
buf[len] = '\0';
cout << "file " << buf << endl;
image = imread( buf, 1 );
if( !image.empty() ){
detectAndDraw(image, cascade,regressor,scale, tryflip );
c = waitKey(0);
if( c == 27 || c == 'q' || c == 'Q' )
break;
}
else{
cerr << "Aw snap, couldn't read image " << buf << endl;
}
}
fclose(f);
}
}
}
cvDestroyWindow("result");
return 0;
}
void detectAndDraw( Mat& img, CascadeClassifier& cascade,
LBFRegressor& regressor,
double scale, bool tryflip ){
int i = 0;
double t = 0;
vector<Rect> faces,faces2;
const static Scalar colors[] = { CV_RGB(0,0,255),
CV_RGB(0,128,255),
CV_RGB(0,255,255),
CV_RGB(0,255,0),
CV_RGB(255,128,0),
CV_RGB(255,255,0),
CV_RGB(255,0,0),
CV_RGB(255,0,255)} ;
Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
cvtColor( img, gray, CV_BGR2GRAY );
resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
equalizeHist( smallImg, smallImg );
// --Detection
t = (double)cvGetTickCount();
cascade.detectMultiScale( smallImg, faces,
1.1, 2, 0
//|CV_HAAR_FIND_BIGGEST_OBJECT
//|CV_HAAR_DO_ROUGH_SEARCH
|CV_HAAR_SCALE_IMAGE
,
Size(30, 30) );
if( tryflip ){
flip(smallImg, smallImg, 1);
cascade.detectMultiScale( smallImg, faces2,
1.1, 2, 0
//|CV_HAAR_FIND_BIGGEST_OBJECT
//|CV_HAAR_DO_ROUGH_SEARCH
|CV_HAAR_SCALE_IMAGE
,
Size(30, 30) );
for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); r++ )
{
faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height));
}
}
t = (double)cvGetTickCount() - t;
printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
// --Alignment
t =(double)cvGetTickCount();
for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ ){
Point center;
Scalar color = colors[i%8];
BoundingBox boundingbox;
boundingbox.start_x = r->x*scale;
boundingbox.start_y = r->y*scale;
boundingbox.width = (r->width-1)*scale;
boundingbox.height = (r->height-1)*scale;
boundingbox.centroid_x = boundingbox.start_x + boundingbox.width/2.0;
boundingbox.centroid_y = boundingbox.start_y + boundingbox.height/2.0;
t =(double)cvGetTickCount();
Mat_<double> current_shape = regressor.Predict(gray,boundingbox,1);
t = (double)cvGetTickCount() - t;
printf( "alignment time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
// // draw bounding box
// rectangle(img, cvPoint(boundingbox.start_x,boundingbox.start_y),
// cvPoint(boundingbox.start_x+boundingbox.width,boundingbox.start_y+boundingbox.height),Scalar(0,255,0), 1, 8, 0);
// draw result :: red
for(int i = 0;i < global_params.landmark_num;i++){
circle(img,Point2d(current_shape(i,0),current_shape(i,1)),3,Scalar(255,255,255),-1,8,0);
}
}
cv::imshow( "result", img );
char a = waitKey(0);
if(a=='s'){
save_count++;
imwrite(to_string(save_count)+".jpg", img);
}
}