forked from EyalAr/Person-Recognizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
119 lines (96 loc) · 3.91 KB
/
main.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
/*
* File: main.cpp
* Author: Eyal Arubas <EyalArubas at gmail>
*/
/** Includes: **/
#include <cstdlib>
#include "defs.h"
#include "FramesReader.h"
#include "FramesWriter.h"
#include "CsvWriter.h"
#include "FaceDetector.h"
#include "PersonRecognizer.h"
using namespace std;
void read_training_set(const string &list_path, vector<Mat> &images) {
ifstream file(list_path.c_str());
string path;
while (getline(file, path)) {
images.push_back(imread(path, CV_LOAD_IMAGE_GRAYSCALE));
}
}
int main(int argc, char** argv) {
/** Initializations: **/
FramesReader fr(string(IN_VID), START_FRAME, END_FRAME, FRAMES_DELTA);
Size frame_size(fr.getSize()); //frame size of the input video
FaceDetector fd(string(CASCADE_PATH), DET_SCALE_FACTOR, DET_MIN_NEIGHBORS, DET_MIN_SIZE_RATIO, DET_MAX_SIZE_RATIO);
vector<Mat> training_set;
vector<Rect> faces;
Mat m;
int c = START_FRAME == -1 ? 0 : START_FRAME - 1;
#ifdef WRITE_OUTPUT
FramesWriter fw(string(OUT_VID), OUT_FPS, frame_size, OUT_FOURCC);
#endif
#ifdef WRITE_CSV
CsvWriter cw(string(CSV_FILE));
#endif
read_training_set(string(TRAINING_LIST), training_set);
PersonRecognizer pr(training_set, LBPH_RADIUS, LBPH_NEIGHBORS, LBPH_GRID_X, LBPH_GRID_Y, LBPH_THRESHOLD);
/**********************/
while (fr.getNext(m)){
c++;
#ifdef WRITE_OUTPUT
cw.addEntry(format("%d", c)); //write frame number
#endif
//detect faces in the image:
fd.findFacesInImage(m, faces);
bool has_match = false;
double match_conf = 0;
//analyze each detected face:
for (vector<Rect>::const_iterator face = faces.begin() ; face != faces.end() ; face++){
Scalar color = NO_MATCH_COLOR;
Mat face_img = m(*face);
double confidence = 0;
bool face_match = false;
//try to recognize the face:
if (pr.recognize(face_img, confidence)){
color = MATCH_COLOR;
has_match = true;
face_match = true;
match_conf = confidence;
}
Point center(face->x + face->width * 0.5, face->y + face->height * 0.5);
circle(m, center, FACE_RADIUS_RATIO * face->width, color, CIRCLE_THICKNESS, LINE_TYPE, 0);
#ifdef WRITE_OUTPUT
cw.addEntry(format("%f", face_match ? confidence : 0)); //write confidence of current face
#endif
}
#ifdef WRITE_OUTPUT
cw.nextLine();
#endif
//write some information on the frame:
putText(m, "Face recognition demo (frontal)", POS_TITLE,
FONT, SCALE_TITLE, FONT_COLOR, THICKNESS_TITLE, LINE_TYPE);
putText(m, "http://blog.eyalarubas.com/category/opencv/", POS_LINK,
FONT, SCALE_LINK, FONT_COLOR, THICKNESS_LINK, LINE_TYPE);
putText(m, format("Frame: %d", c), cvPoint(10, m.rows - 105),
FONT, 2, FONT_COLOR, 1, LINE_TYPE);
putText(m, format("FPS: %d", 15), cvPoint(10, m.rows - 80),
FONT, 2, FONT_COLOR, 1, LINE_TYPE);
putText(m, format("Faces: %d", faces.size()), cvPoint(10, m.rows - 55),
FONT, 2, FONT_COLOR, 1, LINE_TYPE);
putText(m, format("Match: %s", has_match ? "True" : "False"), cvPoint(10, m.rows - 30),
FONT, 2, FONT_COLOR, 1, LINE_TYPE);
putText(m, format("Confidence: %f", has_match ? match_conf : 0), cvPoint(10, m.rows - 5),
FONT, 2, FONT_COLOR, 1, LINE_TYPE);
#ifdef SHOW_OUTPUT
imshow("Output",m);
if ('x' == waitKey(1)){
return 0;
}
#endif
#ifdef WRITE_OUTPUT
fw.write(m);
#endif
}
return 0;
}