2021-09-25 17:51:15 +02:00
|
|
|
/**
|
|
|
|
* Warmup algorithm that uses embedded images to excercise loaded models for faster future inference
|
|
|
|
*/
|
|
|
|
|
2021-09-27 19:58:13 +02:00
|
|
|
import { log, now, mergeDeep } from './util/util';
|
2021-09-13 00:37:06 +02:00
|
|
|
import * as sample from './sample';
|
|
|
|
import * as tf from '../dist/tfjs.esm.js';
|
2021-09-13 19:28:35 +02:00
|
|
|
import * as image from './image/image';
|
|
|
|
import type { Config } from './config';
|
|
|
|
import type { Result } from './result';
|
2021-09-27 19:58:13 +02:00
|
|
|
import { env } from './util/env';
|
2021-09-13 00:37:06 +02:00
|
|
|
|
|
|
|
async function warmupBitmap(instance) {
|
|
|
|
const b64toBlob = (base64: string, type = 'application/octet-stream') => fetch(`data:${type};base64,${base64}`).then((res) => res.blob());
|
|
|
|
let blob;
|
|
|
|
let res;
|
|
|
|
switch (instance.config.warmup) {
|
|
|
|
case 'face': blob = await b64toBlob(sample.face); break;
|
2021-09-21 03:59:49 +02:00
|
|
|
case 'body':
|
2021-09-13 00:37:06 +02:00
|
|
|
case 'full': blob = await b64toBlob(sample.body); break;
|
|
|
|
default: blob = null;
|
|
|
|
}
|
|
|
|
if (blob) {
|
|
|
|
const bitmap = await createImageBitmap(blob);
|
|
|
|
res = await instance.detect(bitmap, instance.config);
|
|
|
|
bitmap.close();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function warmupCanvas(instance) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
let src;
|
2021-09-13 19:28:35 +02:00
|
|
|
// let size = 0;
|
2021-09-13 00:37:06 +02:00
|
|
|
switch (instance.config.warmup) {
|
|
|
|
case 'face':
|
2021-09-13 19:28:35 +02:00
|
|
|
// size = 256;
|
2021-09-13 00:37:06 +02:00
|
|
|
src = 'data:image/jpeg;base64,' + sample.face;
|
|
|
|
break;
|
|
|
|
case 'full':
|
|
|
|
case 'body':
|
2021-09-13 19:28:35 +02:00
|
|
|
// size = 1200;
|
2021-09-13 00:37:06 +02:00
|
|
|
src = 'data:image/jpeg;base64,' + sample.body;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
src = null;
|
|
|
|
}
|
|
|
|
// src = encodeURI('../assets/human-sample-upper.jpg');
|
2021-09-13 19:28:35 +02:00
|
|
|
let img;
|
|
|
|
if (typeof Image !== 'undefined') img = new Image();
|
|
|
|
// @ts-ignore env.image is an external monkey-patch
|
|
|
|
else if (env.Image) img = new env.Image();
|
2021-09-13 00:37:06 +02:00
|
|
|
img.onload = async () => {
|
2021-09-13 19:28:35 +02:00
|
|
|
const canvas = image.canvas(img.naturalWidth, img.naturalHeight);
|
|
|
|
if (!canvas) {
|
|
|
|
log('Warmup: Canvas not found');
|
|
|
|
resolve({});
|
|
|
|
} else {
|
|
|
|
const ctx = canvas.getContext('2d');
|
2021-09-17 20:30:57 +02:00
|
|
|
if (ctx) ctx.drawImage(img, 0, 0);
|
2021-09-13 19:28:35 +02:00
|
|
|
// const data = ctx?.getImageData(0, 0, canvas.height, canvas.width);
|
|
|
|
const tensor = await instance.image(canvas);
|
|
|
|
const res = await instance.detect(tensor.tensor, instance.config);
|
|
|
|
resolve(res);
|
|
|
|
}
|
2021-09-13 00:37:06 +02:00
|
|
|
};
|
|
|
|
if (src) img.src = src;
|
|
|
|
else resolve(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function warmupNode(instance) {
|
|
|
|
const atob = (str: string) => Buffer.from(str, 'base64');
|
|
|
|
let img;
|
|
|
|
if (instance.config.warmup === 'face') img = atob(sample.face);
|
|
|
|
if (instance.config.warmup === 'body' || instance.config.warmup === 'full') img = atob(sample.body);
|
|
|
|
if (!img) return null;
|
|
|
|
let res;
|
|
|
|
if (typeof tf['node'] !== 'undefined') {
|
|
|
|
const data = tf['node'].decodeJpeg(img);
|
|
|
|
const expanded = data.expandDims(0);
|
|
|
|
instance.tf.dispose(data);
|
|
|
|
// log('Input:', expanded);
|
|
|
|
res = await instance.detect(expanded, instance.config);
|
|
|
|
instance.tf.dispose(expanded);
|
|
|
|
} else {
|
|
|
|
if (instance.config.debug) log('Warmup tfjs-node not loaded');
|
|
|
|
/*
|
|
|
|
const input = await canvasJS.loadImage(img);
|
|
|
|
const canvas = canvasJS.createCanvas(input.width, input.height);
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
ctx.drawImage(img, 0, 0, input.width, input.height);
|
|
|
|
res = await instance.detect(input, instance.config);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Warmup method pre-initializes all configured models for faster inference
|
|
|
|
* - can take significant time on startup
|
|
|
|
* - only used for `webgl` and `humangl` backends
|
|
|
|
* @param userConfig?: Config
|
|
|
|
*/
|
|
|
|
export async function warmup(instance, userConfig?: Partial<Config>): Promise<Result | { error }> {
|
|
|
|
const t0 = now();
|
2021-09-21 03:59:49 +02:00
|
|
|
instance.state = 'warmup';
|
2021-09-13 00:37:06 +02:00
|
|
|
if (userConfig) instance.config = mergeDeep(instance.config, userConfig) as Config;
|
|
|
|
if (!instance.config.warmup || instance.config.warmup === 'none') return { error: 'null' };
|
|
|
|
let res;
|
2021-09-21 03:59:49 +02:00
|
|
|
return new Promise(async (resolve) => {
|
|
|
|
if (typeof createImageBitmap === 'function') res = await warmupBitmap(instance);
|
|
|
|
else if (typeof Image !== 'undefined' || env.Canvas !== undefined) res = await warmupCanvas(instance);
|
|
|
|
else res = await warmupNode(instance);
|
|
|
|
const t1 = now();
|
|
|
|
if (instance.config.debug) log('Warmup', instance.config.warmup, Math.round(t1 - t0), 'ms');
|
|
|
|
instance.emit('warmup');
|
|
|
|
resolve(res);
|
|
|
|
});
|
2021-09-13 00:37:06 +02:00
|
|
|
}
|