This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
initial-guess.cpp
204 lines (167 loc) · 6.03 KB
/
initial-guess.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
199
200
201
202
203
204
#include "ht-api.h"
#include "ht-internal.h"
#include <algorithm>
using namespace std;
using namespace cv;
#include "flandmark_detector.h"
typedef enum {
fl_center = 0,
fl_left_eye_int = 1,
fl_right_eye_int = 2,
fl_mouth_left = 3,
fl_mouth_right = 4,
fl_left_eye_ext = 5,
fl_right_eye_ext = 6,
fl_nose = 7,
fl_count = 8
} fl_indices;
bool ht_fl_estimate(headtracker_t& ctx, Mat& frame, const Rect roi, Mat& rvec_, Mat& tvec_)
{
int bbox[4];
bbox[0] = roi.x;
bbox[1] = roi.y;
#if 0
bbox[2] = max(roi.width, roi.height) + roi.x;
bbox[3] = max(roi.width, roi.height) + roi.y;
if (bbox[0] + bbox[2] > frame.cols)
bbox[2] = bbox[3] = frame.cols - bbox[0];
if (bbox[1] + bbox[1] > frame.rows)
bbox[2] = bbox[3] = frame.rows - bbox[1];
#else
bbox[2] = roi.width + roi.x;
bbox[3] = roi.height + roi.y;
#endif
IplImage c_image = frame;
double landmarks[fl_count * 2];
if (flandmark_detect(&c_image, bbox, ctx.flandmark_model, landmarks))
return false;
Point2f left_eye_right = Point2f(
landmarks[2 * fl_left_eye_int],
landmarks[2 * fl_left_eye_int + 1]);
Point2f left_eye_left = Point2f(
landmarks[2 * fl_left_eye_ext],
landmarks[2 * fl_left_eye_ext + 1]);
Point2f right_eye_left = Point2f(
landmarks[2 * fl_right_eye_int],
landmarks[2 * fl_right_eye_int + 1]);
Point2f right_eye_right = Point2f(
landmarks[2 * fl_right_eye_ext],
landmarks[2 * fl_right_eye_ext + 1]);
Point2f nose = Point2f(landmarks[2 * fl_nose], landmarks[2 * fl_nose + 1]);
Point2f mouth_left = Point2f(
landmarks[2 * fl_mouth_left],
landmarks[2 * fl_mouth_left + 1]);
Point2f mouth_right = Point2f(
landmarks[2 * fl_mouth_right],
landmarks[2 * fl_mouth_right + 1]);
vector<Point2f> image_points(7);
vector<Point3f> object_points(7);
object_points[0] = Point3d(-0.03387, -0.03985, 0.14169);
object_points[1] = Point3d(0.03387, -0.03985, 0.14169);
object_points[2] = Point3d(-0.08307, -0.04124, 0.1327);
object_points[3] = Point3d(0.08307, -0.04124, 0.1327);
object_points[5] = Point3d(-0.04472, 0.08171, 0.15877);
object_points[6] = Point3d(0.04472, 0.08171, 0.15877);
object_points[4] = Point3d(0, 0.0335, 0.19386);
for (unsigned i = 0; i < object_points.size(); i++)
{
object_points[i].x *= 100;
object_points[i].y *= 100;
object_points[i].z *= 100;
}
image_points[0] = left_eye_right;
image_points[1] = right_eye_left;
image_points[2] = left_eye_left;
image_points[3] = right_eye_right;
image_points[5] = mouth_left;
image_points[6] = mouth_right;
image_points[4] = nose;
Mat intrinsics = Mat::eye(3, 3, CV_32FC1);
intrinsics.at<float> (0, 0) = ctx.focal_length_w;
intrinsics.at<float> (1, 1) = ctx.focal_length_h;
intrinsics.at<float> (0, 2) = ctx.grayscale.cols/2;
intrinsics.at<float> (1, 2) = ctx.grayscale.rows/2;
Mat dist_coeffs = Mat::zeros(5, 1, CV_32FC1);
Mat rvec = Mat::zeros(3, 1, CV_64FC1);
Mat tvec = Mat::zeros(3, 1, CV_64FC1);
for (int i = 0; i < 5; i++)
dist_coeffs.at<float>(i) = ctx.config.dist_coeffs[i];
rvec.at<double> (0, 0) = 1.0;
tvec.at<double> (0, 0) = 1.0;
tvec.at<double> (1, 0) = 1.0;
if (ctx.has_pose)
{
rvec = ctx.rvec.clone();
tvec = ctx.tvec.clone();
}
if (ctx.has_pose) {
if (!solvePnP(object_points, image_points, intrinsics, dist_coeffs, rvec, tvec, true, HT_PNP_TYPE))
return false;
} else {
if (!solvePnP(object_points, image_points, intrinsics, dist_coeffs, rvec, tvec, false, cv::SOLVEPNP_EPNP))
return false;
if (!solvePnP(object_points, image_points, intrinsics, dist_coeffs, rvec, tvec, true, HT_PNP_TYPE))
return false;
}
if (ctx.has_pose)
{
vector<Point2f> image_points2;
projectPoints(object_points, ctx.rvec, ctx.tvec, intrinsics, dist_coeffs, image_points2);
Scalar color(0, 0, 255);
float mult = ctx.color.cols / (float)ctx.grayscale.cols;
Scalar color2(255, 255, 255);
float error = 0;
static const float error_weights[7] = {
0.1,
0.1,
1,
1,
1.,
0.2,
0.2,
};
for (unsigned i = 0; i < image_points.size(); i++)
{
float tmp1 = image_points[i].x - image_points2[i].x;
float tmp2 = image_points[i].y - image_points2[i].y;
error += error_weights[i] * (tmp1 * tmp1 + tmp2 * tmp2);
}
error /= ctx.zoom_ratio;
const float max_error = 45.;
if (error > max_error)
return false;
if (ctx.config.debug)
{
for (unsigned i = 0; i < image_points.size(); i++)
{
line(ctx.color, image_points[i] * mult , image_points2[i] * mult, color, 7);
circle(ctx.color, image_points[i] * mult, 5, color2, -1);
}
fprintf(stderr, "flandmark error %f\n", error);
fflush(stderr);
}
}
rvec_ = rvec.clone();
tvec_ = tvec.clone();
return true;
}
bool ht_initial_guess(headtracker_t& ctx, Mat& frame, Mat& rvec_, Mat& tvec_) {
int ticks = ht_tickcount();
if (ctx.ticks_last_classification / ctx.config.classification_delay == ticks / ctx.config.classification_delay)
return false;
ctx.ticks_last_classification = ticks;
Rect face_rect;
if (!ht_classify(ctx.head_classifier, ctx.grayscale, face_rect))
return false;
if (face_rect.x < 0)
face_rect.x = 0;
if (face_rect.y < 0)
face_rect.y = 0;
if (face_rect.width + face_rect.x > ctx.grayscale.cols)
face_rect.width = ctx.grayscale.cols - face_rect.x;
if (face_rect.height + face_rect.y > ctx.grayscale.rows)
face_rect.height = ctx.grayscale.rows - face_rect.y;
if (face_rect.width < 10 && face_rect.height < 10)
return false;
return ht_fl_estimate(ctx, frame, face_rect, rvec_, tvec_);
}