-
Notifications
You must be signed in to change notification settings - Fork 148
/
minimalWebcamWorker.html
241 lines (146 loc) · 5.96 KB
/
minimalWebcamWorker.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Beyond Reality Face - BRFv4 - HTML5/Javascript face tracking</title>
<style>
html, body { width: 100%; height: 100%; background-color: #ffffff; margin: 0; padding: 0; overflow: hidden; }
* {position: absolute; }
</style>
</head>
<body>
<video id="_webcam" style="display: none;" playsinline></video>
<canvas id="_imageData"></canvas>
<canvas id="_resultData"></canvas>
<script>
var brfv4 = {};
var brfv4Example = { stats: {} };
</script>
<script src="js/utils/BRFv4Stats.js"></script>
<script src="js/utils/BRFv4PublicAPI.js"></script>
<script>
if(!window.Worker) {
throw "No worker support";
}
console.log("Loading worker (WASM)");
var worker = new Worker("js/libs/brf_wasm/BRFv4_JS_TK210219_v4.2.0_trial.worker.js");
worker.addEventListener("error", function(e) {
console.error("js worker error: ", e);
}, false);
var webcam = document.getElementById("_webcam"); // our webcam video
var imageData = document.getElementById("_imageData"); // image data for BRFv4
var resultData = document.getElementById("_resultData"); // canvas fpr BRFv4 results
var imageDataCtx = null;
var resultDataCtx = null;
var resolution = null;
var gotLastResult = true;
var stats = brfv4Example.stats;
if(stats.init) { stats.init(60); }
function waitForSDK() {
console.log("js waitForSDK");
worker.addEventListener('message', function(e) {
// console.log("js message", e);
if(e.data === "initSDK") {
initSDK();
} else if(e.data === "onInitBRFv4Manager") {
trackFaces();
} else {
var vertices = new Float32Array(e.data);
if(vertices.length === 68 * 2) {
// onTrackedFaces
gotLastResult = true;
resultDataCtx.clearRect(0, 0, 640, 480);
resultDataCtx.fillStyle="#00a0ff";
var r = 2;
for(var k = 0; k < vertices.length; k += 2) {
resultDataCtx.fillRect(vertices[k] - r, vertices[k + 1] - r, 2 * r, 2 * r);
}
if(brfv4Example.stats.end) brfv4Example.stats.end();
}
}
}, false);
console.log("js postMessage waitForSDK");
worker.postMessage("waitForSDK");
}
function initSDK() {
console.log("js initSDK");
resolution = new brfv4.Rectangle(0, 0, imageData.width, imageData.height);
// var imgData = ctx.getImageData(0, 0, 512, 512);
var resolutionData = new Uint32Array(4);
resolutionData.set([resolution.x, resolution.y, resolution.width, resolution.height]);
// "initBRFv4Manager"
worker.postMessage(resolutionData.buffer, [resolutionData.buffer]);
}
function trackFaces() {
cancelAnimationFrame(trackFaces);
imageDataCtx.setTransform(-1.0, 0, 0, 1, resolution.width, 0); // mirrored for draw of video
imageDataCtx.drawImage(webcam, 0, 0, resolution.width, resolution.height);
imageDataCtx.setTransform( 1.0, 0, 0, 1, 0, 0); // unmirrored for draw of results
if(gotLastResult) {
if(brfv4Example.stats.start) brfv4Example.stats.start();
gotLastResult = false;
var _imageData = imageDataCtx.getImageData(0, 0, resolution.width, resolution.height);
var imageDataArray = new Uint8ClampedArray(imageData.width * imageData.height * 4);
imageDataArray.set(_imageData.data);
worker.postMessage(imageDataArray.buffer, [imageDataArray.buffer]);
} else {
// skip frame, still waiting for tha last results from worker
}
requestAnimationFrame(trackFaces);
}
function startCamera() {
console.log("startCamera");
// Start video playback once the camera was fetched.
function onStreamFetched (mediaStream) {
console.log("onStreamFetched");
webcam.srcObject = mediaStream;
webcam.play();
// Check whether we know the video dimensions yet, if so, start BRFv4.
function onStreamDimensionsAvailable () {
console.log("onStreamDimensionsAvailable");
if (webcam.videoWidth === 0) {
setTimeout(onStreamDimensionsAvailable, 100);
} else {
// Resize the canvas to match the webcam video size.
imageData.width = webcam.videoWidth;
imageData.height = webcam.videoHeight;
imageDataCtx = imageData.getContext("2d");
resultData.width = webcam.videoWidth;
resultData.height = webcam.videoHeight;
resultDataCtx = resultData.getContext("2d");
onResize();
window.addEventListener("resize", onResize);
waitForSDK();
}
}
if(imageDataCtx === null) {
onStreamDimensionsAvailable();
} else {
trackFaces();
}
}
window.navigator.mediaDevices.getUserMedia({video: {width: 640, height: 480, frameRate: 30}})
.then(onStreamFetched).catch(function () { alert("No camera available."); });
}
startCamera();
function onResize () {
var imageData = document.getElementById("_imageData"); // image data for BRFv4
var resultData = document.getElementById("_resultData"); // image data for BRFv4
var ww = window.innerWidth;
var wh = window.innerHeight;
var s = wh / imageData.height;
if (imageData.width * s < ww) {
s = ww / imageData.width;
}
var iw = imageData.width * s;
var ih = imageData.height * s;
var ix = (ww - iw) * 0.5;
var iy = (wh - ih) * 0.5;
imageData.style.transformOrigin = "0% 0%";
imageData.style.transform = "matrix("+s+", 0, 0, "+s+", "+ix+", "+iy+")";
resultData.style.transformOrigin = "0% 0%";
resultData.style.transform = "matrix("+s+", 0, 0, "+s+", "+ix+", "+iy+")";
}
</script>
</body>
</html>