This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
153 lines (125 loc) · 4.26 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
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
#define CV_NO_BACKWARD_COMPATIBILITY
#include <OpenCV/OpenCV.h>
#include <fstream>
#include <iostream>
using namespace std;
using namespace cv;
void detectAndDraw(Mat &img, CascadeClassifier &cascade,
CascadeClassifier &nestedCascade, ofstream &outfile);
int main(int argc, const char **argv) {
Mat image;
const string amountOpt = "--amount=";
size_t amountOptLen = amountOpt.length();
string inputName;
CascadeClassifier cascade, nestedCascade;
int amount = 0;
for (int i = 1; i < argc; i++) {
if (amountOpt.compare(0, amountOptLen, argv[i], amountOptLen) == 0) {
if (!sscanf(argv[i] + amountOpt.length(), "%d", &amount) || amount < 0) {
amount = 0;
}
} else if (argv[i][0] == '-') {
cerr << "WARNING: Unknown option %s" << argv[i] << endl;
} else {
inputName.assign(argv[i]);
}
}
if (!cascade.load("haarcascade_frontalface_alt_tree.xml")) {
cerr << "ERROR: Could not load classifier cascade" << endl;
return -1;
}
if (!nestedCascade.load("haarcascade_mcs_righteye.xml")) {
cerr << "WARNING: Could not load classifier cascade for nested objects"
<< endl;
return -1;
}
ofstream outfile;
outfile.open("test.csv");
outfile << "name,left_eye_x,left_eye_y,right_eye_x,right_eye_y,face_x,face_y,"
"face_width,face_height"
<< endl;
for (int i = 1; i <= amount; i++) {
stringstream out;
out << inputName << i << ".jpg";
outfile << out.str() << ",";
image = imread(out.str(), 1);
if (!image.empty()) {
detectAndDraw(image, cascade, nestedCascade, outfile);
}
outfile << endl;
}
outfile.close();
return 0;
}
void detectAndDraw(Mat &img, CascadeClassifier &cascade,
CascadeClassifier &nestedCascade,
ofstream &outfile) //, ofstream& meshfile)
{
bool eyesFound = false, stop_searching = false;
double t = 0;
vector<Rect> faces;
Mat gray, smallImg(cvRound(img.rows), cvRound(img.cols), CV_8UC1);
cvtColor(img, gray, CV_BGR2GRAY);
resize(gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR);
equalizeHist(smallImg, smallImg);
t = (double)cvGetTickCount();
cascade.detectMultiScale(smallImg, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE,
Size(30, 30));
for (vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++) {
Mat smallImgROI;
vector<Rect> nestedObjects;
vector<Point> centers;
Point center, centerLeft, centerRight;
smallImgROI = smallImg(*r);
nestedCascade.detectMultiScale(smallImgROI, nestedObjects, 1.1, 2,
0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (vector<Rect>::const_iterator nr = nestedObjects.begin();
nr != nestedObjects.end(); nr++) {
center.x = cvRound((r->x + nr->x + nr->width * 0.5));
center.y = cvRound((r->y + nr->y + nr->height * 0.5));
if (center.y < (r->y + r->height * 0.5)) {
centers.push_back(center);
}
eyesFound = true;
}
if (eyesFound) {
int lefters = 0, righters = 0, xLeftSum = 0, xRightSum = 0, yLeftSum = 0,
yRightSum = 0;
for (vector<Point>::const_iterator cntr = centers.begin();
cntr != centers.end(); cntr++) {
if (cntr->x < (r->x + r->width * 0.5)) {
lefters++;
xLeftSum += cntr->x;
yLeftSum += cntr->y;
} else {
righters++;
xRightSum += cntr->x;
yRightSum += cntr->y;
}
}
if (lefters > 0 && righters > 0) {
centerLeft.x = cvRound(xLeftSum / lefters);
centerLeft.y = cvRound(yLeftSum / lefters);
centerRight.x = cvRound(xRightSum / righters);
centerRight.y = cvRound(yRightSum / righters);
outfile << centerLeft.x << ",";
outfile << centerLeft.y << ",";
outfile << centerRight.x << ",";
outfile << centerRight.y << ",";
outfile << r->x << ",";
outfile << r->y << ",";
outfile << r->width << ",";
outfile << r->height;
stop_searching = true;
}
}
centers.clear();
eyesFound = false;
if (stop_searching) {
break;
}
}
t = (double)cvGetTickCount() - t;
printf("detection time = %g ms\n",
t / ((double)cvGetTickFrequency() * 1000.));
}