human/demo/node.js

154 lines
5.0 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');
2021-04-19 17:15:29 +02:00
const fetch = require('node-fetch').default;
2021-03-12 18:54:08 +01:00
// for NodeJS, `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');
2021-03-12 18:54:08 +01:00
// 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 = {
2021-04-01 15:24:56 +02:00
backend: 'tensorflow',
2021-04-09 14:07:58 +02:00
modelBasePath: 'file://models/',
2021-04-01 15:24:56 +02:00
debug: true,
videoOptimized: false,
2021-02-06 23:41:53 +01:00
async: false,
2020-10-14 02:52:30 +02:00
face: {
2021-03-04 16:33:08 +01:00
enabled: true,
2021-04-09 14:07:58 +02:00
detector: { enabled: true, rotation: false },
mesh: { enabled: true },
iris: { enabled: true },
description: { enabled: true },
emotion: { enabled: true },
2020-10-14 02:52:30 +02:00
},
hand: {
2021-03-04 16:33:08 +01:00
enabled: true,
2020-10-14 02:52:30 +02:00
},
2021-04-09 14:07:58 +02:00
// body: { modelPath: 'efficientpose.json', enabled: true },
// body: { modelPath: 'blazepose.json', enabled: true },
body: { enabled: true },
object: { enabled: true },
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
2021-03-04 16:33:08 +01:00
log.info('Human:', human.version);
log.info('Active Configuration', human.config);
await human.load();
2021-03-04 16:33:08 +01:00
const loaded = Object.keys(human.models).filter((a) => human.models[a]);
log.info('Loaded:', loaded);
log.info('Memory state:', human.tf.engine().memory());
2021-01-30 19:23:07 +01:00
}
async function detect(input) {
// read input image file and create tensor to be used for processing
2021-04-19 17:15:29 +02:00
let buffer;
log.info('Loading image:', input);
if (input.startsWith('http')) {
const res = await fetch(input);
if (res && res.ok) buffer = await res.buffer();
2021-04-19 17:20:24 +02:00
else log.error('Invalid image URL:', input, res.status, res.statusText, res.headers.get('content-type'));
2021-04-19 17:15:29 +02:00
} else {
buffer = fs.readFileSync(input);
}
2021-04-19 17:17:55 +02:00
// decode image using tfjs-node so we don't need external depenencies
2021-04-19 17:36:44 +02:00
// can also be done using canvas.js or some other 3rd party image library
const decoded = human.tf.node.decodeImage(buffer);
const casted = decoded.toFloat();
2021-04-19 17:36:44 +02:00
const tensor = casted.expandDims(0);
decoded.dispose();
casted.dispose();
2021-04-19 17:17:55 +02:00
// image shape contains image dimensions and depth
2021-04-19 17:36:44 +02:00
log.state('Processing:', tensor.shape);
2021-04-19 17:17:55 +02:00
// run actual detection
2021-04-19 17:36:44 +02:00
const result = await human.detect(tensor, myConfig);
2021-04-19 17:17:55 +02:00
// dispose image tensor as we no longer need it
2021-04-19 17:36:44 +02:00
tensor.dispose();
2021-04-19 17:17:55 +02:00
// print data to console
2021-04-01 15:24:56 +02:00
log.data('Results:');
2021-04-19 17:36:44 +02:00
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(` Face: #${i} boxConfidence:${face.boxConfidence} faceConfidence:${face.boxConfidence} age:${face.age} genderConfidence:${face.genderConfidence} gender:${face.gender} emotionScore:${emotion.score} emotion:${emotion.emotion} iris:${face.iris}`);
}
2021-04-01 15:24:56 +02:00
}
2021-04-19 17:36:44 +02:00
if (result && result.body && result.body.length > 0) {
for (let i = 0; i < result.body.length; i++) {
const body = result.body[i];
log.data(` Body: #${i} score:${body.score}`);
}
} else {
log.data(' Body: N/A');
2021-04-01 15:24:56 +02:00
}
2021-04-19 17:36:44 +02:00
if (result && result.hand && result.hand.length > 0) {
for (let i = 0; i < result.hand.length; i++) {
const hand = result.hand[i];
log.data(` Hand: #${i} confidence:${hand.confidence}`);
}
} else {
log.data(' Hand: N/A');
2021-04-01 15:24:56 +02:00
}
2021-04-19 17:36:44 +02:00
if (result && result.gesture && result.gesture.length > 0) {
for (let i = 0; i < result.gesture.length; i++) {
const [key, val] = Object.entries(result.gesture[i]);
log.data(` Gesture: ${key[0]}#${key[1]} gesture:${val[1]}`);
}
} else {
log.data(' Gesture: N/A');
2021-04-01 15:24:56 +02:00
}
2021-04-19 17:36:44 +02:00
if (result && result.object && result.object.length > 0) {
for (let i = 0; i < result.object.length; i++) {
const object = result.object[i];
log.data(` Object: #${i} score:${object.score} label:${object.label}`);
}
} else {
log.data(' Object: N/A');
2021-04-01 15:24:56 +02:00
}
2021-03-10 00:32:35 +01:00
return result;
2020-10-14 02:52:30 +02:00
}
2021-01-30 19:23:07 +01:00
async function test() {
2021-03-06 16:38:04 +01:00
// test with embedded full body image
let result;
2021-02-06 23:41:53 +01:00
log.state('Processing embedded warmup image: face');
myConfig.warmup = 'face';
2021-03-06 16:38:04 +01:00
result = await human.warmup(myConfig);
2021-02-08 17:39:09 +01:00
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-03-06 16:38:04 +01:00
result = await human.warmup(myConfig);
2021-03-10 00:32:35 +01:00
// no need to print results as they are printed to console during detection from within the library due to human.config.debug set
return result;
2021-01-30 19:23:07 +01:00
}
2020-10-14 02:52:30 +02:00
async function main() {
2021-03-06 16:38:04 +01:00
log.header();
2021-03-04 16:33:08 +01:00
log.info('Current folder:', process.env.PWD);
2021-01-30 19:23:07 +01:00
await init();
if (process.argv.length !== 3) {
log.warn('Parameters: <input image> missing');
await test();
2021-04-19 17:15:29 +02:00
} else if (!fs.existsSync(process.argv[2]) && !process.argv[2].startsWith('http')) {
2021-01-30 19:23:07 +01:00
log.error(`File not found: ${process.argv[2]}`);
} else {
await detect(process.argv[2]);
}
2020-10-14 02:52:30 +02:00
}
main();