Repository navigation
Expand file tree
/
Copy pathfacialRecognitionAngleDetection.cpp
More file actions
407 lines (343 loc) · 14.8 KB
/
Copy pathfacialRecognitionAngleDetection.cpp
File metadata and controls
407 lines (343 loc) · 14.8 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#define _USE_MATH_DEFINES
#include "opencv2/objdetect.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
using namespace cv;
/** Function Headers */
void detectAndDisplay(Mat frame);
vector<Rect> detectNoses(Mat faceROI, Rect face);
vector<Rect> detectMouths(Mat faceROI, Rect face);
vector<Rect> detectFaces(Mat frame_gray);
vector<float> displayNose(Rect nose, Mat frame, Rect face);
vector<float> displayMouth(Rect mouth, Mat frame, Rect face);
void displayFace(Mat frame, Rect face);
Rect determineCorrectNose(vector<Rect> noses, Rect face);
Rect determineCorrectMouth(vector<Rect> mouths, Rect face);
vector<float> getVariance(vector<Rect> attributes);
vector<float> getAvgXYCenter(vector<Rect> attributes);
Rect findSingleBestAttribute(vector<Rect> attributes, float avgX, float avgY, float stndDeviationX, float stndDeviationY, float STD_DEV_CONSTANT);
Rect getAvgAttribute(vector<Rect> attributes);
vector<float> determineAngle(vector<float> configuration, vector<float> nosePositions, vector<float> mouthPositions);
float determineDistance(vector<float> configuration, Rect face);
vector<float> parseConfigFile(String fileName);
/** Global variables */
string window_name = "Capture - Face detection";
RNG rng(12345);
float MOUTH_STANDARD_DEVIATIONS = 2.0;
float NOSE_STANDARD_DEVIATIONS = 2.0;
String face_cascade_name = "C:/opencv/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml";
String mouth_cascade_name = "C:/opencv/opencv/sources/data/haarcascades/mouth.xml";
String nose_cascade_name = "C:/opencv/opencv/sources/data/haarcascades/nose.xml";
String CONFIG_FILE = "C:/Users/Evan/Desktop/configuration.txt";
CascadeClassifier face_cascade, mouth_cascade, nose_cascade;
/*
Runs the program. Takes the camera input and determines
1. If there is a face in the shot
2. If so, finds
a. the face's distance from the camera
b. yaw rotation
c. pitch rotation
*/
int main(int argc, const char** argv)
{
// Load the cascades
if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading face cascade\n"); return -1; };
if (!mouth_cascade.load(mouth_cascade_name)) { printf("--(!)Error loading mouth cascade\n"); return -1; };
if (!nose_cascade.load(nose_cascade_name)) { printf("--(!)Error loading nose cascade\n"); return -1; }
//Open the video stream (0 is the webcam)
VideoCapture cap(0);
if (!cap.isOpened()) {
return -1;
}
//go through 100 frames
for (int q = 0; q<100; q++) {
Mat frame;
cap >> frame;
if (!frame.empty())
{
detectAndDisplay(frame);
}
else
{
printf(" --(!) No captured frame -- Break!"); break;
}
int c = waitKey(1);
if ((char)c == 'c') { break; }
}
return 0;
}
//Finds faces, mouths, and noses
//Performs the bulk of computation
void detectAndDisplay(Mat frame)
{
//initialization
std::vector<Rect> faces;
Mat frame_gray;
cvtColor(frame, frame_gray, CV_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
//Detect faces
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
if (faces.size() > 1) {
cout << "Hey! More than 1 face was detected in the scene." << endl;
}
//Note: only goes through 1 face here (sometimes nonexistent faces are detected)
for (size_t i = 0; i < faces.size(); i++)
{
vector<float> nosePositions, mouthPositions;
Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
ellipse(frame, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
Mat faceROI = frame_gray(faces[i]);
//detect, filter, and display noses
vector<Rect> noses = detectNoses(faceROI, faces[i]);
Rect bestNose = determineCorrectNose(noses, faces[i]);
nosePositions = displayNose(bestNose, frame, faces[i]); //normalized to the face ( betweeen 0 and 1)
//detect, filter, and display mouths
vector<Rect> mouths = detectMouths(faceROI, faces[i]);
Rect bestMouth = determineCorrectMouth(mouths, faces[i]);
mouthPositions = displayMouth(bestMouth, frame, faces[i]); //normalized to the face
/*
vector<float> configuration{ 0.45f, 0.0f,0.4f,0.0f,
0.32f, 0.52f,0.34f,0.7f,
0.24f, 0.0f,0.29f,0.0f,
0.0f,0.57f,0.0f,0.8f,
0.0f,0.3f,0.0f,0.6f,
200.0f };
*/
//NOTE: CONFIG FILE IS BEING PARSED EVERY FRAME
vector<float> configuration = parseConfigFile(CONFIG_FILE);
//calculate yaw and pitch rotations
vector<float> angles = determineAngle(configuration, nosePositions, mouthPositions);
//determine how far away the face is to the camera
float distance = determineDistance(configuration, faces[i]);
cout << "Dist: " << distance << endl;
cout << "Yaw: " << angles[0] << endl;
cout << "Pitch: " << angles[1] << endl;
}
imshow(window_name, frame);
}
vector<float> parseConfigFile(String fileName) {
ifstream myFile;
myFile.open(fileName);
vector<float> configuration;
std::string line;
while (getline(myFile, line)) {
std::istringstream iss(line);
float noseX, noseY, mouthX, mouthY, width;
if (iss >> width) {
configuration.push_back(width);
}
else { break; }
if (iss >> noseY >> mouthX >> mouthY) {
configuration.push_back(noseY);
configuration.push_back(mouthX);
configuration.push_back(mouthY);
}
else { break; }
}
return configuration;
}
//Using initial configurations, determines how far away the face is from the camera
//The observed size of the face falls off as 1/distance ratio
float determineDistance(vector<float> configuration, Rect face) {
float initalDistance = 0.6; //meters
float initalHeadWidth = configuration[20]; //pixels
float currentHeadWidth = face.width; //pixels
return initalDistance*initalHeadWidth / currentHeadWidth; //meters
}
vector<float> determineAngle(vector<float> configuration, vector<float> nosePositions, vector<float> mouthPositions) {
// config {leftNoseX, leftNoseY, leftMouthX, leftMouthY,
// midNoseX, midNoseY, midMouthX, midMouthY
// rightNoseX, rightNoseY, rightMouthX, rightMouthY,
// downNoseX, downNoseY, downMouthX, downMouthY,
// upNoseX, upNoseY, upMouthX, upMouthY,
// faceWidth}
float leftNoseXConfig = configuration[0]; float leftMouthXConfig = configuration[2];
float midNoseXConfig = configuration[4]; float midMouthXConfig = configuration[6];
float rightNoseXConfig = configuration[8]; float rightMouthXConfig = configuration[10];
float currentNoseX = nosePositions[0]; float currentMouthX = mouthPositions[0];
float downNoseYConfig = configuration[13]; float downMouthYConfig = configuration[15];
float midNoseYConfig = configuration[5]; float midMouthYConfig = configuration[7];
float upNoseYConfig = configuration[17]; float upMouthYConfig = configuration[19];
float currentNoseY = nosePositions[1]; float currentMouthY = mouthPositions[1];
float yawAngleFromNose, yawAngleFromMouth, distanceBetween, yawAngle, pitchAngle, pitchAngleFromNose, pitchAngleFromMouth;
float midAngleValue = 0.0;
float leftAngleValue = 50.0;
float rightAngleValue = -40.0;
float downAngleValue = -30.0;
float upAngleValue = 50.0;
//determine yaw (horizontal) angle
distanceBetween = abs(leftNoseXConfig - midNoseXConfig);
yawAngleFromNose = ((1 - ((currentNoseX - midNoseXConfig) / distanceBetween))*midAngleValue) + ((currentNoseX - midNoseXConfig) / distanceBetween)*leftAngleValue;
distanceBetween = abs(leftMouthXConfig - midMouthXConfig);
yawAngleFromMouth = ((1 - ((currentMouthX - midMouthXConfig) / distanceBetween))*midAngleValue) + ((currentMouthX - midMouthXConfig) / distanceBetween)*leftAngleValue;
//determine pitch (vertical) angle
if (currentNoseY < midNoseYConfig) {
distanceBetween = abs(upNoseYConfig - midNoseYConfig);
pitchAngleFromNose = ((1 - (abs(currentNoseY - midNoseYConfig) / distanceBetween))*midAngleValue) + (abs(currentNoseY - midNoseYConfig) / distanceBetween)*upAngleValue;
}
else {
distanceBetween = abs(downNoseYConfig - midNoseYConfig);
pitchAngleFromNose = ((1 - (abs(currentNoseY - midNoseYConfig) / distanceBetween))*midAngleValue) + (abs(currentNoseY - midNoseYConfig) / distanceBetween)*downAngleValue;
}
distanceBetween = abs(downMouthYConfig - midMouthYConfig);
pitchAngleFromMouth = ((1 - ((currentMouthY - midMouthYConfig) / distanceBetween))*midAngleValue) + ((currentMouthY - midMouthYConfig) / distanceBetween)*downAngleValue;
if (currentNoseX == 0 && currentNoseY == 0) {
//no nose was detected
yawAngle = yawAngleFromMouth;
pitchAngle = pitchAngleFromMouth;
}
else {
yawAngle = yawAngleFromNose;
pitchAngle = pitchAngleFromNose;
}
return{ yawAngle, pitchAngle };
}
//for a vector of attributes (face, nose, mouth, etc..), returns a Rect representing
//the average of position and size of the attributes Rect(x,y,width,height)
Rect getAvgAttribute(vector<Rect> attributes) {
float totalX = 0.0;
float totalY = 0.0;
float totalWidth = 0.0;
float totalHeight = 0.0;
int s = attributes.size();
for (int i = 0; i < attributes.size(); i++) {
totalX += attributes[i].x;
totalY += attributes[i].y;
totalWidth += attributes[i].width;
totalHeight += attributes[i].height;
}
return Rect(totalX / float(s), totalY / float(s), totalWidth / float(s), totalHeight / float(s));
}
//Given a vector of noses, finds a single nose that best represents the group
Rect determineCorrectNose(vector<Rect> noses, Rect face) {
if (noses.size() == 0) {
//edge case: if there is no nose found, return an empty Rect
return Rect();
}
//get the spatial variance of the mouth locations
float varianceX = getVariance(noses)[0];
float varianceY = getVariance(noses)[1];
float stdDevX = sqrt(varianceX);
float stdDevY = sqrt(varianceY);
float avgX = getAvgXYCenter(noses)[0];
float avgY = getAvgXYCenter(noses)[1];
//chop off outliers
return findSingleBestAttribute(noses, avgX, avgY, stdDevX, stdDevY, NOSE_STANDARD_DEVIATIONS);
}
Rect determineCorrectMouth(vector<Rect> mouths, Rect face) {
// simple check if mouths are in bottom half of face
float faceCenterX = face.width / 2.0;
float faceCenterY = face.height / 2.0;
vector<Rect> newMouths;
for (int i = 0; i < mouths.size(); i++) {
if (mouths[i].y > faceCenterY) {
newMouths.push_back(mouths[i]);
}
}
if (newMouths.size() == 0) {
//edge case: if no mouths are in the bottom half of the face, return empty Rect
return Rect();
}
//get the spatial variance of the mouth locations
float varianceX = getVariance(newMouths)[0];
float varianceY = getVariance(newMouths)[1];
float stdDevX = sqrt(varianceX);
float stdDevY = sqrt(varianceY);
float avgX = getAvgXYCenter(newMouths)[0];
float avgY = getAvgXYCenter(newMouths)[1];
//chop off outliers who are not within 2 std deviations
return findSingleBestAttribute(newMouths, avgX, avgY, stdDevX, stdDevY, MOUTH_STANDARD_DEVIATIONS);
}
//detect all of the noses within the face
vector<Rect> detectNoses(Mat faceROI, Rect face) {
vector<Rect> noses;
nose_cascade.detectMultiScale(faceROI, noses, 1.1, 0, 0 | CASCADE_SCALE_IMAGE , Size(face.width / 5.5, face.width / 5.5), Size(face.width / 3.0, face.width / 3.0));
return noses;
}
//detect all of the mouths within the face
vector<Rect> detectMouths(Mat faceROI, Rect face) {
vector<Rect> mouths;
mouth_cascade.detectMultiScale(faceROI, mouths, 1.1, 0, 0 | CASCADE_SCALE_IMAGE, Size(face.width / 4.0, face.height / 8.0), Size(face.width / 2.5, face.height / 5.0));
//cout << "NUM MOUTHS: " << mouths.size() << endl;
return mouths;
}
//puts an ellipse in the frame at the location of the nose
//returns the location of the nose with respect to the face
vector<float> displayNose(Rect nose, Mat frame, Rect face) {
Point nCenter(face.x + nose.x + nose.width / 2, face.y + nose.y + nose.height / 2);
ellipse(frame, nCenter, Size(nose.width / 4, nose.height / 4), 0, 0, 360, Scalar(255, 255, 0), 4, 8, 0);
return{ nose.x / float(face.width) , nose.y / float(face.height) };
}
//puts an ellipse in the frame at the location of the mouth
//returns the location of the mouth with respect to the face
vector<float> displayMouth(Rect mouth, Mat frame, Rect face) {
Point mCenter(face.x + mouth.x + mouth.width / 2, face.y + mouth.y + mouth.height / 2);
ellipse(frame, mCenter, Size(mouth.width*0.75, mouth.height / 2), 0, 0, 360, Scalar(0, 255, 0), 4, 8, 0);
return{ mouth.x / float(face.width) , mouth.y / float(face.height) };
}
//gets the variance of an attribute distribution
//returns vector representing <xVariance, yVariance>
vector<float> getVariance(vector<Rect> attributes) {
//get spatial variance
float avgX = getAvgXYCenter(attributes)[0];
float avgY = getAvgXYCenter(attributes)[1];
float sumTotalX = 0.0;
float sumTotalY = 0.0;
for (int j = 0; j < attributes.size(); j++) {
float attributeCenterX = attributes[j].x + attributes[j].width / 2.0;
float attributeCenterY = attributes[j].y + attributes[j].height / 2.0;
sumTotalX += pow((attributeCenterX - avgX), 2.0);
sumTotalY += pow((attributeCenterY - avgY), 2.0);
}
float varianceX = sumTotalX / float(attributes.size());
float varianceY = sumTotalY / float(attributes.size());
vector<float> variances{ varianceX, varianceY };
return variances;
}
//given a vector of attributes, gets the average x and y coordinates of the attributes
vector<float> getAvgXYCenter(vector<Rect> attributes) {
float totalX = 0.0;
float totalY = 0.0;
float totalWeight = 0.0;
float totalHeight = 0.0;
for (int i = 0; i < attributes.size(); i++) {
float attributeCenterX = attributes[i].x + attributes[i].width / 2.0;
float attributeCenterY = attributes[i].y + attributes[i].height / 2.0;
totalX += attributeCenterX;
totalY += attributeCenterY;
}
float avgX = totalX / float(attributes.size());
float avgY = totalY / float(attributes.size());
vector<float> avg{ avgX, avgY };
return avg;
}
//given a list of attributes, determines the most probable location and size
//of the best attribute
Rect findSingleBestAttribute(vector<Rect> attributes, float avgX, float avgY, float stndDeviationX, float stndDeviationY, float STD_DEV_CONSTANT) {
float totalWidth = 0.0;
float totalHeight = 0.0;
vector<Rect> newAttributes;
for (int i = 0; i < attributes.size(); i++) {
float attributesCenterX = attributes[i].x + attributes[i].width / 2.0;
float attributesCenterY = attributes[i].y + attributes[i].height / 2.0;
if (abs(attributesCenterX - avgX) <= stndDeviationX*STD_DEV_CONSTANT
&& abs(attributesCenterY - avgY) <= stndDeviationY*STD_DEV_CONSTANT) {
totalWidth += attributes[i].width;
totalHeight += attributes[i].height;
newAttributes.push_back(attributes[i]);
}
}
float avgWidth = totalWidth / newAttributes.size();
float avgHeight = totalHeight / newAttributes.size();
float newAvgX = getAvgXYCenter(newAttributes)[0];
float newAvgY = getAvgXYCenter(newAttributes)[1];
//take avg center and dimensions
return Rect(newAvgX - avgWidth / 2.0, newAvgY - avgHeight / 2.0, avgWidth, avgHeight);
}