From 2b4fe6c78e39042c78f96b79398ee7cde0f1dfdf Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Sat, 1 Jan 2022 08:13:04 -0500 Subject: [PATCH] update demos --- demo/nodejs/node-canvas.js | 8 +++-- demo/nodejs/node-event.js | 57 +++++++++++++++++----------------- demo/nodejs/node-similarity.js | 11 ++++--- demo/nodejs/node-simple.js | 8 +++-- demo/nodejs/node-video.js | 18 +++++------ demo/nodejs/node-webcam.js | 17 +++++----- demo/nodejs/node.js | 12 +++---- 7 files changed, 66 insertions(+), 65 deletions(-) diff --git a/demo/nodejs/node-canvas.js b/demo/nodejs/node-canvas.js index fd0b20f2..c1359199 100644 --- a/demo/nodejs/node-canvas.js +++ b/demo/nodejs/node-canvas.js @@ -6,8 +6,11 @@ const fs = require('fs'); const process = require('process'); const log = require('@vladmandic/pilogger'); const canvas = require('canvas'); -require('@tensorflow/tfjs-node'); // for nodejs, `tfjs-node` or `tfjs-node-gpu` should be loaded before using Human -const Human = require('../../dist/human.node.js'); // this is 'const Human = require('../dist/human.node-gpu.js').default;' + +// eslint-disable-next-line import/no-extraneous-dependencies, no-unused-vars, @typescript-eslint/no-unused-vars +const tf = require('@tensorflow/tfjs-node'); // in nodejs environments tfjs-node is required to be loaded before human +// const faceapi = require('@vladmandic/face-api'); // use this when human is installed as module (majority of use cases) +const Human = require('../../dist/human.node.js'); // use this when using human in dev mode const config = { // just enable all and leave default settings debug: false, @@ -68,6 +71,7 @@ async function main() { const outputCanvas = new canvas.Canvas(inputImage.width, inputImage.height); // create canvas const outputCtx = outputCanvas.getContext('2d'); outputCtx.drawImage(result.canvas || inputImage, 0, 0); // draw input image onto canvas + // @ts-ignore canvas is not checked for typedefs human.draw.all(outputCanvas, result); // use human build-in method to draw results as overlays on canvas const outFile = fs.createWriteStream(output); // write canvas to new image file outFile.on('finish', () => log.state('Output image:', output, outputCanvas.width, outputCanvas.height)); diff --git a/demo/nodejs/node-event.js b/demo/nodejs/node-event.js index 79f10b3e..7d9485f2 100644 --- a/demo/nodejs/node-event.js +++ b/demo/nodejs/node-event.js @@ -8,17 +8,14 @@ const process = require('process'); let fetch; // fetch is dynamically imported later -// for NodeJS, `tfjs-node` or `tfjs-node-gpu` should be loaded before using Human -// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars -const tf = require('@tensorflow/tfjs-node'); // or const tf = require('@tensorflow/tfjs-node-gpu'); - -// load specific version of Human library that matches TensorFlow mode -const Human = require('../../dist/human.node.js').default; // or const Human = require('../dist/human.node-gpu.js').default; +// eslint-disable-next-line import/no-extraneous-dependencies, no-unused-vars, @typescript-eslint/no-unused-vars +const tf = require('@tensorflow/tfjs-node'); // in nodejs environments tfjs-node is required to be loaded before human +// const faceapi = require('@vladmandic/face-api'); // use this when human is installed as module (majority of use cases) +const Human = require('../../dist/human.node.js'); // use this when using human in dev mode let human = null; const myConfig = { - backend: 'tensorflow', modelBasePath: 'file://models/', debug: false, async: true, @@ -61,32 +58,34 @@ async function detect(input) { async function main() { log.header(); - human = new Human(myConfig); + human = new Human.Human(myConfig); - human.events.addEventListener('warmup', () => { - log.info('Event Warmup'); - }); + if (human.events) { + human.events.addEventListener('warmup', () => { + log.info('Event Warmup'); + }); - human.events.addEventListener('load', () => { - const loaded = Object.keys(human.models).filter((a) => human.models[a]); - log.info('Event Loaded:', loaded, human.tf.engine().memory()); - }); + human.events.addEventListener('load', () => { + const loaded = Object.keys(human.models).filter((a) => human.models[a]); + log.info('Event Loaded:', loaded, human.tf.engine().memory()); + }); - human.events.addEventListener('image', () => { - log.info('Event Image:', human.process.tensor.shape); - }); + human.events.addEventListener('image', () => { + log.info('Event Image:', human.process.tensor.shape); + }); - human.events.addEventListener('detect', () => { - log.data('Event Detected:'); - const persons = human.result.persons; - for (let i = 0; i < persons.length; i++) { - const face = persons[i].face; - const faceTxt = face ? `score:${face.score} age:${face.age} gender:${face.gender} iris:${face.iris}` : null; - const body = persons[i].body; - const bodyTxt = body ? `score:${body.score} keypoints:${body.keypoints?.length}` : null; - log.data(` #${i}: Face:${faceTxt} Body:${bodyTxt} LeftHand:${persons[i].hands.left ? 'yes' : 'no'} RightHand:${persons[i].hands.right ? 'yes' : 'no'} Gestures:${persons[i].gestures.length}`); - } - }); + human.events.addEventListener('detect', () => { + log.data('Event Detected:'); + const persons = human.result.persons; + for (let i = 0; i < persons.length; i++) { + const face = persons[i].face; + const faceTxt = face ? `score:${face.score} age:${face.age} gender:${face.gender} iris:${face.iris}` : null; + const body = persons[i].body; + const bodyTxt = body ? `score:${body.score} keypoints:${body.keypoints?.length}` : null; + log.data(` #${i}: Face:${faceTxt} Body:${bodyTxt} LeftHand:${persons[i].hands.left ? 'yes' : 'no'} RightHand:${persons[i].hands.right ? 'yes' : 'no'} Gestures:${persons[i].gestures.length}`); + } + }); + } await human.tf.ready(); // wait until tf is ready diff --git a/demo/nodejs/node-similarity.js b/demo/nodejs/node-similarity.js index 168e5f06..df607bd8 100644 --- a/demo/nodejs/node-similarity.js +++ b/demo/nodejs/node-similarity.js @@ -5,14 +5,15 @@ const log = require('@vladmandic/pilogger'); const fs = require('fs'); const process = require('process'); -// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars -const tf = require('@tensorflow/tfjs-node'); -const Human = require('../../dist/human.node.js').default; + +// eslint-disable-next-line import/no-extraneous-dependencies, no-unused-vars, @typescript-eslint/no-unused-vars +const tf = require('@tensorflow/tfjs-node'); // in nodejs environments tfjs-node is required to be loaded before human +// const faceapi = require('@vladmandic/face-api'); // use this when human is installed as module (majority of use cases) +const Human = require('../../dist/human.node.js'); // use this when using human in dev mode let human = null; const myConfig = { - backend: 'tensorflow', modelBasePath: 'file://models/', debug: true, face: { emotion: { enabled: false } }, @@ -22,7 +23,7 @@ const myConfig = { }; async function init() { - human = new Human(myConfig); + human = new Human.Human(myConfig); await human.tf.ready(); log.info('Human:', human.version); await human.load(); diff --git a/demo/nodejs/node-simple.js b/demo/nodejs/node-simple.js index 667e8309..f1c4f2a9 100644 --- a/demo/nodejs/node-simple.js +++ b/demo/nodejs/node-simple.js @@ -1,8 +1,12 @@ const fs = require('fs'); -const Human = require('../../dist/human.node.js').default; // this is same as `@vladmandic/human` but using relative paths + +// eslint-disable-next-line import/no-extraneous-dependencies, no-unused-vars, @typescript-eslint/no-unused-vars +const tf = require('@tensorflow/tfjs-node'); // in nodejs environments tfjs-node is required to be loaded before human +// const faceapi = require('@vladmandic/face-api'); // use this when human is installed as module (majority of use cases) +const Human = require('../../dist/human.node.js'); // use this when using human in dev mode async function main(inputFile) { - const human = new Human(); // create instance of human using default configuration + const human = new Human.Human(); // create instance of human using default configuration await human.load(); // optional as models would be loaded on-demand first time they are required await human.warmup(); // optional as model warmup is performed on-demand first time its executed const buffer = fs.readFileSync(inputFile); // read file data into buffer diff --git a/demo/nodejs/node-video.js b/demo/nodejs/node-video.js index 41431f75..f66fb4a1 100644 --- a/demo/nodejs/node-video.js +++ b/demo/nodejs/node-video.js @@ -16,18 +16,17 @@ const log = require('@vladmandic/pilogger'); // @ts-ignore pipe2jpeg is not installed by default // eslint-disable-next-line node/no-missing-require const Pipe2Jpeg = require('pipe2jpeg'); -// for NodeJS, `tfjs-node` or `tfjs-node-gpu` should be loaded before using Human -// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars -const tf = require('@tensorflow/tfjs-node'); // or const tf = require('@tensorflow/tfjs-node-gpu'); -// load specific version of Human library that matches TensorFlow mode -const Human = require('../../dist/human.node.js').default; // or const Human = require('../dist/human.node-gpu.js').default; + +// eslint-disable-next-line import/no-extraneous-dependencies, no-unused-vars, @typescript-eslint/no-unused-vars +const tf = require('@tensorflow/tfjs-node'); // in nodejs environments tfjs-node is required to be loaded before human +// const faceapi = require('@vladmandic/face-api'); // use this when human is installed as module (majority of use cases) +const Human = require('../../dist/human.node.js'); // use this when using human in dev mode let count = 0; // counter let busy = false; // busy flag const inputFile = './test.mp4'; const humanConfig = { - backend: 'tensorflow', modelBasePath: 'file://models/', debug: false, async: true, @@ -45,7 +44,7 @@ const humanConfig = { object: { enabled: false }, }; -const human = new Human(humanConfig); +const human = new Human.Human(humanConfig); const pipe2jpeg = new Pipe2Jpeg(); const ffmpegParams = [ @@ -65,10 +64,7 @@ const ffmpegParams = [ async function process(jpegBuffer) { if (busy) return; // skip processing if busy busy = true; - const decoded = tf.node.decodeJpeg(jpegBuffer, 3); // decode jpeg buffer to raw tensor - const tensor = tf.expandDims(decoded, 0); // almost all tf models use first dimension as batch number so we add it - tf.dispose(decoded); - + const tensor = human.tf.node.decodeJpeg(jpegBuffer, 3); // decode jpeg buffer to raw tensor log.state('input frame:', ++count, 'size:', jpegBuffer.length, 'decoded shape:', tensor.shape); const res = await human.detect(tensor); log.data('gesture', JSON.stringify(res.gesture)); diff --git a/demo/nodejs/node-webcam.js b/demo/nodejs/node-webcam.js index 70b1f094..53d409d1 100644 --- a/demo/nodejs/node-webcam.js +++ b/demo/nodejs/node-webcam.js @@ -11,11 +11,11 @@ const log = require('@vladmandic/pilogger'); // @ts-ignore node-webcam is not installed by default // eslint-disable-next-line node/no-missing-require const nodeWebCam = require('node-webcam'); -// for NodeJS, `tfjs-node` or `tfjs-node-gpu` should be loaded before using Human -// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars -const tf = require('@tensorflow/tfjs-node'); // or const tf = require('@tensorflow/tfjs-node-gpu'); -// load specific version of Human library that matches TensorFlow mode -const Human = require('../../dist/human.node.js').default; // or const Human = require('../dist/human.node-gpu.js').default; + +// eslint-disable-next-line import/no-extraneous-dependencies, no-unused-vars, @typescript-eslint/no-unused-vars +const tf = require('@tensorflow/tfjs-node'); // in nodejs environments tfjs-node is required to be loaded before human +// const faceapi = require('@vladmandic/face-api'); // use this when human is installed as module (majority of use cases) +const Human = require('../../dist/human.node.js'); // use this when using human in dev mode // options for node-webcam const tempFile = 'webcam-snap'; // node-webcam requires writting snapshot to a file, recommended to use tmpfs to avoid excessive disk writes @@ -27,10 +27,9 @@ const camera = nodeWebCam.create(optionsCamera); // options for human const optionsHuman = { - backend: 'tensorflow', modelBasePath: 'file://models/', }; -const human = new Human(optionsHuman); +const human = new Human.Human(optionsHuman); function buffer2tensor(buffer) { return human.tf.tidy(() => { @@ -67,8 +66,8 @@ async function detect() { if (result && result.face && result.face.length > 0) { for (let i = 0; i < result.face.length; i++) { const face = result.face[i]; - const emotion = face.emotion.reduce((prev, curr) => (prev.score > curr.score ? prev : curr)); - log.data(`detected face: #${i} boxScore:${face.boxScore} faceScore:${face.faceScore} age:${face.age} genderScore:${face.genderScore} gender:${face.gender} emotionScore:${emotion.score} emotion:${emotion.emotion} iris:${face.iris}`); + const emotion = face.emotion?.reduce((prev, curr) => (prev.score > curr.score ? prev : curr)); + log.data(`detected face: #${i} boxScore:${face.boxScore} faceScore:${face.faceScore} age:${face.age} genderScore:${face.genderScore} gender:${face.gender} emotionScore:${emotion?.score} emotion:${emotion?.emotion} iris:${face.iris}`); } } else { log.data(' Face: N/A'); diff --git a/demo/nodejs/node.js b/demo/nodejs/node.js index f187ba04..db282a12 100644 --- a/demo/nodejs/node.js +++ b/demo/nodejs/node.js @@ -9,12 +9,10 @@ const process = require('process'); let fetch; // fetch is dynamically imported later -// for NodeJS, `tfjs-node` or `tfjs-node-gpu` should be loaded before using Human -// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars -const tf = require('@tensorflow/tfjs-node'); // or const tf = require('@tensorflow/tfjs-node-gpu'); - -// load specific version of Human library that matches TensorFlow mode -const Human = require('../../dist/human.node.js').default; // or const Human = require('../dist/human.node-gpu.js').default; +// eslint-disable-next-line import/no-extraneous-dependencies, no-unused-vars, @typescript-eslint/no-unused-vars +const tf = require('@tensorflow/tfjs-node'); // in nodejs environments tfjs-node is required to be loaded before human +// const faceapi = require('@vladmandic/face-api'); // use this when human is installed as module (majority of use cases) +const Human = require('../../dist/human.node.js'); // use this when using human in dev mode let human = null; @@ -45,7 +43,7 @@ const myConfig = { async function init() { // create instance of human - human = new Human(myConfig); + human = new Human.Human(myConfig); // wait until tf is ready await human.tf.ready(); // pre-load models