Skip to content

Commit 4269aa4

Browse files
authored
Merge pull request #7 from AIRLegend/dev
Dev
2 parents e43bb87 + a724a07 commit 4269aa4

30 files changed

+818
-541
lines changed

AITracker/AITracker.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
<ConformanceMode>true</ConformanceMode>
190190
<AdditionalIncludeDirectories>$(SolutionDir)Dependencies\OpenCV\include\;$(SolutionDir)Dependencies\onnxruntime\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
191191
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
192+
<Optimization>Disabled</Optimization>
192193
</ClCompile>
193194
<Link>
194195
<SubSystem>Console</SubSystem>

AITracker/src/PositionSolver.cpp

Lines changed: 50 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
#include "PositionSolver.h"
22

33

4-
PositionSolver::PositionSolver(int width, int height,
5-
float prior_pitch, float prior_yaw, float prior_distance):
6-
contour_indices{ 0,1,8,15,16,27,28,29,30,31,32,33,34,35,36,39,42,45 }
4+
PositionSolver::PositionSolver(int width, int height,
5+
float prior_pitch, float prior_yaw, float prior_distance) :
6+
contour_indices{ 0,1,8,15,16,27,28,29,30,31,32,33,34,35,36,39,42,45 },
7+
landmark_points_buffer(18, 1, CV_32FC2),
8+
rv({ 0, 0, 0 }),
9+
tv({ 0, 0, 0 })
710
{
811
this->prior_pitch = (1.1f * (prior_pitch + 90.f) / 180.f) - (double)2.5f;
9-
this->prior_distance = prior_distance * -2. ;
10-
this->prior_yaw = (1.84f*(prior_yaw + 90.f)/180.f) - (double)3.14f;
12+
this->prior_distance = prior_distance * -2.;
13+
this->prior_yaw = (1.84f * (prior_yaw + 90.f) / 180.f) - (double)3.14f;
14+
15+
this->rv[0] = this->prior_pitch;
16+
this->rv[1] = this->prior_yaw;
17+
this->tv[2] = this->prior_distance;
1118

1219
//std::cout << "PRIORS CALCULATED: \nPITCH: " <<this->prior_pitch << " YAW: " << this->prior_yaw << " DISTANCE: " << this->prior_distance;
1320

1421
mat3dcontour = (cv::Mat_<double>(18, 3) <<
1522
0.45517698, -0.30089578, 0.76442945,
1623
0.44899884, -0.16699584, 0.765143,
17-
0., 0.621079, 0.28729478,
24+
0., 0.621079, 0.28729478,
1825
-0.44899884, -0.16699584, 0.765143,
1926
-0.45517698, -0.30089578, 0.76442945,
2027
0., -0.2933326, 0.1375821,
@@ -32,124 +39,39 @@ PositionSolver::PositionSolver(int width, int height,
3239
-0.31590518, -0.2983375, 0.2851074
3340
);
3441

35-
/*mat3dface = (cv::Mat_<double>(70, 3) <<
36-
0.4551769692672, 0.300895790030204, -0.764429433974752,
37-
0.448998827123556, 0.166995837790733, -0.765143004071253,
38-
0.437431554952677, 0.022655479179981, -0.739267175112735,
39-
0.415033422928434, -0.088941454648772, -0.747947437846473,
40-
0.389123587370091, -0.232380029794684, -0.704788385327458,
41-
0.334630113904382, -0.361265387599081, -0.615587579236862,
42-
0.263725112132858, -0.460009725616771, -0.491479221041573,
43-
0.16241621322721, -0.558037146073869, -0.339445180872282,
44-
0., -0.621079019321682, -0.287294770748887,
45-
-0.16241621322721, -0.558037146073869, -0.339445180872282,
46-
-0.263725112132858, -0.460009725616771, -0.491479221041573,
47-
-0.334630113904382, -0.361265387599081, -0.615587579236862,
48-
-0.389123587370091, -0.232380029794684, -0.704788385327458,
49-
-0.415033422928434, -0.088941454648772, -0.747947437846473,
50-
-0.437431554952677, 0.022655479179981, -0.739267175112735,
51-
-0.448998827123556, 0.166995837790733, -0.765143004071253,
52-
-0.4551769692672, 0.300895790030204, -0.764429433974752,
53-
0.385529968662985, 0.402800553948697, -0.310031082540741,
54-
0.322196658344302, 0.464439136821772, -0.250558059367669,
55-
0.25409760441282, 0.46420381416882, -0.208177722146526,
56-
0.186875436782135, 0.44706071961879, -0.145299823706503,
57-
0.120880983543622, 0.423566314072968, -0.110757158774771,
58-
-0.120880983543622, 0.423566314072968, -0.110757158774771,
59-
-0.186875436782135, 0.44706071961879, -0.145299823706503,
60-
-0.25409760441282, 0.46420381416882, -0.208177722146526,
61-
-0.322196658344302, 0.464439136821772, -0.250558059367669,
62-
-0.385529968662985, 0.402800553948697, -0.310031082540741,
63-
0., 0.293332603215811, -0.137582088779393,
64-
0., 0.194828701837823, -0.069158109325951,
65-
0., 0.103844017393155, -0.009151819844964,
66-
0., 0., 0.,
67-
0.080626352317973, -0.041276068128093, -0.134161035564826,
68-
0.046439347377934, -0.057675223874769, -0.102990627164664,
69-
0., -0.068753126205604, -0.090545348482397,
70-
-0.046439347377934, -0.057675223874769, -0.102990627164664,
71-
-0.080626352317973, -0.041276068128093, -0.134161035564826,
72-
0.315905195966084, 0.298337502555443, -0.285107407636464,
73-
0.275252345439353, 0.312721904921771, -0.244558251170671,
74-
0.176394511553111, 0.311907184376107, -0.219205360345231,
75-
0.131229723798772, 0.284447361805627, -0.234239149487417,
76-
0.184124948330084, 0.260179585304867, -0.226590776513707,
77-
0.279433549294448, 0.267363071770222, -0.248441437111633,
78-
-0.131229723798772, 0.284447361805627, -0.234239149487417,
79-
-0.176394511553111, 0.311907184376107, -0.219205360345231,
80-
-0.275252345439353, 0.312721904921771, -0.244558251170671,
81-
-0.315905195966084, 0.298337502555443, -0.285107407636464,
82-
-0.279433549294448, 0.267363071770222, -0.248441437111633,
83-
-0.184124948330084, 0.260179585304867, -0.226590776513707,
84-
0.121155252430729, -0.208988660580347, -0.160606287940521,
85-
0.041356305910044, -0.194484199722098, -0.096159882202821,
86-
0., -0.205180167345702, -0.083299217789729,
87-
-0.041356305910044, -0.194484199722098, -0.096159882202821,
88-
-0.121155252430729, -0.208988660580347, -0.160606287940521,
89-
-0.132325402795928, -0.290857984604968, -0.187067868218105,
90-
-0.064137791831655, -0.325377847425684, -0.158924039726607,
91-
0., -0.343742581679188, -0.113925986025684,
92-
0.064137791831655, -0.325377847425684, -0.158924039726607,
93-
0.132325402795928, -0.290857984604968, -0.187067868218105,
94-
0.181481567104525, -0.243239316141725, -0.231284988892766,
95-
0.083999507750469, -0.239717753728704, -0.155256465640701,
96-
0., -0.256058040176369, -0.0950619498899,
97-
-0.083999507750469, -0.239717753728704, -0.155256465640701,
98-
-0.181481567104525, -0.243239316141725, -0.231284988892766,
99-
-0.074036069749345, -0.250689938345682, -0.177346470406188,
100-
0., -0.264945854681568, -0.112349967428413,
101-
0.074036069749345, -0.250689938345682, -0.177346470406188,
102-
0.257990002632141, 0.276080012321472, -0.219998998939991,
103-
-0.257990002632141, 0.276080012321472, -0.219998998939991,
104-
0.257990002632141, 0.276080012321472, -0.324570998549461,
105-
-0.257990002632141, 0.276080012321472, -0.324570998549461
106-
);*/
107-
10842
camera_matrix = (cv::Mat_<double>(3, 3) <<
10943
height, 0, height / 2,
11044
0, height, width / 2,
11145
0, 0, 1
112-
);
46+
);
11347

11448
camera_distortion = (cv::Mat_<double>(4, 1) << 0, 0, 0, 0);
11549
}
11650

