-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.html
221 lines (178 loc) · 6.65 KB
/
index.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
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width" />
<title>ncnn webassembly scrfd</title>
<style>
video {
/* position: absolute; */
/* visibility: hidden; */
}
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<div>
<h1>ncnn webassembly scrfd</h1>
<div>
<button disabled id="switch-camera-btn" style="height:48px">Switch Camera</button>
</div>
<div>
<canvas id="canvas" width="640"></canvas>
</div>
<video id="video" playsinline autoplay></video>
</div>
<script src="wasmFeatureDetect.js"></script>
<script type='text/javascript'>
var Module = {};
var has_simd;
var has_threads;
var wasmModuleLoaded = false;
var wasmModuleLoadedCallbacks = [];
Module.onRuntimeInitialized = function() {
wasmModuleLoaded = true;
for (var i = 0; i < wasmModuleLoadedCallbacks.length; i++) {
wasmModuleLoadedCallbacks[i]();
}
}
wasmFeatureDetect.simd().then(simdSupported => {
has_simd = simdSupported;
wasmFeatureDetect.threads().then(threadsSupported => {
has_threads = threadsSupported;
if (has_simd)
{
if (has_threads)
{
scrfd_module_name = 'scrfd-simd-threads';
}
else
{
scrfd_module_name = 'scrfd-simd';
}
}
else
{
if (has_threads)
{
scrfd_module_name = 'scrfd-threads';
}
else
{
scrfd_module_name = 'scrfd-basic';
}
}
console.log('load ' + scrfd_module_name);
var scrfdwasm = scrfd_module_name + '.wasm';
var scrfdjs = scrfd_module_name + '.js';
fetch(scrfdwasm)
.then(response => response.arrayBuffer())
.then(buffer => {
Module.wasmBinary = buffer;
var script = document.createElement('script');
script.src = scrfdjs;
script.onload = function() {
console.log('Emscripten boilerplate loaded.');
}
document.body.appendChild(script);
});
});
});
var shouldFaceUser = true;
var stream = null;
var w = 640;
var h = 480;
var dst = null;
var resultarray = null;
var resultbuffer = null;
window.addEventListener('DOMContentLoaded', function() {
var isStreaming = false;
switchcamerabtn = document.getElementById('switch-camera-btn');
video = document.getElementById('video');
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
// Wait until the video stream canvas play
video.addEventListener('canplay', function(e) {
if (!isStreaming) {
// videoWidth isn't always set correctly in all browsers
if (video.videoWidth > 0) h = video.videoHeight / (video.videoWidth / w);
canvas.setAttribute('width', w);
canvas.setAttribute('height', h);
isStreaming = true;
}
}, false);
// Wait for the video to start to play
video.addEventListener('play', function() {
//Setup image memory
var id = ctx.getImageData(0, 0, canvas.width, canvas.height);
var d = id.data;
if (wasmModuleLoaded) {
mallocAndCallSFilter();
} else {
wasmModuleLoadedCallbacks.push(mallocAndCallSFilter);
}
function mallocAndCallSFilter() {
if (dst != null)
{
_free(dst);
dst = null;
}
dst = _malloc(d.length);
//console.log("What " + d.length);
sFilter();
}
});
// check whether we can use facingMode
var supports = navigator.mediaDevices.getSupportedConstraints();
if (supports['facingMode'] === true) {
switchcamerabtn.disabled = false;
}
switchcamerabtn.addEventListener('click', function() {
if (stream == null)
return
stream.getTracks().forEach(t => {
t.stop();
});
shouldFaceUser = !shouldFaceUser;
capture();
});
capture();
});
function capture() {
var constraints = { audio: false, video: { width: 640, height: 480, facingMode: shouldFaceUser ? 'user' : 'environment' } };
navigator.mediaDevices.getUserMedia(constraints)
.then(function(mediaStream) {
var video = document.querySelector('video');
stream = mediaStream;
video.srcObject = mediaStream;
video.onloadedmetadata = function(e) {
video.play();
};
})
.catch(function(err) {
console.log(err.message);
});
}
function ncnn_scrfd() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var data = imageData.data;
HEAPU8.set(data, dst);
_scrfd_ncnn(dst, canvas.width, canvas.height);
var result = HEAPU8.subarray(dst, dst + data.length);
imageData.data.set(result);
ctx.putImageData(imageData, 0, 0);
}
//Request Animation Frame function
var sFilter = function() {
if (video.paused || video.ended) return;
ctx.fillRect(0, 0, w, h);
ctx.drawImage(video, 0, 0, w, h);
ncnn_scrfd();
window.requestAnimationFrame(sFilter);
}
</script>
</body>
</html>