-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain.js
283 lines (260 loc) · 9.59 KB
/
main.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
'use strict';
import {FastStyleTransferNet} from './fast_style_transfer_net.js';
import * as ui from '../common/ui.js';
import * as utils from '../common/utils.js';
const maxWidth = 380;
const maxHeight = 380;
const imgElement = document.getElementById('feedElement');
imgElement.src = './images/content-images/travelspace.jpg';
const camElement = document.getElementById('feedMediaElement');
let modelId = 'starry-night';
let isFirstTimeLoad = true;
let isModelChanged = false;
let rafReq;
let inputType = 'image';
let fastStyleTransferNet;
let stream = null;
let loadTime = 0;
let buildTime = 0;
let computeTime = 0;
let deviceType = '';
let lastdeviceType = '';
let backend = '';
let lastBackend = '';
const disabledSelectors = [
'#tabs > li',
'#gallery',
'#gallery > div > img',
'.btn',
];
$(document).ready(async () => {
$('.icdisplay').hide();
if (!await utils.isWebNN()) {
console.log(utils.webNNNotSupportMessage());
ui.addAlert(utils.webNNNotSupportMessageHTML());
}
});
$(document).ready(() => {
$('.icdisplay').hide();
$('.badge').html(modelId);
});
$('#backendBtns .btn').on('change', async (e) => {
[backend, deviceType] =
$('input[name="backend"]:checked').attr('id').split('_');
if (inputType === 'camera') utils.stopCameraStream(rafReq, stream);
await main();
});
// Click trigger to do inference with <img> element
$('#img').click(async () => {
if (inputType === 'camera') utils.stopCameraStream(rafReq, stream);
inputType = 'image';
$('.shoulddisplay').hide();
await main();
});
$('#gallery .gallery-image').hover((e) => {
const id = $(e.target).attr('id');
const modelName = $('#' + id).attr('title');
$('.badge').html(modelName);
}, () => {
const modelName = $(`#${modelId}`).attr('title');
$('.badge').html(modelName);
});
// Click trigger to do inference with switched <img> element
$('#gallery .gallery-item').click(async (e) => {
const newModelId = $(e.target).attr('id');
if (inputType === 'camera') utils.stopCameraStream(rafReq, stream);
if (newModelId !== modelId) {
isModelChanged = true;
modelId = newModelId;
const modelName = $(`#${modelId}`).attr('title');
$('.badge').html(modelName);
$('#gallery .gallery-item').removeClass('hl');
$(e.target).parent().addClass('hl');
}
await main();
});
$('#imageFile').change((e) => {
const files = e.target.files;
if (files.length > 0) {
$('#feedElement').removeAttr('height');
$('#feedElement').removeAttr('width');
imgElement.src = URL.createObjectURL(files[0]);
}
});
$('#feedElement').on('load', async () => {
if (!isFirstTimeLoad) {
await main();
}
});
// Click trigger to do inference with <video> media element
$('#cam').click(async () => {
inputType = 'camera';
$('.shoulddisplay').hide();
await main();
});
/**
* This method is used to render live camera tab.
*/
async function renderCamStream() {
if (!stream.active) return;
// If the video element's readyState is 0, the video's width and height are 0.
// So check the readState here to make sure it is greater than 0.
if (camElement.readyState === 0) {
rafReq = requestAnimationFrame(renderCamStream);
return;
}
const inputBuffer =
utils.getInputTensor(camElement, fastStyleTransferNet.inputOptions);
const inputCanvas = utils.getVideoFrame(camElement);
console.log('- Computing... ');
const start = performance.now();
const outputBuffer = await fastStyleTransferNet.compute(inputBuffer);
computeTime = (performance.now() - start).toFixed(2);
console.log(` done in ${computeTime} ms.`);
camElement.width = camElement.videoWidth;
camElement.height = camElement.videoHeight;
drawInput(inputCanvas, 'camInCanvas');
showPerfResult();
drawOutput('camInCanvas', 'camOutCanvas', outputBuffer);
$('#fps').text(`${(1000/computeTime).toFixed(0)} FPS`);
rafReq = requestAnimationFrame(renderCamStream);
}
function drawInput(srcElement, canvasId) {
const inputCanvas = document.getElementById(canvasId);
const resizeRatio = Math.max(
Math.max(srcElement.width / maxWidth, srcElement.height / maxHeight), 1);
const scaledWidth = Math.floor(srcElement.width / resizeRatio);
const scaledHeight = Math.floor(srcElement.height / resizeRatio);
inputCanvas.height = scaledHeight;
inputCanvas.width = scaledWidth;
const ctx = inputCanvas.getContext('2d');
ctx.drawImage(srcElement, 0, 0, scaledWidth, scaledHeight);
}
function drawOutput(inCanvasId, outCanvasId, outputBuffer) {
const outputSize = fastStyleTransferNet.outputShape;
const height = outputSize[2];
const width = outputSize[3];
const mean = [1, 1, 1, 1];
const offset = [0, 0, 0, 0];
const bytes = new Uint8ClampedArray(width * height * 4);
const a = 255;
for (let i = 0; i < height * width; ++i) {
const j = i * 4;
const r = outputBuffer[i] * mean[0] + offset[0];
const g = outputBuffer[i + height * width] * mean[1] + offset[1];
const b = outputBuffer[i + height * width * 2] * mean[2] + offset[2];
bytes[j + 0] = Math.round(r);
bytes[j + 1] = Math.round(g);
bytes[j + 2] = Math.round(b);
bytes[j + 3] = Math.round(a);
}
const imageData = new ImageData(bytes, width, height);
const outCanvas = document.createElement('canvas');
const outCtx = outCanvas.getContext('2d');
outCanvas.width = width;
outCanvas.height = height;
outCtx.putImageData(imageData, 0, 0, 0, 0, outCanvas.width, outCanvas.height);
const inputCanvas = document.getElementById(inCanvasId);
const outputCanvas = document.getElementById(outCanvasId);
outputCanvas.width = inputCanvas.width;
outputCanvas.height = inputCanvas.height;
const ctx = outputCanvas.getContext('2d');
ctx.drawImage(outCanvas, 0, 0, outputCanvas.width, outputCanvas.height);
}
function showPerfResult(medianComputeTime = undefined) {
$('#loadTime').html(`${loadTime} ms`);
$('#buildTime').html(`${buildTime} ms`);
if (medianComputeTime !== undefined) {
$('#computeLabel').html('Median inference time:');
$('#computeTime').html(`${medianComputeTime} ms`);
} else {
$('#computeLabel').html('Inference time:');
$('#computeTime').html(`${computeTime} ms`);
}
}
export async function main() {
try {
if (backend === '') return;
ui.handleClick(disabledSelectors, true);
if (isFirstTimeLoad) $('#hint').hide();
let start;
const [numRuns, powerPreference, numThreads] = utils.getUrlParams();
// Only do load() and build() when model first time loads,
// there's new model choosed, backend changed or device changed
if (isFirstTimeLoad || isModelChanged ||
lastdeviceType != deviceType || lastBackend != backend) {
if (lastdeviceType != deviceType || lastBackend != backend) {
// Set backend and device
lastdeviceType = lastdeviceType != deviceType ?
deviceType : lastdeviceType;
lastBackend = lastBackend != backend ? backend : lastBackend;
}
fastStyleTransferNet = new FastStyleTransferNet();
isFirstTimeLoad = false;
isModelChanged = false;
console.log(`- Model ID: ${modelId} -`);
// UI shows model loading progress
await ui.showProgressComponent('current', 'pending', 'pending');
console.log('- Loading weights... ');
const contextOptions = {deviceType};
if (powerPreference) {
contextOptions['powerPreference'] = powerPreference;
}
if (numThreads) {
contextOptions['numThreads'] = numThreads;
}
start = performance.now();
const outputOperand =
await fastStyleTransferNet.load(contextOptions, modelId);
loadTime = (performance.now() - start).toFixed(2);
console.log(` done in ${loadTime} ms.`);
// UI shows model building progress
await ui.showProgressComponent('done', 'current', 'pending');
console.log('- Building... ');
start = performance.now();
await fastStyleTransferNet.build(outputOperand);
buildTime = (performance.now() - start).toFixed(2);
console.log(` done in ${buildTime} ms.`);
}
// UI shows inferencing progress
await ui.showProgressComponent('done', 'done', 'current');
if (inputType === 'image') {
const inputBuffer =
utils.getInputTensor(imgElement, fastStyleTransferNet.inputOptions);
console.log('- Computing... ');
const computeTimeArray = [];
let medianComputeTime;
// Do warm up
const outputBuffer = await fastStyleTransferNet.compute(inputBuffer);
for (let i = 0; i < numRuns; i++) {
start = performance.now();
await fastStyleTransferNet.compute(inputBuffer);
computeTime = (performance.now() - start).toFixed(2);
console.log(` compute time ${i+1}: ${computeTime} ms`);
computeTimeArray.push(Number(computeTime));
}
if (numRuns > 1) {
medianComputeTime = utils.getMedianValue(computeTimeArray);
medianComputeTime = medianComputeTime.toFixed(2);
console.log(` median compute time: ${medianComputeTime} ms`);
}
await ui.showProgressComponent('done', 'done', 'done');
ui.readyShowResultComponents();
drawInput(imgElement, 'inputCanvas');
drawOutput('inputCanvas', 'outputCanvas', outputBuffer);
showPerfResult(medianComputeTime);
} else if (inputType === 'camera') {
stream = await utils.getMediaStream();
camElement.srcObject = stream;
camElement.onloadeddata = await renderCamStream();
await ui.showProgressComponent('done', 'done', 'done');
ui.readyShowResultComponents();
} else {
throw Error(`Unknown inputType ${inputType}`);
}
} catch (error) {
console.log(error);
ui.addAlert(error.message);
}
ui.handleClick(disabledSelectors, false);
}