-
Notifications
You must be signed in to change notification settings - Fork 1
/
FaceDetector.cpp
executable file
·198 lines (182 loc) · 6.09 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
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
#include <algorithm>
//#include "omp.h"
#include "FaceDetector.h"
#define inw 640
#define inh 480
Detector::Detector():
_nms(0.4),
_threshold(0.95)
{
}
Detector::~Detector()
{
}
void Detector::Detect(std::vector<bbox>& boxes, std::vector<std::vector<float>> results)
{
std::vector<box> anchor;
//create_anchor_retinaface(anchor, inw, inh);
create_anchor(anchor, inw, inh);
std::vector<bbox > total_box;
std::vector<float> loc = results[0];
std::vector<float> landms = results[1];
std::vector<float> score = results[2];
int indexLoc=0;
int indexLm=0;
int indexsco=0;
// #pragma omp parallel for num_threads(2)
for (int i = 0; i < anchor.size(); ++i)
{
if (score[indexsco+1] > _threshold)
{
box tmp = anchor[i];
box tmp1;
bbox result;
// loc and conf
tmp1.cx = tmp.cx + loc[indexLoc] * 0.1 * tmp.sx;
tmp1.cy = tmp.cy + loc[indexLoc+1] * 0.1 * tmp.sy;
tmp1.sx = tmp.sx * exp(loc[indexLoc+2] * 0.2);
tmp1.sy = tmp.sy * exp(loc[indexLoc+3] * 0.2);
result.x1 = (tmp1.cx - tmp1.sx/2) * inw;
if (result.x1<0)
result.x1 = 0;
result.y1 = (tmp1.cy - tmp1.sy/2) * inh;
if (result.y1<0)
result.y1 = 0;
result.x2 = (tmp1.cx + tmp1.sx/2) * inw;
if (result.x2>inw)
result.x2 = inw;
result.y2 = (tmp1.cy + tmp1.sy/2)* inh;
if (result.y2>inh)
result.y2 = inh;
result.s = score[indexsco+1];
// landmark
for (int j = 0; j < 5; ++j)
{
result.point[j]._x =( tmp.cx + landms[indexLm + (j<<1)] * 0.1 * tmp.sx ) * inw;
result.point[j]._y =( tmp.cy + landms[indexLm + (j<<1) + 1] * 0.1 * tmp.sy ) * inh;
}
total_box.push_back(result);
}
indexLoc += 4;
indexsco += 2;
indexLm += 10;
}
std::sort(total_box.begin(), total_box.end(), cmp);
nms(total_box, _nms);
// printf("Total box %d\n", indexsco);
for (int j = 0; j < total_box.size(); ++j)
{
boxes.push_back(total_box[j]);
}
}
inline bool Detector::cmp(bbox a, bbox b) {
if (a.s > b.s)
return true;
return false;
}
void Detector::create_anchor(std::vector<box> &anchor, int w, int h)
{
// anchor.reserve(num_boxes);
anchor.clear();
std::vector<std::vector<int> > feature_map(4), min_sizes(4);
float steps[] = {8, 16, 32, 64};
for (int i = 0; i < feature_map.size(); ++i) {
feature_map[i].push_back(ceil(h/steps[i]));
feature_map[i].push_back(ceil(w/steps[i]));
}
std::vector<int> minsize1 = {10, 16, 24};
min_sizes[0] = minsize1;
std::vector<int> minsize2 = {32, 48};
min_sizes[1] = minsize2;
std::vector<int> minsize3 = {64, 96};
min_sizes[2] = minsize3;
std::vector<int> minsize4 = {128, 192, 256};
min_sizes[3] = minsize4;
for (int k = 0; k < feature_map.size(); ++k)
{
std::vector<int> min_size = min_sizes[k];
for (int i = 0; i < feature_map[k][0]; ++i)
{
for (int j = 0; j < feature_map[k][1]; ++j)
{
for (int l = 0; l < min_size.size(); ++l)
{
float s_kx = min_size[l]*1.0/w;
float s_ky = min_size[l]*1.0/h;
float cx = (j + 0.5) * steps[k]/w;
float cy = (i + 0.5) * steps[k]/h;
box axil = {cx, cy, s_kx, s_ky};
anchor.push_back(axil);
}
}
}
}
}
void Detector::create_anchor_retinaface(std::vector<box> &anchor, int w, int h)
{
// anchor.reserve(num_boxes);
anchor.clear();
std::vector<std::vector<int> > feature_map(3), min_sizes(3);
float steps[] = {8, 16, 32};
for (int i = 0; i < feature_map.size(); ++i) {
feature_map[i].push_back(ceil(h/steps[i]));
feature_map[i].push_back(ceil(w/steps[i]));
}
std::vector<int> minsize1 = {10, 20};
min_sizes[0] = minsize1;
std::vector<int> minsize2 = {32, 64};
min_sizes[1] = minsize2;
std::vector<int> minsize3 = {128, 256};
min_sizes[2] = minsize3;
for (int k = 0; k < feature_map.size(); ++k)
{
std::vector<int> min_size = min_sizes[k];
for (int i = 0; i < feature_map[k][0]; ++i)
{
for (int j = 0; j < feature_map[k][1]; ++j)
{
for (int l = 0; l < min_size.size(); ++l)
{
float s_kx = min_size[l]*1.0/w;
float s_ky = min_size[l]*1.0/h;
float cx = (j + 0.5) * steps[k]/w;
float cy = (i + 0.5) * steps[k]/h;
box axil = {cx, cy, s_kx, s_ky};
anchor.push_back(axil);
}
}
}
}
}
void Detector::nms(std::vector<bbox> &input_boxes, float NMS_THRESH)
{
std::vector<float>vArea(input_boxes.size());
for (int i = 0; i < int(input_boxes.size()); ++i)
{
vArea[i] = (input_boxes.at(i).x2 - input_boxes.at(i).x1 + 1)
* (input_boxes.at(i).y2 - input_boxes.at(i).y1 + 1);
}
for (int i = 0; i < int(input_boxes.size()); ++i)
{
for (int j = i + 1; j < int(input_boxes.size());)
{
float xx1 = std::max(input_boxes[i].x1, input_boxes[j].x1);
float yy1 = std::max(input_boxes[i].y1, input_boxes[j].y1);
float xx2 = std::min(input_boxes[i].x2, input_boxes[j].x2);
float yy2 = std::min(input_boxes[i].y2, input_boxes[j].y2);
float w = std::max(float(0), xx2 - xx1 + 1);
float h = std::max(float(0), yy2 - yy1 + 1);
float inter = w * h;
float ovr = inter / (vArea[i] + vArea[j] - inter);
if (ovr >= NMS_THRESH)
{
input_boxes.erase(input_boxes.begin() + j);
vArea.erase(vArea.begin() + j);
}
else
{
j++;
}
}
}
}