web worker fixes

pull/293/head
Vladimir Mandic 2020-11-15 09:28:57 -05:00
parent 92b4ac71dd
commit a4da0b7fe2
2 changed files with 8 additions and 5 deletions

View File

@ -247,7 +247,7 @@ function webWorker(input, image, canvas, timestamp) {
} }
// pass image data as arraybuffer to worker by reference to avoid copy // pass image data as arraybuffer to worker by reference to avoid copy
if (ui.bench) bench.begin(); if (ui.bench) bench.begin();
worker.postMessage({ image: image.data.buffer, width: canvas.width, height: canvas.height }, [image.data.buffer]); worker.postMessage({ image: image.data.buffer, width: canvas.width, height: canvas.height, userConfig }, [image.data.buffer]);
} }
// main processing function when input is webcam, can use direct invocation or web worker // main processing function when input is webcam, can use direct invocation or web worker
@ -272,7 +272,10 @@ function runHumanDetect(input, canvas, timestamp) {
status(''); status('');
if (ui.useWorker) { if (ui.useWorker) {
// get image data from video as we cannot send html objects to webworker // get image data from video as we cannot send html objects to webworker
const offscreen = new OffscreenCanvas(canvas.width, canvas.height); const offscreen = (typeof OffscreenCanvas !== 'undefined') ? new OffscreenCanvas(canvas.width, canvas.height) : document.createElement('canvas');
offscreen.width = canvas.width;
offscreen.height = canvas.height;
const ctx = offscreen.getContext('2d'); const ctx = offscreen.getContext('2d');
ctx.drawImage(input, 0, 0, input.width, input.height, 0, 0, canvas.width, canvas.height); ctx.drawImage(input, 0, 0, input.width, input.height, 0, 0, canvas.width, canvas.height);
const data = ctx.getImageData(0, 0, canvas.width, canvas.height); const data = ctx.getImageData(0, 0, canvas.width, canvas.height);
@ -475,12 +478,12 @@ async function main() {
document.getElementById('log').innerText = `Human: version ${human.version} TensorFlow/JS: version ${human.tf.version_core}`; document.getElementById('log').innerText = `Human: version ${human.version} TensorFlow/JS: version ${human.tf.version_core}`;
// human.tf.ENV.set('WEBGL_FORCE_F16_TEXTURES', true); // human.tf.ENV.set('WEBGL_FORCE_F16_TEXTURES', true);
// this is not required, just pre-loads all models // this is not required, just pre-loads all models
if (ui.modelsPreload) { if (ui.modelsPreload && !ui.useWorker) {
status('loading'); status('loading');
await human.load(userConfig); await human.load(userConfig);
} }
// this is not required, just pre-warms all models for faster initial inference // this is not required, just pre-warms all models for faster initial inference
if (ui.modelsWarmup) { if (ui.modelsWarmup && !ui.useWorker) {
status('initializing'); status('initializing');
sample = await human.warmup(userConfig, document.getElementById('sample-image')); sample = await human.warmup(userConfig, document.getElementById('sample-image'));
} }

View File

@ -182,7 +182,7 @@ class Human {
/* debug mode is really too mcuh /* debug mode is really too mcuh
tf.enableDebugMode(); tf.enableDebugMode();
*/ */
if (this.config.backend === 'webgl') { if (tf.getBackend() === 'webgl') {
if (this.config.deallocate) { if (this.config.deallocate) {
this.log('changing webgl: WEBGL_DELETE_TEXTURE_THRESHOLD:', this.config.deallocate); this.log('changing webgl: WEBGL_DELETE_TEXTURE_THRESHOLD:', this.config.deallocate);
tf.ENV.set('WEBGL_DELETE_TEXTURE_THRESHOLD', this.config.deallocate ? 0 : -1); tf.ENV.set('WEBGL_DELETE_TEXTURE_THRESHOLD', this.config.deallocate ? 0 : -1);