-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.js
559 lines (459 loc) · 16.3 KB
/
camera.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/**
* @license
* Copyright 2020 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import * as posenet_module from '@tensorflow-models/posenet';
import * as facemesh_module from '@tensorflow-models/facemesh';
import * as tf from '@tensorflow/tfjs';
import * as paper from 'paper';
import dat from 'dat.gui';
import Stats from 'stats.js';
import 'babel-polyfill';
import {drawKeypoints, drawPoint, drawSkeleton, isMobile, toggleLoadingUI, setStatusText} from './utils/demoUtils';
import {SVGUtils} from './utils/svgUtils';
import {PoseIllustration} from './illustrationGen/illustration';
import {Skeleton, facePartName2Index} from './illustrationGen/skeleton';
import {FileUtils} from './utils/fileUtils';
import * as girlSVG from './resources/illustration/girl.svg';
import * as boySVG from './resources/illustration/boy.svg';
import * as abstractSVG from './resources/illustration/abstract.svg';
import * as blathersSVG from './resources/illustration/blathers.svg';
import * as tomNookSVG from './resources/illustration/tom-nook.svg';
// Camera stream video element
let video;
let videoWidth = 500;
let videoHeight = 500;
// Canvas
let faceDetection = null;
let illustration = null;
let canvasScope;
let canvasWidth = 500;
let canvasHeight = 500;
// ML models
let facemesh;
let posenet;
let minPoseConfidence = 0.15;
let minPartConfidence = 0.1;
let nmsRadius = 30.0;
// Misc
let mobile = false;
const stats = new Stats();
const avatarSvgs = {
'girl': girlSVG.default,
'boy': boySVG.default,
'abstract': abstractSVG.default,
'blathers': blathersSVG.default,
'tom-nook': tomNookSVG.default,
};
// references for render setup
const keypointCanvas = document.getElementById('keypoints');
const canvas = document.getElementById('output');
const keypointCtx = keypointCanvas.getContext('2d');
const videoCtx = canvas.getContext('2d');
// WebRTC connection nodes
let pc1;
let pc2;
// WebRTC streaming channel
let channel;
// Analysis monitors
// const monitors = ['bytesReceived', 'packetsReceived', 'headerBytesReceived', 'packetsLost', 'totalDecodeTime', 'totalInterFrameDelay', 'codecId'];
const monitors = ['bytesReceived'];
// order list for poses deconstruction and reconstruction
const parts = ['nose', 'leftEye', 'rightEye', 'leftEar', 'rightEar', 'leftShoulder', 'rightShoulder', 'leftElbow', 'rightElbow', 'leftWrist', 'rightWrist', 'leftHip', 'rightHip', 'leftKnee', 'rightKnee', 'leftAnkle', 'rightAnkle'];
// summations for finding necessary statistics
let previousTime;
let previousBytesIntegral = 0;
function getOtherPeerConnection(pc) {
if (pc === pc1) {
return pc2;
} else {
return pc1;
}
}
/**
* Adds the passed candidate to the opposite node of the connection provided
*
*/
function onIceCandidate(pc, event) {
(getOtherPeerConnection(pc)).addIceCandidate(event.candidate);
}
/**
* Connects the two peer connections, adds handlers for messages received
* and data channel detected.
*
*/
async function initiateRtcStreamingChannel() {
// setting up pc1 (receiving end)
pc1 = new RTCPeerConnection({});
pc1.addEventListener('icecandidate', e => onIceCandidate(pc1, e));
// creates the data channel at the receiving end, transmission starts when the transmitting end detects this channel
const dataChannel = pc1.createDataChannel('pose-animator data channel');
// for messages received, parse the transmitted arrays as poses and project them
let message = [];
let faceDetection;
dataChannel.onmessage = function(event) {
message.push(event.data);
if (message.length === 4) {
// builds pose object
let pose = reconstructPose(new Int16Array(message[0]), new Int16Array(message[1]));
// clears the output canvas
canvasScope.project.clear();
// projects the poses skeleton on the existing svg skeleton
Skeleton.flipPose(pose);
illustration.updateSkeleton(pose, null);
// illustration.draw(canvasScope, videoWidth, videoHeight);
if (guiState.debug.showIllustrationDebug) {
illustration.debugDraw(canvasScope);
}
canvasScope.project.activeLayer.scale(
canvasWidth / videoWidth,
canvasHeight / videoHeight,
new canvasScope.Point(0, 0));
// faceDetection = JSON.parse(message[2]);
let faceData = message[2];
if (faceData !== 0) {
let face = {
positions: reconstructFaceData(message[2]),
faceInViewConfidence: message[3]
};
illustration.updateSkeleton(pose, face);
// if (faceDetection && faceDetection.length > 0) {
// let face = Skeleton.toFaceFrame(faceDetection[0]);
// illustration.updateSkeleton(pose, face);
// }
illustration.draw(canvasScope, videoWidth, videoHeight);
}
message = [];
}
};
// setting up pc2 (transmitting end)
pc2 = new RTCPeerConnection({});
pc2.addEventListener('icecandidate', e => onIceCandidate(pc2, e));
// sets the pc2 data channel to the global context
pc2.ondatachannel = function(event) {
channel = event.channel;
};
let statsInterval = window.setInterval(getConnectionStats, 1000);
// connects pc1 and pc2
let offer = await pc1.createOffer({
offerToReceiveAudio: 0,
offerToReceiveVideo: 0,
});
await pc2.setRemoteDescription(offer);
await pc1.setLocalDescription(offer);
let answer = await pc2.createAnswer();
await pc1.setRemoteDescription(answer);
await pc2.setLocalDescription(answer);
}
// in: buffer for a Uint32Array
function reconstructFaceData(positionsBuffer) {
let view = new Float32Array(positionsBuffer);
let out = [];
view.forEach(coordinate => {
out.push(coordinate);
});
return out;
}
/**
* Loops the transmission of deconstructed poses
*
*/
async function transmit() {
// Begin monitoring code for frames per second
stats.begin();
// get face information
const input = tf.browser.fromPixels(canvas);
faceDetection = await facemesh.estimateFaces(input, false, false);
input.dispose();
// initializes poses
let poses = [];
// populates poses
let all_poses = await posenet.estimatePoses(video, {
flipHorizontal: true,
decodingMethod: 'multi-person',
maxDetections: 1,
scoreThreshold: minPartConfidence,
nmsRadius: nmsRadius,
});
// merges all poses
poses = poses.concat(all_poses);
// clears previous render
videoCtx.clearRect(0, 0, videoWidth, videoHeight);
// draw video
videoCtx.save();
videoCtx.scale(-1, 1);
videoCtx.translate(-videoWidth, 0);
videoCtx.drawImage(video, 0, 0, videoWidth, videoHeight);
videoCtx.restore();
// projects pose and face onto svg
keypointCtx.clearRect(0, 0, videoWidth, videoHeight);
if (guiState.debug.showDetectionDebug) {
poses.forEach(({score, keypoints}) => {
if (score >= minPoseConfidence) {
drawKeypoints(keypoints, minPartConfidence, keypointCtx);
drawSkeleton(keypoints, minPartConfidence, keypointCtx);
}
});
faceDetection.forEach(face => {
for (let i = 0; i < face.scaledMesh.length; i++) {
let p = face.scaledMesh[i];
drawPoint(keypointCtx, p[1], p[0], 2, 'red');
}
//
// Object.values(facePartName2Index).forEach(index => {
// let p = face.scaledMesh[index];
// drawPoint(keypointCtx, p[1], p[0], 2, 'red');
// });
});
}
// converts pose to streamable buffers
let deconstructedPose = deconstructPose(poses[0]);
// deconstructedPose === null if difference between consecutive frames is 0
if (deconstructedPose !== null) {
channel.send(deconstructedPose[0].buffer);
channel.send(deconstructedPose[1].buffer);
}
// channel.send(JSON.stringify(faceDetection));
if (faceDetection && faceDetection.length > 0) {
// let face = Skeleton.toFaceFrame(faceDetection[0]);
let face = Skeleton.toBufferedFaceFrame(faceDetection[0]);
channel.send(face.positions.buffer);
channel.send(face.faceInViewConfidence);
} else {
channel.send(0);
channel.send(0);
}
// channel.send(JSON.stringify(Skeleton.toFaceFrame(faceDetection[0])));
// End monitoring code for frames per second
stats.end();
// loop back
setTimeout(transmit, 10);
}
/**
* Converts a pose object to streamable array views, the corresponding
* buffers are streamed
*
*/
function deconstructPose(pose) {
if (pose == null) return null;
let confidences = new Int16Array(18);
let positions = new Int16Array(34);
confidences[0] = 10000 * pose.score; // to reduce transmission size
for (let i = 0; i < pose.keypoints.length; i++) {
confidences[i + 1] = 10000 * pose.keypoints[i].score;
positions[i * 2] = pose.keypoints[i].position.x;
positions[i * 2 + 1] = pose.keypoints[i].position.y;
}
return [confidences, positions];
}
/**
* Converts streamed arrays (after view initialized) into a pose object for
* animation rendering.
*
*/
function reconstructPose(confidences, positions) {
let pose = {
'score': confidences[0] / 10000,
'keypoints': [],
};
for (let i = 0; i < 17; i += 1) {
pose.keypoints.push({
'score': confidences[i + 1] / 10000,
'part': parts[i],
'position': {
'x': positions[i * 2],
'y': positions[i * 2 + 1],
},
});
}
return pose;
}
/**
* Loads a the camera to be used in the demo
*
*/
async function setupCamera() {
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
throw new Error(
'Browser API navigator.mediaDevices.getUserMedia not available');
}
const video = document.getElementById('video');
video.width = videoWidth;
video.height = videoHeight;
const stream = await navigator.mediaDevices.getUserMedia({
'audio': false,
'video': {
facingMode: 'user',
width: videoWidth,
height: videoHeight,
},
});
video.srcObject = stream;
return new Promise((resolve) => {
video.onloadedmetadata = () => {
resolve(video);
};
});
}
async function loadVideo() {
const video = await setupCamera();
video.play();
return video;
}
const defaultPoseNetArchitecture = 'MobileNetV1';
const defaultQuantBytes = 2;
const defaultMultiplier = 1.0;
const defaultStride = 16;
const defaultInputResolution = 200;
const guiState = {
avatarSVG: Object.keys(avatarSvgs)[0],
debug: {
showDetectionDebug: true,
showIllustrationDebug: false,
},
};
/**
* Sets up dat.gui controller on the top-right of the window
*
*/
function setupGui(cameras) {
if (cameras.length > 0) {
guiState.camera = cameras[0].deviceId;
}
const gui = new dat.GUI({width: 300});
let multi = gui.addFolder('Image');
gui.add(guiState, 'avatarSVG', Object.keys(avatarSvgs)).onChange(() => parseSVG(avatarSvgs[guiState.avatarSVG]));
multi.open();
let output = gui.addFolder('Debug control');
output.add(guiState.debug, 'showDetectionDebug');
output.add(guiState.debug, 'showIllustrationDebug');
output.open();
}
/**
* Sets up a frames per second panel on the top-left of the window
*
*/
function setupFPS() {
stats.showPanel(0);
document.getElementById('main').appendChild(stats.dom);
}
// more render configuration
function setupCanvas() {
mobile = isMobile();
if (mobile) {
canvasWidth = Math.min(window.innerWidth, window.innerHeight);
canvasHeight = canvasWidth;
videoWidth *= 0.7;
videoHeight *= 0.7;
}
canvasScope = paper.default;
let canvas = document.querySelector('.illustration-canvas');
canvas.width = canvasWidth;
canvas.height = canvasHeight;
canvasScope.setup(canvas);
}
/**
* Kicks off the demo by loading the posenet model, finding and loading
* available camera devices, and setting off pose transmission device.
*/
export async function bindPage() {
setupCanvas();
toggleLoadingUI(true);
setStatusText('Loading PoseNet model...');
posenet = await posenet_module.load({
architecture: defaultPoseNetArchitecture,
outputStride: defaultStride,
inputResolution: defaultInputResolution,
multiplier: defaultMultiplier,
quantBytes: defaultQuantBytes,
});
setStatusText('Loading FaceMesh model...');
facemesh = await facemesh_module.load();
setStatusText('Loading Avatar file...');
let t0 = new Date();
await parseSVG(Object.values(avatarSvgs)[0]);
setStatusText('Setting up camera...');
try {
video = await loadVideo();
} catch (e) {
let info = document.getElementById('info');
info.textContent = 'this device type is not supported yet, ' +
'or this browser does not support video capture: ' + e.toString();
info.style.display = 'block';
throw e;
}
setupGui([], posenet);
setupFPS();
toggleLoadingUI(false);
}
// initiates svg skeleton to be used
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
FileUtils.setDragDropHandler((result) => {
parseSVG(result);
});
async function parseSVG(target) {
let svgScope = await SVGUtils.importSVG(target /* SVG string or file path */);
let skeleton = new Skeleton(svgScope);
illustration = new PoseIllustration(canvasScope);
illustration.bindSkeleton(skeleton, svgScope);
}
/**
* Monitors inbound byte stream for the calculation of network transmission rate
*
*/
function getConnectionStats() {
let taken = [];
pc1.getStats(null).then(stats => {
let statsOutput = '';
stats.forEach(report => {
if (!report.id.startsWith('RTCDataChannel_')) return;
Object.keys(report).forEach(statName => {
if (monitors.includes(statName)) {
let bytesIntegral = parseInt(report[statName]);
if (bytesIntegral !== 0 && !taken.includes(statName)) {
let currentTime = new Date().getTime();
let timeIntegral = (currentTime - previousTime) / 1000;
let kbytesPerSecond = (bytesIntegral - previousBytesIntegral) / timeIntegral / 1000;
previousBytesIntegral = bytesIntegral;
previousTime = currentTime;
if (statName === 'bytesReceived') {
statsOutput += `<strong>kilobit rate: </strong> ${(kbytesPerSecond * 8).toFixed(2)} kb/s <br>`;
taken.push(statName);
} else {
statsOutput += `<strong>${statName}:</strong> ${kbytesPerSecond * 8} kb/s <br>`;
taken.push(statName);
}
}
}
});
});
document.querySelector('#bitstream-box').innerHTML = statsOutput;
});
return 0;
}
function startTimer() {
previousTime = new Date().getTime();
}
/**
* Sets up local and receiving renderers
*/
function configureRender() {
canvas.width = videoWidth;
canvas.height = videoHeight;
keypointCanvas.width = videoWidth;
keypointCanvas.height = videoHeight;
}
bindPage().then(initiateRtcStreamingChannel).then(configureRender).then(startTimer).then(transmit);