-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaceDetector.cpp
161 lines (122 loc) · 4.51 KB
/
faceDetector.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
/////////////////////////////////////////////////////////////////////////////
//
// COMS30121 - face.cpp
//
/////////////////////////////////////////////////////////////////////////////
// header inclusion
// header inclusion
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
using namespace cv;
/** Function Headers */
void detectAndDisplay( Mat frame );
float F1Test( int facesDetected, const char* imgName, Mat frame );
/** Global variables */
String cascade_name = "frontalface.xml";
CascadeClassifier cascade;
std::vector<Rect> detectedFaces;
std::vector<Rect> trueFaces;
int main( int argc, const char** argv )
{
const char* imgName = argv[1];
// 1. Read Input Image
Mat frame = imread(argv[1], CV_LOAD_IMAGE_COLOR);
// 2. Load the Strong Classifier in a structure called `Cascade'
if( !cascade.load( cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
// 3. Detect Faces and Display Result
detectAndDisplay( frame );
// ADDED: 4. Perform F1 test
float f1score = F1Test(detectedFaces.size(), imgName, frame);
// 5. Save Result Image
imwrite( "detected.jpg", frame );
return 0;
}
float F1Test( int facesDetected, const char* imgName, Mat frame ){
int validFaces = 0;
// Manipulate string to get correct CSV file name
string fileExtension = "points.csv";
string current_line;
std::string imgNameString(imgName);
string::size_type i = imgNameString.rfind('.', imgNameString.length());
if (i != string::npos) {
imgNameString.replace(i, fileExtension.length(), fileExtension);
}
const char *c = imgNameString.c_str();
ifstream inputFile(c);
// Go through CSV file line by line
while(getline(inputFile, current_line)){
// Array of values for each rectangle
std::vector<int> values;
std::stringstream convertor(current_line);
std::string token; // somewhere to put the comma separated value
// Insert each value into values array
while (std::getline(convertor, token, ',')) {
values.push_back(std::atoi(token.c_str()));
}
// Populate array with ground truth rectangles
trueFaces.push_back(Rect(values[0], values[1], values[2], values[3]));
}
int truePositives = 0;
int falsePositives = 0;
// Compare each detected face to every ground truth face
for (int i = 0; i < detectedFaces.size(); i++) {
for (int j = 0; j < trueFaces.size(); j++) {
// Get intersection and check matching area percentage
Rect intersection = detectedFaces[i] & trueFaces[j];
float intersectionArea = intersection.area();
// If there is an intersection, check percentage of intersection area
// to detection area
if (intersectionArea > 0) {
float matchPercentage = (intersectionArea / trueFaces[j].area()) * 100;
// If threshold reached, increment true positives
if (matchPercentage > 60){
truePositives++;
break;
}
if (j == (trueFaces.size() - 1)) falsePositives++;
}
// If loop reaches end without reaching intersection threshold, it is
// a false negative
else {
if (j == (trueFaces.size() - 1)) falsePositives++;
}
}
}
std::cout << "true positives: " << truePositives << ", false positives: " << falsePositives << "\n";
// Time for F1 test
// Precision = TP / (TP + FP)
// Recall = TPR (True Positive Rate)
// F1 = 2((PRE * REC)/(PRE + REC))
float precision = (float)truePositives / ((float)truePositives + (float)falsePositives);
float recall = (float)truePositives / (float)trueFaces.size();
float f1 = 2 * ((precision * recall)/(precision + recall));
std::cout << "f1 score: " << f1 << "\n";
return f1;
}
void detectAndDisplay( Mat frame )
{
Mat frame_gray;
// 1. Prepare Image by turning it into Grayscale and normalising lighting
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
// 2. Perform Viola-Jones Object Detection
cascade.detectMultiScale( frame_gray, detectedFaces, 1.1, 1, 0|CV_HAAR_SCALE_IMAGE, Size(50, 50), Size(500,500) );
// 3. Print number of Faces found
std::cout << "faces detected: " << detectedFaces.size() << std::endl;
// 4. Draw box around faces found
for( int i = 0; i < detectedFaces.size(); i++ )
{
rectangle(frame, Point(detectedFaces[i].x, detectedFaces[i].y), Point(detectedFaces[i].x + detectedFaces[i].width, detectedFaces[i].y + detectedFaces[i].height), Scalar( 0, 255, 0 ), 2);
}
}