diff --git a/CHANGELOG.md b/CHANGELOG.md index 86682e07..15d1d0eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ ## Changelog -### **HEAD -> main** 2022/06/10 mandic00@live.com +### **HEAD -> main** 2022/06/21 mandic00@live.com ### **release: 2.8.1** 2022/06/08 mandic00@live.com diff --git a/demo/typescript/index.js.map b/demo/typescript/index.js.map index faad7e91..16dcc14f 100644 --- a/demo/typescript/index.js.map +++ b/demo/typescript/index.js.map @@ -1,7 +1,7 @@ { "version": 3, "sources": ["index.ts"], - "sourcesContent": ["/**\n * Human demo for browsers\n * @default Human Library\n * @summary \n * @author \n * @copyright \n * @license MIT\n */\n\nimport { Human, Config } from '../../dist/human.esm.js'; // equivalent of @vladmandic/Human\n\nconst humanConfig: Partial = { // user configuration for human, used to fine-tune behavior\n // backend: 'webgpu' as const,\n // wasmPath: 'https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm@3.18.0/dist/',\n // cacheSensitivity: 0,\n async: true,\n modelBasePath: '../../models',\n filter: { enabled: true, equalization: false, flip: false },\n face: { enabled: true, detector: { rotation: false }, mesh: { enabled: true }, attention: { enabled: false }, iris: { enabled: true }, description: { enabled: true }, emotion: { enabled: true } },\n body: { enabled: true },\n hand: { enabled: true },\n object: { enabled: false },\n gesture: { enabled: true },\n};\n\nconst human = new Human(humanConfig); // create instance of human with overrides from user configuration\n\nhuman.env['perfadd'] = false; // is performance data showing instant or total values\nhuman.draw.options.font = 'small-caps 18px \"Lato\"'; // set font used to draw labels when using draw methods\nhuman.draw.options.lineHeight = 20;\n\nconst dom = { // grab instances of dom objects so we dont have to look them up later\n video: document.getElementById('video') as HTMLVideoElement,\n canvas: document.getElementById('canvas') as HTMLCanvasElement,\n log: document.getElementById('log') as HTMLPreElement,\n fps: document.getElementById('status') as HTMLPreElement,\n perf: document.getElementById('performance') as HTMLDivElement,\n};\nconst timestamp = { detect: 0, draw: 0, tensors: 0 }; // holds information used to calculate performance and possible memory leaks\nconst fps = { detect: 0, draw: 0 }; // holds calculated fps information for both detect and screen refresh\n\nconst log = (...msg) => { // helper method to output messages\n dom.log.innerText += msg.join(' ') + '\\n';\n // eslint-disable-next-line no-console\n console.log(...msg);\n};\nconst status = (msg) => dom.fps.innerText = msg; // print status element\nconst perf = (msg) => dom.perf.innerText = 'tensors:' + human.tf.memory().numTensors + ' | performance: ' + JSON.stringify(msg).replace(/\"|{|}/g, '').replace(/,/g, ' | '); // print performance element\n\nasync function webCam() { // initialize webcam\n status('starting webcam...');\n // @ts-ignore resizeMode is not yet defined in tslib\n const options: MediaStreamConstraints = { audio: false, video: { facingMode: 'user', resizeMode: 'none', width: { ideal: document.body.clientWidth }, height: { ideal: document.body.clientHeight } } };\n const stream: MediaStream = await navigator.mediaDevices.getUserMedia(options);\n const ready = new Promise((resolve) => { dom.video.onloadeddata = () => resolve(true); });\n dom.video.srcObject = stream;\n dom.video.play();\n await ready;\n dom.canvas.width = dom.video.videoWidth;\n dom.canvas.height = dom.video.videoHeight;\n const track: MediaStreamTrack = stream.getVideoTracks()[0];\n const capabilities: MediaTrackCapabilities | string = track.getCapabilities ? track.getCapabilities() : '';\n const settings: MediaTrackSettings | string = track.getSettings ? track.getSettings() : '';\n const constraints: MediaTrackConstraints | string = track.getConstraints ? track.getConstraints() : '';\n log('video:', dom.video.videoWidth, dom.video.videoHeight, track.label, { stream, track, settings, constraints, capabilities });\n dom.canvas.onclick = () => { // pause when clicked on screen and resume on next click\n if (dom.video.paused) dom.video.play();\n else dom.video.pause();\n };\n}\n\nasync function detectionLoop() { // main detection loop\n if (!dom.video.paused) {\n // console.log('profiling data:', await human.profile(dom.video));\n await human.detect(dom.video); // actual detection; were not capturing output in a local variable as it can also be reached via human.result\n const tensors = human.tf.memory().numTensors; // check current tensor usage for memory leaks\n if (tensors - timestamp.tensors !== 0) log('allocated tensors:', tensors - timestamp.tensors); // printed on start and each time there is a tensor leak\n timestamp.tensors = tensors;\n }\n const now = human.now();\n fps.detect = 1000 / (now - timestamp.detect);\n timestamp.detect = now;\n requestAnimationFrame(detectionLoop); // start new frame immediately\n}\n\nasync function drawLoop() { // main screen refresh loop\n if (!dom.video.paused) {\n const interpolated = await human.next(human.result); // smoothen result using last-known results\n if (human.config.filter.flip) await human.draw.canvas(interpolated.canvas as HTMLCanvasElement, dom.canvas); // draw processed image to screen canvas\n else await human.draw.canvas(dom.video, dom.canvas); // draw original video to screen canvas // better than using procesed image as this loop happens faster than processing loop\n await human.draw.all(dom.canvas, interpolated); // draw labels, boxes, lines, etc.\n perf(interpolated.performance); // write performance data\n }\n const now = human.now();\n fps.draw = 1000 / (now - timestamp.draw);\n timestamp.draw = now;\n status(dom.video.paused ? 'paused' : `fps: ${fps.detect.toFixed(1).padStart(5, ' ')} detect | ${fps.draw.toFixed(1).padStart(5, ' ')} draw`); // write status\n // requestAnimationFrame(drawLoop); // refresh at screen refresh rate\n setTimeout(drawLoop, 30); // use to slow down refresh from max refresh rate to target of 30 fps\n}\n\nasync function main() { // main entry point\n log('human version:', human.version, '| tfjs version:', human.tf.version['tfjs-core']);\n log('platform:', human.env.platform, '| agent:', human.env.agent);\n status('loading...');\n await human.load(); // preload all models\n log('backend:', human.tf.getBackend(), '| available:', human.env.backends);\n log('loaded models:', Object.values(human.models).filter((model) => model !== null).length);\n status('initializing...');\n await human.warmup(); // warmup function to initialize backend for future faster detection\n await webCam(); // start webcam\n await detectionLoop(); // start detection loop\n await drawLoop(); // start draw loop\n}\n\nwindow.onload = main;\n"], + "sourcesContent": ["/**\n * Human demo for browsers\n * @default Human Library\n * @summary \n * @author \n * @copyright \n * @license MIT\n */\n\nimport { Human, Config } from '../../dist/human.esm.js'; // equivalent of @vladmandic/Human\n\nconst humanConfig: Partial = { // user configuration for human, used to fine-tune behavior\n // backend: 'wasm' as const,\n // wasmPath: 'https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm@3.18.0/dist/',\n // cacheSensitivity: 0,\n async: true,\n modelBasePath: '../../models',\n filter: { enabled: true, equalization: false, flip: false },\n face: { enabled: true, detector: { rotation: false }, mesh: { enabled: true }, attention: { enabled: false }, iris: { enabled: true }, description: { enabled: true }, emotion: { enabled: true } },\n body: { enabled: true },\n hand: { enabled: true },\n object: { enabled: false },\n gesture: { enabled: true },\n};\n\nconst human = new Human(humanConfig); // create instance of human with overrides from user configuration\n\nhuman.env['perfadd'] = false; // is performance data showing instant or total values\nhuman.draw.options.font = 'small-caps 18px \"Lato\"'; // set font used to draw labels when using draw methods\nhuman.draw.options.lineHeight = 20;\n\nconst dom = { // grab instances of dom objects so we dont have to look them up later\n video: document.getElementById('video') as HTMLVideoElement,\n canvas: document.getElementById('canvas') as HTMLCanvasElement,\n log: document.getElementById('log') as HTMLPreElement,\n fps: document.getElementById('status') as HTMLPreElement,\n perf: document.getElementById('performance') as HTMLDivElement,\n};\nconst timestamp = { detect: 0, draw: 0, tensors: 0 }; // holds information used to calculate performance and possible memory leaks\nconst fps = { detect: 0, draw: 0 }; // holds calculated fps information for both detect and screen refresh\n\nconst log = (...msg) => { // helper method to output messages\n dom.log.innerText += msg.join(' ') + '\\n';\n // eslint-disable-next-line no-console\n console.log(...msg);\n};\nconst status = (msg) => dom.fps.innerText = msg; // print status element\nconst perf = (msg) => dom.perf.innerText = 'tensors:' + human.tf.memory().numTensors + ' | performance: ' + JSON.stringify(msg).replace(/\"|{|}/g, '').replace(/,/g, ' | '); // print performance element\n\nasync function webCam() { // initialize webcam\n status('starting webcam...');\n // @ts-ignore resizeMode is not yet defined in tslib\n const options: MediaStreamConstraints = { audio: false, video: { facingMode: 'user', resizeMode: 'none', width: { ideal: document.body.clientWidth }, height: { ideal: document.body.clientHeight } } };\n const stream: MediaStream = await navigator.mediaDevices.getUserMedia(options);\n const ready = new Promise((resolve) => { dom.video.onloadeddata = () => resolve(true); });\n dom.video.srcObject = stream;\n dom.video.play();\n await ready;\n dom.canvas.width = dom.video.videoWidth;\n dom.canvas.height = dom.video.videoHeight;\n const track: MediaStreamTrack = stream.getVideoTracks()[0];\n const capabilities: MediaTrackCapabilities | string = track.getCapabilities ? track.getCapabilities() : '';\n const settings: MediaTrackSettings | string = track.getSettings ? track.getSettings() : '';\n const constraints: MediaTrackConstraints | string = track.getConstraints ? track.getConstraints() : '';\n log('video:', dom.video.videoWidth, dom.video.videoHeight, track.label, { stream, track, settings, constraints, capabilities });\n dom.canvas.onclick = () => { // pause when clicked on screen and resume on next click\n if (dom.video.paused) dom.video.play();\n else dom.video.pause();\n };\n}\n\nasync function detectionLoop() { // main detection loop\n if (!dom.video.paused) {\n // console.log('profiling data:', await human.profile(dom.video));\n await human.detect(dom.video); // actual detection; were not capturing output in a local variable as it can also be reached via human.result\n const tensors = human.tf.memory().numTensors; // check current tensor usage for memory leaks\n if (tensors - timestamp.tensors !== 0) log('allocated tensors:', tensors - timestamp.tensors); // printed on start and each time there is a tensor leak\n timestamp.tensors = tensors;\n }\n const now = human.now();\n fps.detect = 1000 / (now - timestamp.detect);\n timestamp.detect = now;\n requestAnimationFrame(detectionLoop); // start new frame immediately\n}\n\nasync function drawLoop() { // main screen refresh loop\n if (!dom.video.paused) {\n const interpolated = await human.next(human.result); // smoothen result using last-known results\n if (human.config.filter.flip) await human.draw.canvas(interpolated.canvas as HTMLCanvasElement, dom.canvas); // draw processed image to screen canvas\n else await human.draw.canvas(dom.video, dom.canvas); // draw original video to screen canvas // better than using procesed image as this loop happens faster than processing loop\n await human.draw.all(dom.canvas, interpolated); // draw labels, boxes, lines, etc.\n perf(interpolated.performance); // write performance data\n }\n const now = human.now();\n fps.draw = 1000 / (now - timestamp.draw);\n timestamp.draw = now;\n status(dom.video.paused ? 'paused' : `fps: ${fps.detect.toFixed(1).padStart(5, ' ')} detect | ${fps.draw.toFixed(1).padStart(5, ' ')} draw`); // write status\n // requestAnimationFrame(drawLoop); // refresh at screen refresh rate\n setTimeout(drawLoop, 30); // use to slow down refresh from max refresh rate to target of 30 fps\n}\n\nasync function main() { // main entry point\n log('human version:', human.version, '| tfjs version:', human.tf.version['tfjs-core']);\n log('platform:', human.env.platform, '| agent:', human.env.agent);\n status('loading...');\n await human.load(); // preload all models\n log('backend:', human.tf.getBackend(), '| available:', human.env.backends);\n log('loaded models:', Object.values(human.models).filter((model) => model !== null).length);\n status('initializing...');\n await human.warmup(); // warmup function to initialize backend for future faster detection\n await webCam(); // start webcam\n await detectionLoop(); // start detection loop\n await drawLoop(); // start draw loop\n}\n\nwindow.onload = main;\n"], "mappings": ";;;;;;AASA,gDATA,AAWA,GAAM,GAA+B,CAInC,MAAO,GACP,cAAe,eACf,OAAQ,CAAE,QAAS,GAAM,aAAc,GAAO,KAAM,EAAM,EAC1D,KAAM,CAAE,QAAS,GAAM,SAAU,CAAE,SAAU,EAAM,EAAG,KAAM,CAAE,QAAS,EAAK,EAAG,UAAW,CAAE,QAAS,EAAM,EAAG,KAAM,CAAE,QAAS,EAAK,EAAG,YAAa,CAAE,QAAS,EAAK,EAAG,QAAS,CAAE,QAAS,EAAK,CAAE,EAClM,KAAM,CAAE,QAAS,EAAK,EACtB,KAAM,CAAE,QAAS,EAAK,EACtB,OAAQ,CAAE,QAAS,EAAM,EACzB,QAAS,CAAE,QAAS,EAAK,CAC3B,EAEM,EAAQ,GAAI,GAAM,CAAW,EAEnC,EAAM,IAAI,QAAa,GACvB,EAAM,KAAK,QAAQ,KAAO,yBAC1B,EAAM,KAAK,QAAQ,WAAa,GAEhC,GAAM,GAAM,CACV,MAAO,SAAS,eAAe,OAAO,EACtC,OAAQ,SAAS,eAAe,QAAQ,EACxC,IAAK,SAAS,eAAe,KAAK,EAClC,IAAK,SAAS,eAAe,QAAQ,EACrC,KAAM,SAAS,eAAe,aAAa,CAC7C,EACM,EAAY,CAAE,OAAQ,EAAG,KAAM,EAAG,QAAS,CAAE,EAC7C,EAAM,CAAE,OAAQ,EAAG,KAAM,CAAE,EAE3B,EAAM,IAAI,IAAQ,CACtB,EAAI,IAAI,WAAa,EAAI,KAAK,GAAG,EAAI;AAAA,EAErC,QAAQ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AAS,CACjB,CAEA,OAAO,OAAS", "names": [] } diff --git a/demo/typescript/index.ts b/demo/typescript/index.ts index e0d27f04..978999b1 100644 --- a/demo/typescript/index.ts +++ b/demo/typescript/index.ts @@ -10,7 +10,7 @@ import { Human, Config } from '../../dist/human.esm.js'; // equivalent of @vladmandic/Human const humanConfig: Partial = { // user configuration for human, used to fine-tune behavior - // backend: 'webgpu' as const, + // backend: 'wasm' as const, // wasmPath: 'https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm@3.18.0/dist/', // cacheSensitivity: 0, async: true, diff --git a/src/face/faceboxes.ts b/src/face/faceboxes.ts index cf84b56d..d3377933 100644 --- a/src/face/faceboxes.ts +++ b/src/face/faceboxes.ts @@ -2,6 +2,7 @@ import { log } from '../util/util'; import * as tf from '../../dist/tfjs.esm.js'; +import { loadModel } from '../tfjs/load'; import type { GraphModel, Tensor } from '../tfjs/types'; import type { Config } from '../config'; @@ -56,7 +57,7 @@ export class FaceBoxes { } export async function load(config) { - const model = await tf.loadGraphModel(config.face.detector.modelPath); + const model = await loadModel(config.face.detector?.modelPath); if (config.debug) log(`load model: ${config.face.detector.modelPath.match(/\/(.*)\./)[1]}`); const faceboxes = new FaceBoxes(model, config); if (config.face.mesh.enabled && config.debug) log(`load model: ${config.face.mesh.modelPath.match(/\/(.*)\./)[1]}`); diff --git a/src/human.ts b/src/human.ts index d8224aea..d8a6dd02 100644 --- a/src/human.ts +++ b/src/human.ts @@ -329,6 +329,9 @@ export class Human { return interpolate.calc(result, this.config) as Result; } + /** get model loading/loaded stats */ + getModelStats = () => models.getModelStats(); + /** Warmup method pre-initializes all configured models for faster inference * - can take significant time on startup * - only used for `webgl` and `humangl` backends diff --git a/src/models.ts b/src/models.ts index 922122be..ce497569 100644 --- a/src/models.ts +++ b/src/models.ts @@ -24,6 +24,7 @@ import * as movenet from './body/movenet'; import * as nanodet from './object/nanodet'; import * as posenet from './body/posenet'; import * as segmentation from './segmentation/segmentation'; +import { modelStats } from './tfjs/load'; import type { GraphModel } from './tfjs/types'; import type { Human } from './human'; @@ -58,6 +59,16 @@ export class Models { antispoof: null | GraphModel | Promise = null; } +export const getModelStats = () => { + let sizeManifest = 0; + let sizeWeights = 0; + for (const m of Object.values(modelStats)) { + sizeManifest += m.manifest; + sizeWeights += m.weights; + } + return { sizeManifest, sizeWeights, numModels: Object.values(modelStats).length }; +}; + export function reset(instance: Human): void { // if (instance.config.debug) log('resetting loaded models'); for (const model of Object.keys(instance.models)) instance.models[model as keyof Models] = null; @@ -67,8 +78,12 @@ export function reset(instance: Human): void { export async function load(instance: Human): Promise { if (env.initial) reset(instance); if (instance.config.hand.enabled) { // handpose model is a combo that must be loaded as a whole - if (!instance.models.handpose && instance.config.hand.detector?.modelPath?.includes('handdetect')) [instance.models.handpose, instance.models.handskeleton] = await handpose.load(instance.config); - if (!instance.models.handskeleton && instance.config.hand.landmarks && instance.config.hand.detector?.modelPath?.includes('handdetect')) [instance.models.handpose, instance.models.handskeleton] = await handpose.load(instance.config); + if (!instance.models.handpose && instance.config.hand.detector?.modelPath?.includes('handdetect')) { + [instance.models.handpose, instance.models.handskeleton] = await handpose.load(instance.config); + } + if (!instance.models.handskeleton && instance.config.hand.landmarks && instance.config.hand.detector?.modelPath?.includes('handdetect')) { + [instance.models.handpose, instance.models.handskeleton] = await handpose.load(instance.config); + } } if (instance.config.body.enabled && !instance.models.blazepose && instance.config.body?.modelPath?.includes('blazepose')) instance.models.blazepose = blazepose.loadPose(instance.config); // @ts-ignore optional model @@ -99,7 +114,9 @@ export async function load(instance: Human): Promise { // models are loaded in parallel asynchronously so lets wait until they are actually loaded for await (const model of Object.keys(instance.models)) { - if (instance.models[model as keyof Models] && typeof instance.models[model as keyof Models] !== 'undefined') instance.models[model as keyof Models] = await instance.models[model as keyof Models]; + if (instance.models[model as keyof Models] && typeof instance.models[model as keyof Models] !== 'undefined') { + instance.models[model as keyof Models] = await instance.models[model as keyof Models]; + } } } diff --git a/src/tfjs/load.ts b/src/tfjs/load.ts index 751c6826..94a79ed0 100644 --- a/src/tfjs/load.ts +++ b/src/tfjs/load.ts @@ -10,6 +10,15 @@ const options = { modelBasePath: '', }; +type ModelStats = { + name: string, + cached: boolean, + manifest: number, + weights: number, +} + +export const modelStats: Record = {}; + async function httpHandler(url, init?): Promise { if (options.debug) log('load model fetch:', url, init); return fetch(url, init); @@ -25,26 +34,35 @@ export async function loadModel(modelPath: string | undefined): Promise httpHandler(url, init) }; - const model: GraphModel = new tf.GraphModel(modelCached ? cachedModelName : modelUrl, tfLoadOptions) as unknown as GraphModel; // create model prototype and decide if load from cache or from original modelurl + const model: GraphModel = new tf.GraphModel(modelStats[shortModelName].cached ? cachedModelName : modelUrl, tfLoadOptions) as unknown as GraphModel; // create model prototype and decide if load from cache or from original modelurl let loaded = false; try { // @ts-ignore private function model.findIOHandler(); // decide how to actually load a model - // @ts-ignore private property - if (options.debug) log('model load handler:', model.handler); + if (options.debug) log('model load handler:', model['handler']); // @ts-ignore private property const artifacts = await model.handler.load(); // load manifest + modelStats[shortModelName].manifest = artifacts?.weightData?.byteLength || 0; model.loadSync(artifacts); // load weights - if (options.verbose) log('load model:', model['modelUrl']); + // @ts-ignore private property + modelStats[shortModelName].weights = model?.artifacts?.weightData?.byteLength || 0; + if (options.verbose) log('load model:', model['modelUrl'], { bytes: modelStats[shortModelName].weights }); loaded = true; } catch (err) { log('error loading model:', modelUrl, err); } - if (loaded && options.cacheModels && !modelCached) { // save model to cache + if (loaded && options.cacheModels && !modelStats[shortModelName].cached) { // save model to cache try { const saveResult = await model.save(cachedModelName); log('model saved:', cachedModelName, saveResult); diff --git a/test/build.log b/test/build.log index e45cb8a0..3a63a854 100644 --- a/test/build.log +++ b/test/build.log @@ -1,24 +1,24 @@ -2022-06-21 13:20:54 INFO:  Application: {"name":"@vladmandic/human","version":"2.8.1"} -2022-06-21 13:20:54 INFO:  Environment: {"profile":"production","config":".build.json","package":"package.json","tsconfig":true,"eslintrc":true,"git":true} -2022-06-21 13:20:54 INFO:  Toolchain: {"build":"0.7.3","esbuild":"0.14.47","typescript":"4.7.4","typedoc":"0.22.17","eslint":"8.18.0"} -2022-06-21 13:20:54 INFO:  Build: {"profile":"production","steps":["clean","compile","typings","typedoc","lint","changelog"]} -2022-06-21 13:20:54 STATE: Clean: {"locations":["dist/*","types/lib/*","typedoc/*"]} -2022-06-21 13:20:54 STATE: Compile: {"name":"tfjs/nodejs/cpu","format":"cjs","platform":"node","input":"tfjs/tf-node.ts","output":"dist/tfjs.esm.js","files":1,"inputBytes":102,"outputBytes":608} -2022-06-21 13:20:54 STATE: Compile: {"name":"human/nodejs/cpu","format":"cjs","platform":"node","input":"src/human.ts","output":"dist/human.node.js","files":73,"inputBytes":642839,"outputBytes":300711} -2022-06-21 13:20:54 STATE: Compile: {"name":"tfjs/nodejs/gpu","format":"cjs","platform":"node","input":"tfjs/tf-node-gpu.ts","output":"dist/tfjs.esm.js","files":1,"inputBytes":110,"outputBytes":612} -2022-06-21 13:20:54 STATE: Compile: {"name":"human/nodejs/gpu","format":"cjs","platform":"node","input":"src/human.ts","output":"dist/human.node-gpu.js","files":73,"inputBytes":642843,"outputBytes":300715} -2022-06-21 13:20:54 STATE: Compile: {"name":"tfjs/nodejs/wasm","format":"cjs","platform":"node","input":"tfjs/tf-node-wasm.ts","output":"dist/tfjs.esm.js","files":1,"inputBytes":149,"outputBytes":664} -2022-06-21 13:20:54 STATE: Compile: {"name":"human/nodejs/wasm","format":"cjs","platform":"node","input":"src/human.ts","output":"dist/human.node-wasm.js","files":73,"inputBytes":642895,"outputBytes":300765} -2022-06-21 13:20:54 STATE: Compile: {"name":"tfjs/browser/version","format":"esm","platform":"browser","input":"tfjs/tf-version.ts","output":"dist/tfjs.version.js","files":1,"inputBytes":1069,"outputBytes":371} -2022-06-21 13:20:54 STATE: Compile: {"name":"tfjs/browser/esm/nobundle","format":"esm","platform":"browser","input":"tfjs/tf-browser.ts","output":"dist/tfjs.esm.js","files":2,"inputBytes":1045,"outputBytes":596} -2022-06-21 13:20:54 STATE: Compile: {"name":"human/browser/esm/nobundle","format":"esm","platform":"browser","input":"src/human.ts","output":"dist/human.esm-nobundle.js","files":73,"inputBytes":642827,"outputBytes":299608} -2022-06-21 13:20:55 STATE: Compile: {"name":"tfjs/browser/esm/custom","format":"esm","platform":"browser","input":"tfjs/tf-custom.ts","output":"dist/tfjs.esm.js","files":1,"inputBytes":110,"outputBytes":1353541} -2022-06-21 13:20:55 STATE: Compile: {"name":"human/browser/iife/bundle","format":"iife","platform":"browser","input":"src/human.ts","output":"dist/human.js","files":73,"inputBytes":1995772,"outputBytes":1652210} -2022-06-21 13:20:55 STATE: Compile: {"name":"human/browser/esm/bundle","format":"esm","platform":"browser","input":"src/human.ts","output":"dist/human.esm.js","files":73,"inputBytes":1995772,"outputBytes":2139073} -2022-06-21 13:21:07 STATE: Typings: {"input":"src/human.ts","output":"types/lib","files":116} -2022-06-21 13:21:13 STATE: TypeDoc: {"input":"src/human.ts","output":"typedoc","objects":73,"generated":true} -2022-06-21 13:21:13 STATE: Compile: {"name":"demo/typescript","format":"esm","platform":"browser","input":"demo/typescript/index.ts","output":"demo/typescript/index.js","files":1,"inputBytes":6326,"outputBytes":3070} -2022-06-21 13:21:13 STATE: Compile: {"name":"demo/faceid","format":"esm","platform":"browser","input":"demo/faceid/index.ts","output":"demo/faceid/index.js","files":2,"inputBytes":15174,"outputBytes":7833} -2022-06-21 13:22:08 STATE: Lint: {"locations":["*.json","src/**/*.ts","test/**/*.js","demo/**/*.js"],"files":106,"errors":0,"warnings":0} -2022-06-21 13:22:08 STATE: ChangeLog: {"repository":"https://github.com/vladmandic/human","branch":"main","output":"CHANGELOG.md"} -2022-06-21 13:22:08 INFO:  Done... +2022-07-02 03:34:20 INFO:  Application: {"name":"@vladmandic/human","version":"2.8.1"} +2022-07-02 03:34:20 INFO:  Environment: {"profile":"production","config":".build.json","package":"package.json","tsconfig":true,"eslintrc":true,"git":true} +2022-07-02 03:34:20 INFO:  Toolchain: {"build":"0.7.3","esbuild":"0.14.47","typescript":"4.7.4","typedoc":"0.22.17","eslint":"8.18.0"} +2022-07-02 03:34:20 INFO:  Build: {"profile":"production","steps":["clean","compile","typings","typedoc","lint","changelog"]} +2022-07-02 03:34:20 STATE: Clean: {"locations":["dist/*","types/lib/*","typedoc/*"]} +2022-07-02 03:34:20 STATE: Compile: {"name":"tfjs/nodejs/cpu","format":"cjs","platform":"node","input":"tfjs/tf-node.ts","output":"dist/tfjs.esm.js","files":1,"inputBytes":102,"outputBytes":608} +2022-07-02 03:34:21 STATE: Compile: {"name":"human/nodejs/cpu","format":"cjs","platform":"node","input":"src/human.ts","output":"dist/human.node.js","files":73,"inputBytes":643891,"outputBytes":301229} +2022-07-02 03:34:21 STATE: Compile: {"name":"tfjs/nodejs/gpu","format":"cjs","platform":"node","input":"tfjs/tf-node-gpu.ts","output":"dist/tfjs.esm.js","files":1,"inputBytes":110,"outputBytes":612} +2022-07-02 03:34:21 STATE: Compile: {"name":"human/nodejs/gpu","format":"cjs","platform":"node","input":"src/human.ts","output":"dist/human.node-gpu.js","files":73,"inputBytes":643895,"outputBytes":301233} +2022-07-02 03:34:21 STATE: Compile: {"name":"tfjs/nodejs/wasm","format":"cjs","platform":"node","input":"tfjs/tf-node-wasm.ts","output":"dist/tfjs.esm.js","files":1,"inputBytes":149,"outputBytes":664} +2022-07-02 03:34:21 STATE: Compile: {"name":"human/nodejs/wasm","format":"cjs","platform":"node","input":"src/human.ts","output":"dist/human.node-wasm.js","files":73,"inputBytes":643947,"outputBytes":301283} +2022-07-02 03:34:21 STATE: Compile: {"name":"tfjs/browser/version","format":"esm","platform":"browser","input":"tfjs/tf-version.ts","output":"dist/tfjs.version.js","files":1,"inputBytes":1069,"outputBytes":371} +2022-07-02 03:34:21 STATE: Compile: {"name":"tfjs/browser/esm/nobundle","format":"esm","platform":"browser","input":"tfjs/tf-browser.ts","output":"dist/tfjs.esm.js","files":2,"inputBytes":1045,"outputBytes":596} +2022-07-02 03:34:21 STATE: Compile: {"name":"human/browser/esm/nobundle","format":"esm","platform":"browser","input":"src/human.ts","output":"dist/human.esm-nobundle.js","files":73,"inputBytes":643879,"outputBytes":300126} +2022-07-02 03:34:21 STATE: Compile: {"name":"tfjs/browser/esm/custom","format":"esm","platform":"browser","input":"tfjs/tf-custom.ts","output":"dist/tfjs.esm.js","files":1,"inputBytes":110,"outputBytes":1353541} +2022-07-02 03:34:21 STATE: Compile: {"name":"human/browser/iife/bundle","format":"iife","platform":"browser","input":"src/human.ts","output":"dist/human.js","files":73,"inputBytes":1996824,"outputBytes":1652733} +2022-07-02 03:34:21 STATE: Compile: {"name":"human/browser/esm/bundle","format":"esm","platform":"browser","input":"src/human.ts","output":"dist/human.esm.js","files":73,"inputBytes":1996824,"outputBytes":2140101} +2022-07-02 03:34:36 STATE: Typings: {"input":"src/human.ts","output":"types/lib","files":116} +2022-07-02 03:34:44 STATE: TypeDoc: {"input":"src/human.ts","output":"typedoc","objects":73,"generated":true} +2022-07-02 03:34:44 STATE: Compile: {"name":"demo/typescript","format":"esm","platform":"browser","input":"demo/typescript/index.ts","output":"demo/typescript/index.js","files":1,"inputBytes":6324,"outputBytes":3070} +2022-07-02 03:34:44 STATE: Compile: {"name":"demo/faceid","format":"esm","platform":"browser","input":"demo/faceid/index.ts","output":"demo/faceid/index.js","files":2,"inputBytes":15174,"outputBytes":7833} +2022-07-02 03:35:04 STATE: Lint: {"locations":["*.json","src/**/*.ts","test/**/*.js","demo/**/*.js"],"files":106,"errors":0,"warnings":0} +2022-07-02 03:35:05 STATE: ChangeLog: {"repository":"https://github.com/vladmandic/human","branch":"main","output":"CHANGELOG.md"} +2022-07-02 03:35:05 INFO:  Done...