11751
void PositionSolver::solve_rotation(FaceData* face_data)
11852
{
119-
std::vector<cv::Point2f> landmarkPoints;
120-
121-
for (int i : this->contour_indices)
53+
int contour_idx = 0;
54+
for (int j = 0; j < 2; j++)
12255
{
123-
landmarkPoints.push_back(
124-
cv::Point(face_data->landmark_coords[2 * i], face_data->landmark_coords[2 * i + 1])
125-
);
126-
}
56+
for (int i = 0; i < NB_CONTOUR_POINTS; i++)
57+
{
58+
contour_idx = contour_indices[i];
59+
landmark_points_buffer.at<float>(i, j) = (int)face_data->landmark_coords[2 * contour_idx + j];
12760

128-
std::vector<double> rv({prior_pitch, prior_yaw,0 }), tv({ 0, 0, prior_distance });
129-
cv::Mat rvec(rv), tvec(tv);
61+
}
62+
}
13063

131-
cv::Mat ip(landmarkPoints);
64+
cv::Mat rvec(rv, true), tvec(tv, true);
13265

133-
13466
solvePnP(mat3dcontour,
135-
ip,
136-
this->camera_matrix,
137-
this->camera_distortion,
138-
rvec,
139-
tvec,
140-
true, //extrinsic guess
141-
cv::SOLVEPNP_ITERATIVE
142-
);
143-
144-
/*cv::solvePnPRefineLM(
145-
mat3dcontour,
146-
ip,
67+
landmark_points_buffer,
14768
this->camera_matrix,
14869
this->camera_distortion,
14970
rvec,
150-
tvec
151-
);*/
152-
71+
tvec,
72+
true, //extrinsic guess
73+
cv::SOLVEPNP_ITERATIVE
74+
);
15375

