-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
152 lines (125 loc) · 5.55 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
<html lang="en">
<head>
<meta charset="utf-8">
<title>retinaface</title>
</head>
<body>
<h3>retinaface</h3>
<div id="file_btn">
<input type="hidden" id="input" accept="image/*"></input>
</div>
<div>
<p id="infer_status">Infer status:</p>
<canvas id="canvas"></canvas>
</div>
<div>
<p id="loading">Load status:</p>
<p id="wasm_support">Webassembly supporting:</p>
</div>
<script src="wasm-feature-detect.js" type="text/javascript"></script>
<script type='text/javascript'>
// check whether browser support webassembly
const supported = (() => {
try {
if (typeof WebAssembly === "object"
&& typeof WebAssembly.instantiate === "function") {
const module = new WebAssembly.Module(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));
if (module instanceof WebAssembly.Module)
return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
}
} catch (e) {
}
return false;
})();
console.log(supported ? "WebAssembly is supported" : "WebAssembly is not supported");
document.getElementById('wasm_support').innerText = document.getElementById('wasm_support').innerText + (supported ? " supported" : " not supported");
var Module = {};
wasmFeatureDetect.simd().then(simdSupported => {
if (simdSupported) {
Module['dynamicLibraries'] = ['libtennis-simd.wasm'];
} else {
Module['dynamicLibraries'] = ['libtennis.wasm'];
}
});
fetch('retinaface.wasm')
.then(response => response.arrayBuffer())
.then(buffer => {
Module.wasmBinary = buffer;
var script = document.createElement('script');
script.src = 'retinaface.js';
script.onload = function() {
console.log('Emscripten boilerplate loaded.');
}
document.body.appendChild(script);
var loading_ele = document.getElementById("loading");
loading_ele.innerText = loading_ele.innerText + " succeed";
var file_btn = document.getElementById('input');
file_btn.type = "file";
});
var colors = [
"rgb(176, 39, 156)",
];
function retinaface() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var data = imageData.data;
dst = _malloc(canvas.width * canvas.height * 3);
HEAPU8.set(data, dst);
(async () => {
resultarray = new Float32Array(20 * 5);
resultbuffer = _malloc(20 * 5 * Float32Array.BYTES_PER_ELEMENT);
HEAPF32.set(resultarray, resultbuffer / Float32Array.BYTES_PER_ELEMENT);
_retinaface(dst, canvas.width, canvas.height, resultbuffer);
js_resultarray = HEAPF32.subarray(resultbuffer / Float32Array.BYTES_PER_ELEMENT, resultbuffer / Float32Array.BYTES_PER_ELEMENT + 5 * 20);
var i;
for (i = 0; i < 20; i++) {
var bbox_x = js_resultarray[i * 5 + 0];
var bbox_y = js_resultarray[i * 5 + 1];
var bbox_w = js_resultarray[i * 5 + 2];
var bbox_h = js_resultarray[i * 5 + 3];
var prob = js_resultarray[i * 5 + 4];
ctx.strokeStyle = colors[0];
ctx.strokeRect(bbox_x, bbox_y, bbox_w, bbox_h);
console.log("prob: ", prob);
}
delete resultarray;
})();
_free(resultbuffer);
_free(dst);
}
document.getElementById('input').onchange = function(e) {
var infer_ele = document.getElementById("infer_status");
infer_ele.innerText = "Infer status:";
var reader = new FileReader();
reader.onload = function(event) {
var img = new Image();
img.onload = function() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
// scale down to 640x640
var img_w = img.width;
var img_h = img.height;
// if (img_w > img_h) {
// img_h = img_h * 256 / img_w;
// img_w = 256;
// }
// else {
// img_w = img_w * 256 / img_h;
// img_h = 256;
// }
canvas.width = img_w;
canvas.height = img_h;
ctx.drawImage(img, 0, 0, img_w, img_h);
var infer_status_text = infer_ele.innerText;
infer_ele.innerText = infer_status_text + " infer...";
retinaface();
infer_ele.innerText = infer_status_text + " infer successfully";
}
img.src = event.target.result;
}
reader.readAsDataURL(e.target.files[0]);
};
</script>
</body>
</html>