forked from WebAR-rocks/WebAR.rocks.face
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebARRocksFaceDebugHelper.js
365 lines (286 loc) · 10.1 KB
/
WebARRocksFaceDebugHelper.js
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
* Copyright 2020 WebAR.rocks ( https://webar.rocks )
*
* WARNING: YOU SHOULD NOT MODIFY THIS FILE OTHERWISE WEBAR.ROCKS
* WON'T BE RESPONSIBLE TO MAINTAIN AND KEEP YOUR ADDED FEATURES
* WEBAR.ROCKS WON'T BE LIABLE FOR BREAKS IN YOUR ADDED FUNCTIONNALITIES
*
* WEBAR.ROCKS KEEP THE RIGHT TO WORK ON AN UNMODIFIED VERSION OF THIS SCRIPT.
*
* THIS FILE IS A HELPER AND SHOULD NOT BE MODIFIED TO IMPLEMENT A SPECIFIC USER SCENARIO
* OR TO ADDRESS A SPECIFIC USE CASE.
*/
const WebARRocksFaceDebugHelper = (function(){
const _settings = {
pointSize: 5, // when landmarks are displayed, their size in pixels
};
let _spec = null;
let _pointSize = _settings.pointSize;
const _shps = { // shader programs
drawPoints: null,
copy: null
};
let _gl = null, _cv = null;
let _videoElement = null;
const _landmarks = {
labels: null,
indices: {}
};
const _drawLandmarks = {
vertices: null,
glIndicesVBO: null,
glVerticesVBO: null
};
const _landmarksStabilizers = [];
// compile a shader:
function compile_shader(source, glType, typeString) {
const glShader = _gl.createShader(glType);
_gl.shaderSource(glShader, source);
_gl.compileShader(glShader);
if (!_gl.getShaderParameter(glShader, _gl.COMPILE_STATUS)) {
alert("ERROR IN " + typeString + " SHADER: " + _gl.getShaderInfoLog(glShader));
console.log('Buggy shader source: \n', source);
return null;
}
return glShader;
};
// build the shader program:
function build_shaderProgram(shaderVertexSource, shaderFragmentSource, id) {
// compile both shader separately:
const GLSLprecision = 'precision lowp float;';
const glShaderVertex = compile_shader(shaderVertexSource, _gl.VERTEX_SHADER, "VERTEX " + id);
const glShaderFragment = compile_shader(GLSLprecision + shaderFragmentSource, _gl.FRAGMENT_SHADER, "FRAGMENT " + id);
const glShaderProgram = _gl.createProgram();
_gl.attachShader(glShaderProgram, glShaderVertex);
_gl.attachShader(glShaderProgram, glShaderFragment);
// start the linking stage:
_gl.linkProgram(glShaderProgram);
const aPos = _gl.getAttribLocation(glShaderProgram, "position");
_gl.enableVertexAttribArray(aPos);
return {
program: glShaderProgram,
uniforms: {}
};
}
function init_drawLandmarks(){
_drawLandmarks.vertices = new Float32Array(_landmarks.labels.length*2);
// create vertex buffer objects:
// VBO to draw only 1 point
_drawLandmarks.glVerticesVBO = _gl.createBuffer();
_gl.bindBuffer(_gl.ARRAY_BUFFER, _drawLandmarks.glVerticesVBO);
_gl.bufferData(_gl.ARRAY_BUFFER, _drawLandmarks.vertices, _gl.DYNAMIC_DRAW);
const indices = new Uint16Array(_landmarks.labels.length);
for (let i=0; i<_landmarks.labels.length; ++i){
indices[i] = i;
}
_drawLandmarks.glIndicesVBO = _gl.createBuffer();
_gl.bindBuffer(_gl.ELEMENT_ARRAY_BUFFER, _drawLandmarks.glIndicesVBO);
_gl.bufferData(_gl.ELEMENT_ARRAY_BUFFER, indices, _gl.STATIC_DRAW);
}
function callbackReady(err, spec){
if (err){
console.log('ERROR in WebARRocksFaceDebugHelper. ERR =', err);
if (_spec.callbackReady){
_spec.callbackReady(err, null);
}
return;
}
console.log('INFO in WebARRocksFaceDebugHelper: WebAR.Rocks.face is ready. spec =', spec);
_gl = spec.GL;
_cv = spec.canvasElement;
_landmarks.labels = spec.landmarksLabels;
_videoElement = spec.video;
if (_videoElement){
console.log('INFO in WebARRocksFaceDebugHelper: video resolution =', _videoElement.videoWidth, 'x', _videoElement.videoHeight);
}
_landmarks.labels.forEach(function(label, ind){
_landmarks.indices[label] = ind;
});
init_shps();
init_drawLandmarks();
if (_spec.callbackReady){
_spec.callbackReady(err, spec);
}
}
function callbackTrack(detectStates){
_gl.viewport(0, 0, that.get_viewWidth(), that.get_viewHeight());
// draw the video:
WEBARROCKSFACE.render_video();
if (detectStates.length){ // multiface detection:
detectStates.forEach(process_faceSlot);
} else { // only 1 face detected
process_faceSlot(detectStates, 0);
}
if (_spec.callbackTrack){
_spec.callbackTrack(detectStates);
}
}
function get_landmarksStabilizer(slotIndex){
if (!_landmarksStabilizers[slotIndex]){
_landmarksStabilizers[slotIndex] = WebARRocksLMStabilizer.instance({});
}
return _landmarksStabilizers[slotIndex];
}
function process_faceSlot(detectState, slotIndex){
if (detectState.isDetected) {
// stabilize landmarks:
let landmarks = null;
if (_spec.isStabilized){
landmarks = get_landmarksStabilizer(slotIndex).update(detectState.landmarks, that.get_viewWidth(), that.get_viewHeight(), detectState.s);
} else {
landmarks = detectState.landmarks;
}
// draw landmarks:
draw_landmarks(landmarks);
} else if(_spec.isStabilized){
get_landmarksStabilizer(slotIndex).reset();
}
}
function draw_landmarks(landmarks){
// copy landmarks:
landmarks.forEach(copy_landmark);
// draw landmarks:
_gl.useProgram(_shps.drawPoints.program);
_gl.uniform1f(_shps.drawPoints.uniforms.pointSize, _pointSize);
_gl.bindBuffer(_gl.ARRAY_BUFFER, _drawLandmarks.glVerticesVBO);
_gl.bufferData(_gl.ARRAY_BUFFER, _drawLandmarks.vertices, _gl.DYNAMIC_DRAW);
_gl.bindBuffer(_gl.ELEMENT_ARRAY_BUFFER, _drawLandmarks.glIndicesVBO);
_gl.vertexAttribPointer(0, 2, _gl.FLOAT, false, 8,0);
_gl.drawElements(_gl.POINTS, _landmarks.labels.length, _gl.UNSIGNED_SHORT, 0);
}
function copy_landmark(lm, lmIndex){
_drawLandmarks.vertices[lmIndex*2] = lm[0]; // X
_drawLandmarks.vertices[lmIndex*2 + 1] = lm[1]; // Y
}
// build shader programs:
function init_shps(){
// create LM display shader program:
const shaderVertexSource = "attribute vec2 position;\n\
uniform float pointSize;\n\
void main(void) {\n\
gl_PointSize = pointSize;\n\
gl_Position = vec4(position, 0., 1.);\n\
} ";
// display lime color:
const shaderFragmentSource = "void main(void){\n\
gl_FragColor = vec4(0.,1.,0.,1.);\n\
}";
_shps.drawPoints = build_shaderProgram(shaderVertexSource, shaderFragmentSource, 'DRAWPOINT');
_shps.drawPoints.uniforms.pointSize = _gl.getUniformLocation(_shps.drawPoints.program, 'pointSize');
}
const that = {
init: function(spec){
_spec = Object.assign({
spec: {},
isStabilized: (typeof(WebARRocksLMStabilizer) !== 'undefined'),
videoURL: null,
// callbacks:
callbackReady: null,
callbackTrack: null
}, spec);
// init WEBAR.rocks.face:WEBARROCKSFACE
const defaultSpecLM = {
canvas: null,
canvasId: 'WebARRocksFaceCanvas',
NNCPath: '../../neuralNets/',
callbackReady: callbackReady,
callbackTrack: callbackTrack
};
_spec.spec = Object.assign({}, defaultSpecLM, spec.spec);
if (_spec.spec.canvas === null){
_spec.spec.canvas = document.getElementById(_spec.spec.canvasId);
}
if (_spec.videoURL){
that.load_video(_spec.videoURL).then(function(){
WEBARROCKSFACE.init(_spec.spec);
})
} else {
WEBARROCKSFACE.init(_spec.spec);
}
},
load_video: function(videoURL){
const domVideo = document.createElement('video');
domVideo.setAttribute('src', videoURL);
domVideo.setAttribute('autoplay', true);
domVideo.setAttribute('loop', true);
domVideo.setAttribute('preload', true);
domVideo.setAttribute('muted', 'muted');
domVideo.setAttribute('playsinline', true); // for IOS
// append the video to the DOM for debug:
document.body.appendChild(domVideo);
domVideo.style.maxWidth = '50vw';
domVideo.style.border = "1px solid red";
return new Promise(function(accept, reject){
domVideo.oncanplay = function(e){
console.log('INFO in WebARRocksFaceThreeHelper: video file can play');
domVideo.oncanplay = null;
let isPlaying = false;
// the user needs to interact with the DOM to start the video (browser security)
const onUserEvent = function(){
domVideo.play();
if (isPlaying) return;
domVideo.style.display = 'none';
isPlaying = true;
_spec.spec.videoSettings = {videoElement: domVideo};
accept();
}
window.addEventListener('click', onUserEvent); // desktop
window.addEventListener('touchstart', onUserEvent); // mobile
}
});
},
resize: function(w, h){
if (_gl){
// Fix a bug with IOS14.7 and WebGL2
_gl.bindFramebuffer(_gl.FRAMEBUFFER, null);
}
_cv.width = w, _cv.height = h;
WEBARROCKSFACE.resize();
},
get_sourceWidth: function(){
return _videoElement.videoWidth;
},
get_sourceHeight: function(){
return _videoElement.videoHeight;
},
get_viewWidth: function(){
return _cv.width;
},
get_viewHeight: function(){
return _cv.height;
},
get_viewAspectRatio: function(){
return that.get_viewWidth() / that.get_viewHeight();
},
set_pointSize: function(ps){
_pointSize = ps;
},
change_NN: function(NNUrl){
return WEBARROCKSFACE.update({
NNCPath: NNUrl
}).then(function(){
_landmarks.labels = WEBARROCKSFACE.get_LMLabels();
init_drawLandmarks();
});
},
update_video: function(video){
return new Promise(function(accept, reject){
WEBARROCKSFACE.update_videoElement(video, function(glVideoTexture){
WEBARROCKSFACE.reset();
WEBARROCKSFACE.resize();
accept();
});
});
},
toggle_stabilization: function(isStabilized){
_spec.isStabilized = isStabilized;
}
}; //end that
return that;
})();
// Export ES6 module:
try {
module.exports = WebARRocksFaceDebugHelper;
} catch(e){
console.log('ES6 Module not exported');
}