-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
155 lines (135 loc) · 4.25 KB
/
Copy pathmain.cpp
File metadata and controls
155 lines (135 loc) · 4.25 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
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <scene.hpp>
#include <face.hpp>
#include <genderclassifier.hpp>
#include <raceclassifier.hpp>
#include <ageclassifier.hpp>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
string getFileName(const string& s){
char sep = '/';
#ifdef _WIN32
sep = '\\';
#endif
size_t i = s.rfind(sep, s.length());
if(i != string::npos){
return (s.substr(i+1, s.length()-i));
}
return("");
}
string classifyAll(vector<Face> *faces, string parameters, string file_name) {
string json = "";
double confidence;
for (vector<Face>::iterator i=faces->begin(); i != faces->end(); i++) {
json += "{ ";
json += "\"nome_file\": \"" + file_name + "\", ";
Rect position = (*i).getPosition();
json += "\"position\": [{ x:" + to_string(position.x) + ", y:" + to_string(position.y) + " }, " + "{ x:" + to_string(position.br().x) + ", y:" + to_string(position.br().y) + " }]";;
if (parameters.find("-g") != std::string::npos) {
json += ", \"gender\": [";
GenderClassifier g;
g.classify((*i), confidence);
((*i).getGender()==Male) ? json+="\"male\", " : json+="\"female\", ";
json += to_string(confidence) + "]";
}
if (parameters.find("-r") != std::string::npos) {
json += ", \"race\": [";
RaceClassifier r;
r.classify((*i), confidence);
((*i).getRace()==Black) ? json+="\"black\", " : ((*i).getRace()==White) ? json+="\"white\", " : json +="\"asian\", ";
json += to_string(confidence) + "]";
}
if (parameters.find("-a") != std::string::npos) {
json += ", \"age\": [";
AgeClassifier a;
a.classify((*i), confidence);
switch((*i).getAge()) {
case Kid: json+="\"kid\", "; break;
case Teen: json+="\"teen\", "; break;
case Young: json+="\"young\", "; break;
case Mature: json+="\"mature\", "; break;
case Old: json+="\"old\", "; break;
}
json += to_string(confidence) + "]";
}
json+=" } ";
}
//
return json;
}
int main(int argc, char *argv[])
{
string json; // main parameter?
//
if (argc < 3) {
cerr << "Error: do you miss some arguments?" << endl;
return -1;
}
// Interpret parameters
string input = argv[1];
if (input != "-vid" && input != "-img" && input != "-cam") {
cerr << "Error: " << input << ". Input format incorrect! Check the documentation and try again." << endl;
return -1;
}
string parameters = "";
for (int i=3; i<argc; i++) {
string request = argv[i];
if (request != "-g" && request != "-r" && request != "-a" &&
request != "-gh" && request != "-ah") {
cerr << "Error: " << request << ". Recognition parameter is wrong. Check the documentation and try again." << endl;
return -1;
}
else {
parameters += request;
}
}
string nome_file = getFileName(argv[2]);
// Biometrics recognition
Mat frame;
Scene scene;
vector<Face> *faces;
if (input == "-img") {
frame = imread(argv[2], CV_LOAD_IMAGE_COLOR);
//Mat original = frame.clone();
scene.detect(frame);
faces = scene.getFaces();
json = classifyAll(faces, parameters, nome_file);
// DELETE! IT'S JUST FOR TESTING
cout << "These are the json objects: \n" << json << endl;
}
if (input == "-vid") {
// Video recognition code
// work in progress
}
else
if (input == "-cam") {
// cast string to int
int deviceId = atoi(argv[2]);
VideoCapture cap(deviceId);
if(!cap.isOpened()) {
cerr << "Capture Device ID " << deviceId << "cannot be opened." << endl;
return -1;
}
cap.set(CAP_PROP_FRAME_WIDTH, 400);
cap.set(CAP_PROP_FRAME_WIDTH, 400);
// Holds the current frame from the Video device:
for(;;) {
cap >> frame;
Mat original = frame.clone();
scene.detect(original);
faces = scene.getFaces();
json = classifyAll(faces, parameters, "");
scene.draw(parameters);
imshow("face_recognizer", original);
char key = (char) waitKey(10);
if(key == 27)
break;
}
}
return 0;
}