From 7cedebbe897917c067e1810a31a12704b398bae6 Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Thu, 5 Aug 2021 10:38:04 -0400 Subject: [PATCH] minor update --- demo/index.js | 18 +++++++++++-- package.json | 8 +++--- src/image/image.ts | 66 +++++++++++++++++++++++++--------------------- 3 files changed, 56 insertions(+), 36 deletions(-) diff --git a/demo/index.js b/demo/index.js index afaf1de7..e2e8316d 100644 --- a/demo/index.js +++ b/demo/index.js @@ -18,6 +18,8 @@ * ui={}: contains all variables exposed in the UI */ +// test url + // @ts-nocheck // typescript checks disabled as this is pure javascript import Human from '../dist/human.esm.js'; // equivalent of @vladmandic/human @@ -907,7 +909,7 @@ async function main() { log('overriding worker:', ui.useWorker); } if (params.has('backend')) { - userConfig.backend = params.get('backend'); + userConfig.backend = params.get('backend'); // string log('overriding backend:', userConfig.backend); } if (params.has('preload')) { @@ -915,9 +917,21 @@ async function main() { log('overriding preload:', ui.modelsPreload); } if (params.has('warmup')) { - ui.modelsWarmup = JSON.parse(params.get('warmup')); + ui.modelsWarmup = params.get('warmup'); // string log('overriding warmup:', ui.modelsWarmup); } + if (params.has('bench')) { + ui.bench = JSON.parse(params.get('bench')); + log('overriding bench:', ui.bench); + } + if (params.has('draw')) { + ui.drawWarmup = JSON.parse(params.get('draw')); + log('overriding drawWarmup:', ui.drawWarmup); + } + if (params.has('async')) { + userConfig.async = JSON.parse(params.get('async')); + log('overriding async:', userConfig.async); + } // create instance of human human = new Human(userConfig); diff --git a/package.json b/package.json index 3d4ae451..0263a8a7 100644 --- a/package.json +++ b/package.json @@ -65,14 +65,14 @@ "@tensorflow/tfjs-layers": "^3.8.0", "@tensorflow/tfjs-node": "^3.8.0", "@tensorflow/tfjs-node-gpu": "^3.8.0", - "@types/node": "^16.4.9", - "@typescript-eslint/eslint-plugin": "^4.28.5", - "@typescript-eslint/parser": "^4.28.5", + "@types/node": "^16.4.12", + "@typescript-eslint/eslint-plugin": "^4.29.0", + "@typescript-eslint/parser": "^4.29.0", "@vladmandic/pilogger": "^0.2.18", "canvas": "^2.8.0", "chokidar": "^3.5.2", "dayjs": "^1.10.6", - "esbuild": "^0.12.17", + "esbuild": "^0.12.18", "eslint": "^7.32.0", "eslint-config-airbnb-base": "^14.2.1", "eslint-plugin-import": "^2.23.4", diff --git a/src/image/image.ts b/src/image/image.ts index ff896b3e..22b7c90c 100644 --- a/src/image/image.ts +++ b/src/image/image.ts @@ -128,6 +128,10 @@ export function process(input: Input, config: Config): { tensor: Tensor | null, } } outCanvas.data = pixBuffer; + const shape = [outCanvas.height, outCanvas.width, 3]; + const pixels = tf.tensor3d(outCanvas.data, shape, 'float32'); + tensor = tf.expandDims(pixels, 0); + tf.dispose(pixels); } */ } else { @@ -135,36 +139,38 @@ export function process(input: Input, config: Config): { tensor: Tensor | null, if (fx) fx = null; } - // create tensor from image - let pixels; - if (outCanvas.data) { // if we have data, just convert to tensor - const shape = [outCanvas.height, outCanvas.width, 3]; - pixels = tf.tensor3d(outCanvas.data, shape, 'int32'); - } else if (outCanvas instanceof ImageData) { // if input is imagedata, just use it - pixels = tf.browser ? tf.browser.fromPixels(outCanvas) : null; - } else if (config.backend === 'webgl' || config.backend === 'humangl') { // tf kernel-optimized method to get imagedata - // we can use canvas as-is as it already has a context, so we do a silly one more canvas - const tempCanvas = (typeof OffscreenCanvas !== 'undefined') ? new OffscreenCanvas(targetWidth, targetHeight) : document.createElement('canvas'); - tempCanvas.width = targetWidth; - tempCanvas.height = targetHeight; - const tempCtx = tempCanvas.getContext('2d'); - tempCtx?.drawImage(outCanvas, 0, 0); - pixels = tf.browser ? tf.browser.fromPixels(tempCanvas) : null; - } else { // cpu and wasm kernel does not implement efficient fromPixels method - // we can use canvas as-is as it already has a context, so we do a silly one more canvas - const tempCanvas = (typeof OffscreenCanvas !== 'undefined') ? new OffscreenCanvas(targetWidth, targetHeight) : document.createElement('canvas'); - tempCanvas.width = targetWidth; - tempCanvas.height = targetHeight; - const tempCtx = tempCanvas.getContext('2d'); - tempCtx?.drawImage(outCanvas, 0, 0); - const data = tempCtx?.getImageData(0, 0, targetWidth, targetHeight); - pixels = tf.browser ? tf.browser.fromPixels(data) : null; - } - if (pixels) { - const casted = tf.cast(pixels, 'float32'); - tensor = tf.expandDims(casted, 0); - tf.dispose(pixels); - tf.dispose(casted); + // create tensor from image if tensor is not already defined + if (!tensor) { + let pixels; + if (outCanvas.data) { // if we have data, just convert to tensor + const shape = [outCanvas.height, outCanvas.width, 3]; + pixels = tf.tensor3d(outCanvas.data, shape, 'int32'); + } else if (outCanvas instanceof ImageData) { // if input is imagedata, just use it + pixels = tf.browser ? tf.browser.fromPixels(outCanvas) : null; + } else if (config.backend === 'webgl' || config.backend === 'humangl') { // tf kernel-optimized method to get imagedata + // we cant use canvas as-is as it already has a context, so we do a silly one more canvas + const tempCanvas = (typeof OffscreenCanvas !== 'undefined') ? new OffscreenCanvas(targetWidth, targetHeight) : document.createElement('canvas'); + tempCanvas.width = targetWidth; + tempCanvas.height = targetHeight; + const tempCtx = tempCanvas.getContext('2d'); + tempCtx?.drawImage(outCanvas, 0, 0); + pixels = tf.browser ? tf.browser.fromPixels(tempCanvas) : null; + } else { // cpu and wasm kernel does not implement efficient fromPixels method + // we cant use canvas as-is as it already has a context, so we do a silly one more canvas and do fromPixels on ImageData instead + const tempCanvas = (typeof OffscreenCanvas !== 'undefined') ? new OffscreenCanvas(targetWidth, targetHeight) : document.createElement('canvas'); + tempCanvas.width = targetWidth; + tempCanvas.height = targetHeight; + const tempCtx = tempCanvas.getContext('2d'); + tempCtx?.drawImage(outCanvas, 0, 0); + const data = tempCtx?.getImageData(0, 0, targetWidth, targetHeight); + pixels = tf.browser ? tf.browser.fromPixels(data) : null; + } + if (pixels) { + const casted = tf.cast(pixels, 'float32'); + tensor = tf.expandDims(casted, 0); + tf.dispose(pixels); + tf.dispose(casted); + } } } const canvas = config.filter.return ? outCanvas : null;