human/demo/node.js

89 lines
2.9 KiB
JavaScript
Raw Normal View History

const log = require('@vladmandic/pilogger');
2020-10-14 02:52:30 +02:00
const fs = require('fs');
const process = require('process');
// for Node, `tfjs-node` or `tfjs-node-gpu` should be loaded before using Human
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;
2020-10-14 02:52:30 +02:00
2021-01-30 19:23:07 +01:00
let human = null;
const myConfig = {
2020-10-16 16:12:12 +02:00
backend: 'tensorflow',
console: true,
videoOptimized: false,
2021-02-06 23:41:53 +01:00
async: false,
2020-10-14 02:52:30 +02:00
face: {
2021-02-28 13:38:09 +01:00
// detector: { modelPath: 'file://models/faceboxes.json' },
detector: { modelPath: 'file://models/blazeface-back.json' }, // cannot use blazeface in nodejs due to missing required kernel function in tfjs-node
2020-11-03 15:34:36 +01:00
mesh: { modelPath: 'file://models/facemesh.json' },
iris: { modelPath: 'file://models/iris.json' },
age: { modelPath: 'file://models/age-ssrnet-imdb.json' },
2021-02-24 15:57:33 +01:00
gender: { modelPath: 'file://models/gender.json' },
emotion: { modelPath: 'file://models/emotion-large.json' },
2020-10-14 02:52:30 +02:00
},
2020-11-03 15:34:36 +01:00
body: { modelPath: 'file://models/posenet.json' },
2020-10-14 02:52:30 +02:00
hand: {
2020-11-03 15:34:36 +01:00
detector: { modelPath: 'file://models/handdetect.json' },
skeleton: { modelPath: 'file://models/handskeleton.json' },
2020-10-14 02:52:30 +02:00
},
};
2021-01-30 19:23:07 +01:00
async function init() {
// wait until tf is ready
2020-10-14 02:52:30 +02:00
await tf.ready();
// create instance of human
2021-01-30 19:23:07 +01:00
human = new Human(myConfig);
// pre-load models
await human.load();
2021-01-30 19:23:07 +01:00
}
async function detect(input) {
// read input image file and create tensor to be used for processing
2020-10-14 02:52:30 +02:00
const buffer = fs.readFileSync(input);
const decoded = human.tf.node.decodeImage(buffer);
const casted = decoded.toFloat();
const image = casted.expandDims(0);
decoded.dispose();
casted.dispose();
// image shape contains image dimensions and depth
log.state('Processing:', image.shape);
// run actual detection
const result = await human.detect(image, myConfig);
// dispose image tensor as we no longer need it
2020-10-14 17:43:33 +02:00
image.dispose();
// print data to console
log.data(result);
2020-10-14 02:52:30 +02:00
}
2021-01-30 19:23:07 +01:00
async function test() {
2021-02-08 17:39:09 +01:00
// test with embedded face image
2021-02-06 23:41:53 +01:00
log.state('Processing embedded warmup image: face');
myConfig.warmup = 'face';
const resultFace = await human.warmup(myConfig);
2021-02-08 17:39:09 +01:00
log.data('Face: ', resultFace.face);
// test with embedded full body image
2021-02-06 23:41:53 +01:00
log.state('Processing embedded warmup image: full');
2021-01-30 19:23:07 +01:00
myConfig.warmup = 'full';
2021-02-06 23:41:53 +01:00
const resultFull = await human.warmup(myConfig);
2021-02-08 17:39:09 +01:00
log.data('Body:', resultFull.body);
log.data('Hand:', resultFull.hand);
log.data('Gesture:', resultFull.gesture);
2021-01-30 19:23:07 +01:00
}
2020-10-14 02:52:30 +02:00
async function main() {
2020-12-23 19:55:22 +01:00
log.info('NodeJS:', process.version);
2021-01-30 19:23:07 +01:00
await init();
if (process.argv.length !== 3) {
log.warn('Parameters: <input image> missing');
await test();
} else if (!fs.existsSync(process.argv[2])) {
log.error(`File not found: ${process.argv[2]}`);
} else {
await detect(process.argv[2]);
}
2020-10-14 02:52:30 +02:00
}
main();