human/demo/nodejs/node-simple.js

33 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-08-20 15:38:08 +02:00
/**
* Human simple demo for NodeJS
*/
2021-11-18 16:10:06 +01:00
const fs = require('fs');
2022-05-22 14:50:51 +02:00
const process = require('process');
2022-01-01 14:13:04 +01:00
2022-08-21 19:34:51 +02:00
// in nodejs environments tfjs-node is required to be loaded before human
const tf = require('@tensorflow/tfjs-node'); // eslint-disable-line node/no-unpublished-require
2022-07-18 14:22:19 +02:00
// const human = require('@vladmandic/human'); // use this when human is installed as module (majority of use cases)
2022-01-01 14:13:04 +01:00
const Human = require('../../dist/human.node.js'); // use this when using human in dev mode
2021-11-18 16:10:06 +01:00
2022-05-22 14:50:51 +02:00
const humanConfig = {
// add any custom config here
2022-09-19 16:46:11 +02:00
debug: true,
2023-02-28 21:03:46 +01:00
body: { enabled: false },
2022-05-22 14:50:51 +02:00
};
async function detect(inputFile) {
const human = new Human.Human(humanConfig); // create instance of human using default configuration
2022-08-20 15:38:08 +02:00
console.log('Human:', human.version, 'TF:', tf.version_core); // eslint-disable-line no-console
2021-12-27 16:59:56 +01:00
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
2021-11-18 16:10:06 +01:00
const buffer = fs.readFileSync(inputFile); // read file data into buffer
const tensor = human.tf.node.decodeImage(buffer); // decode jpg data
2022-08-20 15:38:08 +02:00
console.log('loaded input file:', inputFile, 'resolution:', tensor.shape); // eslint-disable-line no-console
2021-11-18 16:10:06 +01:00
const result = await human.detect(tensor); // run detection; will initialize backend and on-demand load models
2022-08-20 15:38:08 +02:00
console.log(result); // eslint-disable-line no-console
2021-11-18 16:10:06 +01:00
}
2022-05-22 14:50:51 +02:00
if (process.argv.length === 3) detect(process.argv[2]); // if input file is provided as cmdline parameter use it
else detect('samples/in/ai-body.jpg'); // else use built-in test inputfile