human/demo/node.js

66 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-10-14 17:43:33 +02:00
const tf = require('@tensorflow/tfjs-node');
2020-10-14 02:52:30 +02:00
const fs = require('fs');
const process = require('process');
const console = require('console');
2020-10-16 16:12:12 +02:00
const human = require('..'); // this resolves to project root which is '@vladmandic/human'
2020-10-14 02:52:30 +02:00
const logger = new console.Console({
stdout: process.stdout,
stderr: process.stderr,
ignoreErrors: true,
groupIndentation: 2,
inspectOptions: {
showHidden: true,
depth: 5,
colors: true,
showProxy: true,
maxArrayLength: 1024,
maxStringLength: 10240,
breakLength: 200,
compact: 64,
sorted: false,
getters: true,
},
});
const config = {
2020-10-16 16:12:12 +02:00
backend: 'tensorflow',
console: true,
2020-10-14 02:52:30 +02:00
face: {
2020-10-18 14:07:45 +02:00
detector: { modelPath: 'file://models/blazeface/back/model.json' },
mesh: { modelPath: 'file://models/facemesh/model.json' },
iris: { modelPath: 'file://models/iris/model.json' },
age: { modelPath: 'file://models/ssrnet-age/imdb/model.json' },
gender: { modelPath: 'file://models/ssrnet-gender/imdb/model.json' },
emotion: { modelPath: 'file://models/emotion/model.json' },
2020-10-14 02:52:30 +02:00
},
2020-10-18 14:07:45 +02:00
body: { modelPath: 'file://models/posenet/model.json' },
2020-10-14 02:52:30 +02:00
hand: {
detector: { anchors: 'file://models/handdetect/anchors.json', modelPath: 'file://models/handdetect/model.json' },
skeleton: { modelPath: 'file://models/handskeleton/model.json' },
},
};
async function detect(input, output) {
await tf.setBackend('tensorflow');
await tf.ready();
logger.info('TFJS Flags:', tf.env().features);
logger.log('Loading:', input);
const buffer = fs.readFileSync(input);
const image = tf.node.decodeImage(buffer);
logger.log('Processing:', image.shape);
const result = await human.detect(image, config);
2020-10-14 17:43:33 +02:00
image.dispose();
2020-10-14 02:52:30 +02:00
logger.log(result);
// Draw detected data and save processed image
2020-10-17 12:30:00 +02:00
logger.log('TODO Saving:', output);
2020-10-14 02:52:30 +02:00
}
async function main() {
if (process.argv.length !== 4) logger.error('Parameters: <input image> <output image>');
else if (!fs.existsSync(process.argv[2])) logger.error(`File not found: ${process.argv[2]}`);
else detect(process.argv[2], process.argv[3]);
}
main();