15476
get_euler(rvec, tvec);
15577

@@ -160,28 +82,43 @@ void PositionSolver::solve_rotation(FaceData* face_data)
16082
face_data->translation[i] = tvec.at<double>(i, 0);
16183
}
16284

163-
}
85+
}
86+
87+
void PositionSolver::set_prior_pitch(float new_pitch)
88+
{
89+
this->prior_pitch = new_pitch;
90+
this->rv[0] = this->prior_pitch;
91+
}
92+
93+
void PositionSolver::set_prior_yaw(float new_yaw)
94+
{
95+
this->prior_yaw = new_yaw;
96+
this->rv[1] = this->prior_yaw;
97+
}
98+
99+
void PositionSolver::set_prior_distance(float new_distance)
100+
{
101+
this->prior_distance = new_distance;
102+
this->tv[2] = this->prior_distance;
103+
}
164104

165105

166106
void PositionSolver::get_euler(cv::Mat& rvec, cv::Mat& tvec)
167107
{
168108
cv::Mat rotM(3, 3, CV_64FC1);
169109
cv::Rodrigues(rvec, rotM);
170110

171-
172-
173111
cv::Mat concated(3, 4, CV_64FC1);
174112
cv::hconcat(rotM, tvec, concated);
175113

176-
177114
cv::decomposeProjectionMatrix(
178115
concated,
179116
cv::Mat(3, 3, CV_64FC1),
180117
cv::Mat(3, 3, CV_64FC1),
181118
cv::Mat(4, 1, CV_64FC1),
182-
cv::Mat(3, 3, CV_64FC1),
183-
cv::Mat(3, 3, CV_64FC1),
184-
cv::Mat(3, 3, CV_64FC1),
119+
cv::noArray(),
120+
cv::noArray(),
121+
cv::noArray(),
185122
rvec
186123
);
187124

AITracker/src/PositionSolver.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "opencv.hpp"
55
#include "data.h"
66

7-
87
/**
98
This class is responsible of mapping 2D facial landmarks to its
109
corresponding 3D world coordinates for pose estimation (Position and rotation).
@@ -14,7 +13,7 @@ class PositionSolver
1413

1514
public:
1615

17-
double prior_pitch, prior_yaw, prior_distance;
16+
double prior_pitch = -2.f, prior_yaw = -2.f, prior_distance = -1.f;
1817

1918
PositionSolver(
2019
int im_width,
@@ -28,16 +27,27 @@ class PositionSolver
2827
@param face_data FaceData instance with 2D landmark coordinates detected
2928
*/
3029
void solve_rotation(FaceData* face_data);
30+
void set_prior_pitch(float new_pitch);
31+
void set_prior_yaw(float new_yaw);
32+
void set_prior_distance(float new_distance);
3133

3234
private:
35+
static const int NB_CONTOUR_POINTS = 18;
36+
3337
cv::Mat mat3dface;
3438
cv::Mat mat3dcontour;
35-
int contour_indices[18];
39+
int contour_indices[NB_CONTOUR_POINTS]; // Facial landmarks that interest us
40+
41+
//Buffer so we dont have to allocate a list on every solve_rotation call.
42+
cv::Mat landmark_points_buffer;
43+
//Prior rotations and translation as a vector so we dont have to allocate them.
44+
std::vector<double> rv, tv;
3645

3746
int width, height;
3847

3948
cv::Mat camera_matrix, camera_distortion;
4049

50+
4151
/**
4252
Gets euler angles from rotation matrix.
4353
*/

AITracker/src/imageprocessor.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void ImageProcessor::normalize(cv::Mat& image)
1717
}
1818

