-
Notifications
You must be signed in to change notification settings - Fork 4
/
FaceWASM.cpp
216 lines (164 loc) · 4.85 KB
/
FaceWASM.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
205
206
207
208
209
210
211
212
213
214
215
#include "Face.h"
#include <emscripten.h>
#include <emscripten/bind.h>
using namespace std;
using namespace cv;
using namespace emscripten;
struct jsPt {
float x;
float y;
};
struct jsObj {
float R;
float P;
float Y;
vector<jsPt> vr;
float S;
};
class FaceWASM {
public:
bool tracker;
float valid,tolerance;
int patchsz, padding;
Face* t;
int width,height;
Mat frame, grayframe;
Mat patch;
Mat world2patch;
Mat patch2world;
vector<Rect> faces;
std::vector<cv::Point2f> wPts;
std::vector<cv::Point2f> esPts;
Mat esPtsVec;
Mat wPtsVec;
FaceWASM(int width, int height) {
/********************/
/* INITIALITZATIONS */
tracker = false;
valid = 0.0f;
tolerance = 0.6f;
this->width = width;
this->height = height;
// Define the template
patchsz = 129;
padding = 100;
t = new Face(patchsz, padding);
// Initialize the image
patch = Mat(t->width, t->height, CV_8UC1, Scalar(0));
// Initialize Mapping transformations
world2patch = Mat(2, 3, CV_32FC1);
patch2world = Mat(2, 3, CV_32FC1);
esPtsVec = Mat(136, 1, CV_32FC1);
wPtsVec = Mat(136, 1, CV_32FC1);
}
jsObj processFrame(int buffer) {
jsPt r;
jsObj obj;
uchar* bufferptr = reinterpret_cast<uchar*>(buffer);
frame = Mat(height, width, CV_8UC4, bufferptr);
cvtColor(frame, grayframe, CV_RGBA2GRAY );
if (!tracker) {
// Detect the face using Viola & Jones
t->detectFaces(grayframe, this->faces);
for (int f=0; f < faces.size(); f++) {
esPts = t->meanPts;
t->ipatch2tmplt(grayframe, patch, world2patch, patch2world, faces[f]);
valid = t->estimateLandmarks(patch, esPts, t->omegaD);
if (valid > tolerance) {
// Transform estimated landmarks from tmplt to world
transform(esPts, wPts, patch2world);
t->ipatch2tmpltPro(grayframe, patch, world2patch, patch2world, wPts);
transform(wPts, esPts, world2patch);
obj.S = sqrt(pow(patch2world.at<float>(0, 0), 2) + pow(patch2world.at<float>(1, 1), 2));
for (int j = 0; j < (this->wPts).size(); j++) {
r.x = wPts[j].x;
r.y = wPts[j].y;
obj.vr.push_back(r);
}
// 3D pose
for (int j = 0; j < 68; j++) {
wPtsVec.at<float>(j) = wPts[j].x - wPts[30].x;
wPtsVec.at<float>(68 + j) = wPts[j].y - wPts[30].y;
}
double max, min;
minMaxLoc(abs(wPtsVec.rowRange(0, 67)), &min, &max, 0, 0);
wPtsVec = wPtsVec.mul(1 / max);
Mat nshape;
cv::vconcat(Mat::ones(1, 1, CV_32FC1), wPtsVec, nshape);
Mat RPY = t->omega3D[0].t() * nshape;
obj.R = RPY.at<float>(0);
obj.P = RPY.at<float>(1);
obj.Y = RPY.at<float>(2);
tracker = true;
cout << "msg:initializing face" << endl;
break;
}
}
faces.clear();
}
else {
t->ipatch2tmpltPro(grayframe, patch, world2patch, patch2world, wPts);
transform(wPts, esPts, world2patch);
for (int j = 0; j < 68; j++) {
esPtsVec.at<float>(j) = esPts[j].x;
esPtsVec.at<float>(68 + j) = esPts[j].y;
}
// Project and reconstruct to constrain initialization
esPtsVec = t->muS[0] + t->eigS[0] * (t->eigS[0].t()*(esPtsVec - t->muS[0]));
for (int j = 0; j < 68; j++) {
esPts[j].x = esPtsVec.at<float>(j);
esPts[j].y = esPtsVec.at<float>(68 + j);
}
valid = t->estimateLandmarks(patch, esPts, t->omegaT);
if (valid > tolerance) {
// Transform estimated landmarks from tmplt to world
transform(esPts, wPts, patch2world);
t->ipatch2tmpltPro(grayframe, patch, world2patch, patch2world, wPts);
transform(wPts, esPts, world2patch);
obj.S = sqrt(pow(patch2world.at<float>(0, 0), 2) + pow(patch2world.at<float>(0, 1), 2));
for (int j = 0; j < (this->wPts).size(); j++) {
r.x = wPts[j].x;
r.y = wPts[j].y;
obj.vr.push_back(r);
}
// 3D pose
for (int j = 0; j < 68; j++) {
wPtsVec.at<float>(j) = wPts[j].x - wPts[30].x;
wPtsVec.at<float>(68 + j) = wPts[j].y - wPts[30].y;
}
double max, min;
minMaxLoc(abs(wPtsVec.rowRange(0, 67)), &min, &max, 0, 0);
wPtsVec = wPtsVec.mul(1 / max);
Mat nshape;
cv::vconcat(Mat::ones(1, 1, CV_32FC1), wPtsVec, nshape);
Mat RPY = t->omega3D[0].t() * nshape;
obj.R = RPY.at<float>(0);
obj.P = RPY.at<float>(1);
obj.Y = RPY.at<float>(2);
}
else {
// Fallback to detection mode
tracker = false;
}
}
return obj;
}
};
EMSCRIPTEN_BINDINGS(facewasm) {
value_array<jsPt>("jsPt")
.element(&jsPt::x)
.element(&jsPt::y)
;
register_vector<jsPt>("vPts");
value_array<jsObj>("jsObj")
.element(&jsObj::R)
.element(&jsObj::P)
.element(&jsObj::Y)
.element(&jsObj::vr)
.element(&jsObj::S)
;
class_<FaceWASM>("FaceWASM")
.constructor<int,int>()
.function("processFrame",&FaceWASM::processFrame)
;
}