minor update

pull/356/head
Vladimir Mandic 2021-08-05 10:38:04 -04:00
parent b70775caa9
commit 7cedebbe89
3 changed files with 56 additions and 36 deletions

View File

@ -18,6 +18,8 @@
* ui={}: contains all variables exposed in the UI * ui={}: contains all variables exposed in the UI
*/ */
// test url <https://human.local/?worker=false&async=false&bench=false&draw=true&warmup=full&backend=humangl>
// @ts-nocheck // typescript checks disabled as this is pure javascript // @ts-nocheck // typescript checks disabled as this is pure javascript
import Human from '../dist/human.esm.js'; // equivalent of @vladmandic/human import Human from '../dist/human.esm.js'; // equivalent of @vladmandic/human
@ -907,7 +909,7 @@ async function main() {
log('overriding worker:', ui.useWorker); log('overriding worker:', ui.useWorker);
} }
if (params.has('backend')) { if (params.has('backend')) {
userConfig.backend = params.get('backend'); userConfig.backend = params.get('backend'); // string
log('overriding backend:', userConfig.backend); log('overriding backend:', userConfig.backend);
} }
if (params.has('preload')) { if (params.has('preload')) {
@ -915,9 +917,21 @@ async function main() {
log('overriding preload:', ui.modelsPreload); log('overriding preload:', ui.modelsPreload);
} }
if (params.has('warmup')) { if (params.has('warmup')) {
ui.modelsWarmup = JSON.parse(params.get('warmup')); ui.modelsWarmup = params.get('warmup'); // string
log('overriding warmup:', ui.modelsWarmup); 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 // create instance of human
human = new Human(userConfig); human = new Human(userConfig);

View File

@ -65,14 +65,14 @@
"@tensorflow/tfjs-layers": "^3.8.0", "@tensorflow/tfjs-layers": "^3.8.0",
"@tensorflow/tfjs-node": "^3.8.0", "@tensorflow/tfjs-node": "^3.8.0",
"@tensorflow/tfjs-node-gpu": "^3.8.0", "@tensorflow/tfjs-node-gpu": "^3.8.0",
"@types/node": "^16.4.9", "@types/node": "^16.4.12",
"@typescript-eslint/eslint-plugin": "^4.28.5", "@typescript-eslint/eslint-plugin": "^4.29.0",
"@typescript-eslint/parser": "^4.28.5", "@typescript-eslint/parser": "^4.29.0",
"@vladmandic/pilogger": "^0.2.18", "@vladmandic/pilogger": "^0.2.18",
"canvas": "^2.8.0", "canvas": "^2.8.0",
"chokidar": "^3.5.2", "chokidar": "^3.5.2",
"dayjs": "^1.10.6", "dayjs": "^1.10.6",
"esbuild": "^0.12.17", "esbuild": "^0.12.18",
"eslint": "^7.32.0", "eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1", "eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.23.4", "eslint-plugin-import": "^2.23.4",

View File

@ -128,6 +128,10 @@ export function process(input: Input, config: Config): { tensor: Tensor | null,
} }
} }
outCanvas.data = pixBuffer; 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 { } else {
@ -135,36 +139,38 @@ export function process(input: Input, config: Config): { tensor: Tensor | null,
if (fx) fx = null; if (fx) fx = null;
} }
// create tensor from image // create tensor from image if tensor is not already defined
let pixels; if (!tensor) {
if (outCanvas.data) { // if we have data, just convert to tensor let pixels;
const shape = [outCanvas.height, outCanvas.width, 3]; if (outCanvas.data) { // if we have data, just convert to tensor
pixels = tf.tensor3d(outCanvas.data, shape, 'int32'); const shape = [outCanvas.height, outCanvas.width, 3];
} else if (outCanvas instanceof ImageData) { // if input is imagedata, just use it pixels = tf.tensor3d(outCanvas.data, shape, 'int32');
pixels = tf.browser ? tf.browser.fromPixels(outCanvas) : null; } else if (outCanvas instanceof ImageData) { // if input is imagedata, just use it
} else if (config.backend === 'webgl' || config.backend === 'humangl') { // tf kernel-optimized method to get imagedata pixels = tf.browser ? tf.browser.fromPixels(outCanvas) : null;
// we can use canvas as-is as it already has a context, so we do a silly one more canvas } else if (config.backend === 'webgl' || config.backend === 'humangl') { // tf kernel-optimized method to get imagedata
const tempCanvas = (typeof OffscreenCanvas !== 'undefined') ? new OffscreenCanvas(targetWidth, targetHeight) : document.createElement('canvas'); // we cant use canvas as-is as it already has a context, so we do a silly one more canvas
tempCanvas.width = targetWidth; const tempCanvas = (typeof OffscreenCanvas !== 'undefined') ? new OffscreenCanvas(targetWidth, targetHeight) : document.createElement('canvas');
tempCanvas.height = targetHeight; tempCanvas.width = targetWidth;
const tempCtx = tempCanvas.getContext('2d'); tempCanvas.height = targetHeight;
tempCtx?.drawImage(outCanvas, 0, 0); const tempCtx = tempCanvas.getContext('2d');
pixels = tf.browser ? tf.browser.fromPixels(tempCanvas) : null; tempCtx?.drawImage(outCanvas, 0, 0);
} else { // cpu and wasm kernel does not implement efficient fromPixels method pixels = tf.browser ? tf.browser.fromPixels(tempCanvas) : null;
// we can use canvas as-is as it already has a context, so we do a silly one more canvas } else { // cpu and wasm kernel does not implement efficient fromPixels method
const tempCanvas = (typeof OffscreenCanvas !== 'undefined') ? new OffscreenCanvas(targetWidth, targetHeight) : document.createElement('canvas'); // 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
tempCanvas.width = targetWidth; const tempCanvas = (typeof OffscreenCanvas !== 'undefined') ? new OffscreenCanvas(targetWidth, targetHeight) : document.createElement('canvas');
tempCanvas.height = targetHeight; tempCanvas.width = targetWidth;
const tempCtx = tempCanvas.getContext('2d'); tempCanvas.height = targetHeight;
tempCtx?.drawImage(outCanvas, 0, 0); const tempCtx = tempCanvas.getContext('2d');
const data = tempCtx?.getImageData(0, 0, targetWidth, targetHeight); tempCtx?.drawImage(outCanvas, 0, 0);
pixels = tf.browser ? tf.browser.fromPixels(data) : null; const data = tempCtx?.getImageData(0, 0, targetWidth, targetHeight);
} pixels = tf.browser ? tf.browser.fromPixels(data) : null;
if (pixels) { }
const casted = tf.cast(pixels, 'float32'); if (pixels) {
tensor = tf.expandDims(casted, 0); const casted = tf.cast(pixels, 'float32');
tf.dispose(pixels); tensor = tf.expandDims(casted, 0);
tf.dispose(casted); tf.dispose(pixels);
tf.dispose(casted);
}
} }
} }
const canvas = config.filter.return ? outCanvas : null; const canvas = config.filter.return ? outCanvas : null;