-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathface-tracker-dlib.cpp
More file actions
211 lines (185 loc) · 5.35 KB
/
face-tracker-dlib.cpp
File metadata and controls
211 lines (185 loc) · 5.35 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <obs-module.h>
#include <util/platform.h>
#include <util/threading.h>
#include "plugin-macros.generated.h"
#include "texture-object.h"
#include "face-tracker-dlib.h"
#include <dlib/image_processing/scan_fhog_pyramid.h>
#include <dlib/image_processing/correlation_tracker.h>
#include <dlib/image_processing.h>
struct face_tracker_dlib_private_s
{
std::shared_ptr<texture_object> tex;
rect_s rect;
dlib::correlation_tracker *tracker;
int tracker_nc, tracker_nr;
dlib::shape_predictor sp;
dlib::full_object_detection shape;
float last_scale;
float score0;
float pslr_max, pslr_min;
bool need_restart;
uint64_t last_ns;
float scale_orig;
int n_track;
rectf_s upsize;
char *landmark_detection_data;
bool landmark_detection_data_updated;
bool sp_available = false;
face_tracker_dlib_private_s()
{
tracker = NULL;
need_restart = false;
tex = NULL;
rect.score = 0.0f;
landmark_detection_data = NULL;
landmark_detection_data_updated = false;
}
};
face_tracker_dlib::face_tracker_dlib()
{
p = new face_tracker_dlib_private_s;
}
face_tracker_dlib::~face_tracker_dlib()
{
bfree(p->landmark_detection_data);
if (p->tracker) delete p->tracker;
delete p;
}
void face_tracker_dlib::set_texture(std::shared_ptr<texture_object> &tex)
{
p->tex = tex;
p->n_track = 0;
}
void face_tracker_dlib::set_position(const rect_s &rect)
{
if (!p->tex) {
blog(LOG_ERROR, "face_tracker_dlib::set_position: texture was not set. rect=(%d %d %d %d %f)",
rect.x0, rect.y0, rect.x1, rect.y1, rect.score);
return;
}
p->rect.x0 = rect.x0 / p->tex->scale;
p->rect.y0 = rect.y0 / p->tex->scale;
p->rect.x1 = rect.x1 / p->tex->scale;
p->rect.y1 = rect.y1 / p->tex->scale;
p->rect.score = 1.0f;
p->need_restart = true;
p->n_track = 0;
}
void face_tracker_dlib::set_upsize_info(const rectf_s &upsize)
{
p->upsize = upsize;
}
void face_tracker_dlib::set_landmark_detection(const char *data_file_path)
{
if (p->landmark_detection_data && data_file_path && strcmp(p->landmark_detection_data, data_file_path) == 0)
return;
bfree(p->landmark_detection_data);
p->landmark_detection_data = NULL;
if (data_file_path) {
p->landmark_detection_data = bstrdup(data_file_path);
p->landmark_detection_data_updated = true;
}
}
template <typename Tx, typename Ta>
inline Tx internal_division(Tx x0, Tx x1, Ta a0, Ta a1)
{
return (x0 * a1 + x1 * a0) / (a0 + a1);
}
void face_tracker_dlib::track_main()
{
if (!p->tex)
return;
uint64_t ns = os_gettime_ns();
if (p->need_restart) {
if (!p->tracker)
p->tracker = new dlib::correlation_tracker();
dlib::matrix<dlib::rgb_pixel> img;
if (!p->tex->get_dlib_rgb_image(img))
return;
dlib::rectangle r (p->rect.x0, p->rect.y0, p->rect.x1, p->rect.y1);
p->tracker->start_track(img, r);
p->tracker_nc = img.nc();
p->tracker_nr = img.nr();
p->score0 = p->rect.score;
p->need_restart = false;
p->pslr_max = 0.0f;
p->pslr_min = 1e9f;
p->scale_orig = p->tex->scale;
p->shape = dlib::full_object_detection();
}
else if (p->tex->scale != p->scale_orig) {
p->rect.score = 0.0f;
}
else {
dlib::matrix<dlib::rgb_pixel> img;
if (!p->tex->get_dlib_rgb_image(img))
return;
if (img.nc() != p->tracker_nc || img.nr() != p->tracker_nr) {
blog(LOG_ERROR, "face_tracker_dlib::track_main: cannot run correlation-tracker with different image size %dx%d, expected %dx%d",
(int)img.nc(), (int)img.nr(),
p->tracker_nc, p->tracker_nr );
p->rect.score = 0;
p->n_track += 1; // to return score=0
return;
}
float s = p->tracker->update(img);
if (s>p->pslr_max) p->pslr_max = s;
if (s<p->pslr_min) p->pslr_min = s;
dlib::rectangle r = p->tracker->get_position();
p->rect.x0 = r.left() * p->tex->scale;
p->rect.y0 = r.top() * p->tex->scale;
p->rect.x1 = r.right() * p->tex->scale;
p->rect.y1 = r.bottom() * p->tex->scale;
s = p->pslr_max / p->pslr_min * ((ns-p->last_ns)*1e-9f);
p->rect.score = (p->rect.score /*+ 0.0f*s */) / (1.0f + s);
p->n_track += 1;
if (p->landmark_detection_data) {
if (p->landmark_detection_data_updated) {
p->landmark_detection_data_updated = false;
blog(LOG_INFO, "loading file %s", p->landmark_detection_data);
try {
p->sp_available = false;
dlib::deserialize(p->landmark_detection_data) >> p->sp;
p->sp_available = true;
} catch (...) {
blog(LOG_ERROR, "Failed to load file %s", p->landmark_detection_data);
}
}
dlib::rectangle r_face (
internal_division(r.left(), r.right(), p->upsize.x0, p->upsize.x1 + 1.0f),
internal_division(r.top(), r.bottom(), p->upsize.y0, p->upsize.y1 + 1.0f),
internal_division(r.left(), r.right(), p->upsize.x0 + 1.0f, p->upsize.x1),
internal_division(r.top(), r.bottom(), p->upsize.y0 + 1.0f, p->upsize.y1) );
if (p->sp_available)
p->shape = p->sp(img, r_face);
p->last_scale = p->tex->scale;
}
}
p->last_ns = ns;
p->tex.reset();
}
bool face_tracker_dlib::get_face(struct rect_s &rect)
{
if (p->n_track>0) {
rect = p->rect;
return true;
}
else
return false;
}
bool face_tracker_dlib::get_landmark(std::vector<pointf_s> &results)
{
if (p->shape.num_parts() > 0) {
const auto &shape = p->shape;
results.resize(shape.num_parts());
for (unsigned long i=0; i<shape.num_parts(); i++) {
const dlib::point pnt =shape.part(i);
results[i].x = (float)pnt.x() * p->last_scale;
results[i].y = (float)pnt.y() * p->last_scale;
}
return true;
}
else
return false;
}