1919

20-
void ImageProcessor::cvt_format(float* from, float* dest, int dim)
20+
/*void ImageProcessor::cvt_format(float* from, float* dest, int dim_x, int dim_y)
2121
{
2222
for (int channel = 1; channel < 4; channel++)
2323
{
@@ -31,7 +31,7 @@ void ImageProcessor::cvt_format(float* from, float* dest, int dim)
3131
}
3232
}
3333
34-
void ImageProcessor::transpose(float* from, float* dest, int dim)
34+
void ImageProcessor::transpose(float* from, float* dest, int dim_x, int dim_y)
3535
{
3636
int stride = 224 * 224;
3737
@@ -42,6 +42,34 @@ void ImageProcessor::transpose(float* from, float* dest, int dim)
4242
dest[i + stride*c] = from[c + i*3];
4343
}
4444
}
45+
}*/
46+
47+
48+
void ImageProcessor::cvt_format(float* from, float* dest, int dim_x, int dim_y)
49+
{
50+
for (int channel = 1; channel < 4; channel++)
51+
{
52+
for (int row = 0; row < dim_x; row++)
53+
{
54+
for (int col = 0; col < dim_y; col++)
55+
{
56+
dest[((channel - 1) * dim_x * dim_y) + (dim_y * col + row)] = from[3 * (dim_y * col + row)];
57+
}
58+
}
59+
}
60+
}
61+
62+
void ImageProcessor::transpose(float* from, float* dest, int dim_x, int dim_y)
63+
{
64+
int stride = dim_x * dim_y;
65+
66+
for (int c = 0; c < 3; c++)
67+
{
68+
for (int i = 0; i < dim_x * dim_y; i++)
69+
{
70+
dest[i + stride*c] = from[c + i*3];
71+
}
72+
}
4573
}
4674

4775

AITracker/src/imageprocessor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class ImageProcessor
77
public:
88
ImageProcessor();
99
void normalize(cv::Mat& image);
10-
void cvt_format(float* from, float* dest, int dim = 224);
11-
void transpose(float* from, float* dest, int dim = 224);
10+
void cvt_format(float* from, float* dest, int dim_x = 224, int dim_y=224);
11+
void transpose(float* from, float* dest, int dim_x = 224, int dim_y=224);
1212

1313
private:
1414
cv::Scalar mean_scaling, std_scaling;

0 commit comments

Comments
 (0)