mirror of https://github.com/vladmandic/human
update demos
parent
3a3e2eb067
commit
ac1522386b
|
@ -6,8 +6,11 @@ const fs = require('fs');
|
||||||
const process = require('process');
|
const process = require('process');
|
||||||
const log = require('@vladmandic/pilogger');
|
const log = require('@vladmandic/pilogger');
|
||||||
const canvas = require('canvas');
|
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
|
const config = { // just enable all and leave default settings
|
||||||
debug: false,
|
debug: false,
|
||||||
|
@ -68,6 +71,7 @@ async function main() {
|
||||||
const outputCanvas = new canvas.Canvas(inputImage.width, inputImage.height); // create canvas
|
const outputCanvas = new canvas.Canvas(inputImage.width, inputImage.height); // create canvas
|
||||||
const outputCtx = outputCanvas.getContext('2d');
|
const outputCtx = outputCanvas.getContext('2d');
|
||||||
outputCtx.drawImage(result.canvas || inputImage, 0, 0); // draw input image onto canvas
|
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
|
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
|
const outFile = fs.createWriteStream(output); // write canvas to new image file
|
||||||
outFile.on('finish', () => log.state('Output image:', output, outputCanvas.width, outputCanvas.height));
|
outFile.on('finish', () => log.state('Output image:', output, outputCanvas.width, outputCanvas.height));
|
||||||
|
|
|
@ -8,17 +8,14 @@ const process = require('process');
|
||||||
|
|
||||||
let fetch; // fetch is dynamically imported later
|
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 import/no-extraneous-dependencies, no-unused-vars, @typescript-eslint/no-unused-vars
|
||||||
// eslint-disable-next-line 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 tf = require('@tensorflow/tfjs-node'); // or const tf = require('@tensorflow/tfjs-node-gpu');
|
// 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
|
||||||
// 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;
|
|
||||||
|
|
||||||
let human = null;
|
let human = null;
|
||||||
|
|
||||||
const myConfig = {
|
const myConfig = {
|
||||||
backend: 'tensorflow',
|
|
||||||
modelBasePath: 'file://models/',
|
modelBasePath: 'file://models/',
|
||||||
debug: false,
|
debug: false,
|
||||||
async: true,
|
async: true,
|
||||||
|
@ -61,32 +58,34 @@ async function detect(input) {
|
||||||
async function main() {
|
async function main() {
|
||||||
log.header();
|
log.header();
|
||||||
|
|
||||||
human = new Human(myConfig);
|
human = new Human.Human(myConfig);
|
||||||
|
|
||||||
human.events.addEventListener('warmup', () => {
|
if (human.events) {
|
||||||
log.info('Event Warmup');
|
human.events.addEventListener('warmup', () => {
|
||||||
});
|
log.info('Event Warmup');
|
||||||
|
});
|
||||||
|
|
||||||
human.events.addEventListener('load', () => {
|
human.events.addEventListener('load', () => {
|
||||||
const loaded = Object.keys(human.models).filter((a) => human.models[a]);
|
const loaded = Object.keys(human.models).filter((a) => human.models[a]);
|
||||||
log.info('Event Loaded:', loaded, human.tf.engine().memory());
|
log.info('Event Loaded:', loaded, human.tf.engine().memory());
|
||||||
});
|
});
|
||||||
|
|
||||||
human.events.addEventListener('image', () => {
|
human.events.addEventListener('image', () => {
|
||||||
log.info('Event Image:', human.process.tensor.shape);
|
log.info('Event Image:', human.process.tensor.shape);
|
||||||
});
|
});
|
||||||
|
|
||||||
human.events.addEventListener('detect', () => {
|
human.events.addEventListener('detect', () => {
|
||||||
log.data('Event Detected:');
|
log.data('Event Detected:');
|
||||||
const persons = human.result.persons;
|
const persons = human.result.persons;
|
||||||
for (let i = 0; i < persons.length; i++) {
|
for (let i = 0; i < persons.length; i++) {
|
||||||
const face = persons[i].face;
|
const face = persons[i].face;
|
||||||
const faceTxt = face ? `score:${face.score} age:${face.age} gender:${face.gender} iris:${face.iris}` : null;
|
const faceTxt = face ? `score:${face.score} age:${face.age} gender:${face.gender} iris:${face.iris}` : null;
|
||||||
const body = persons[i].body;
|
const body = persons[i].body;
|
||||||
const bodyTxt = body ? `score:${body.score} keypoints:${body.keypoints?.length}` : null;
|
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}`);
|
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
|
await human.tf.ready(); // wait until tf is ready
|
||||||
|
|
||||||
|
|
|
@ -5,14 +5,15 @@
|
||||||
const log = require('@vladmandic/pilogger');
|
const log = require('@vladmandic/pilogger');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const process = require('process');
|
const process = require('process');
|
||||||
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
|
|
||||||
const tf = require('@tensorflow/tfjs-node');
|
// eslint-disable-next-line import/no-extraneous-dependencies, no-unused-vars, @typescript-eslint/no-unused-vars
|
||||||
const Human = require('../../dist/human.node.js').default;
|
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;
|
let human = null;
|
||||||
|
|
||||||
const myConfig = {
|
const myConfig = {
|
||||||
backend: 'tensorflow',
|
|
||||||
modelBasePath: 'file://models/',
|
modelBasePath: 'file://models/',
|
||||||
debug: true,
|
debug: true,
|
||||||
face: { emotion: { enabled: false } },
|
face: { emotion: { enabled: false } },
|
||||||
|
@ -22,7 +23,7 @@ const myConfig = {
|
||||||
};
|
};
|
||||||
|
|
||||||
async function init() {
|
async function init() {
|
||||||
human = new Human(myConfig);
|
human = new Human.Human(myConfig);
|
||||||
await human.tf.ready();
|
await human.tf.ready();
|
||||||
log.info('Human:', human.version);
|
log.info('Human:', human.version);
|
||||||
await human.load();
|
await human.load();
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
const fs = require('fs');
|
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) {
|
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.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
|
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
|
const buffer = fs.readFileSync(inputFile); // read file data into buffer
|
||||||
|
|
|
@ -16,18 +16,17 @@ const log = require('@vladmandic/pilogger');
|
||||||
// @ts-ignore pipe2jpeg is not installed by default
|
// @ts-ignore pipe2jpeg is not installed by default
|
||||||
// eslint-disable-next-line node/no-missing-require
|
// eslint-disable-next-line node/no-missing-require
|
||||||
const Pipe2Jpeg = require('pipe2jpeg');
|
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
|
// eslint-disable-next-line import/no-extraneous-dependencies, no-unused-vars, @typescript-eslint/no-unused-vars
|
||||||
const tf = require('@tensorflow/tfjs-node'); // or const tf = require('@tensorflow/tfjs-node-gpu');
|
const tf = require('@tensorflow/tfjs-node'); // in nodejs environments tfjs-node is required to be loaded before human
|
||||||
// load specific version of Human library that matches TensorFlow mode
|
// 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').default; // or const Human = require('../dist/human.node-gpu.js').default;
|
const Human = require('../../dist/human.node.js'); // use this when using human in dev mode
|
||||||
|
|
||||||
let count = 0; // counter
|
let count = 0; // counter
|
||||||
let busy = false; // busy flag
|
let busy = false; // busy flag
|
||||||
const inputFile = './test.mp4';
|
const inputFile = './test.mp4';
|
||||||
|
|
||||||
const humanConfig = {
|
const humanConfig = {
|
||||||
backend: 'tensorflow',
|
|
||||||
modelBasePath: 'file://models/',
|
modelBasePath: 'file://models/',
|
||||||
debug: false,
|
debug: false,
|
||||||
async: true,
|
async: true,
|
||||||
|
@ -45,7 +44,7 @@ const humanConfig = {
|
||||||
object: { enabled: false },
|
object: { enabled: false },
|
||||||
};
|
};
|
||||||
|
|
||||||
const human = new Human(humanConfig);
|
const human = new Human.Human(humanConfig);
|
||||||
const pipe2jpeg = new Pipe2Jpeg();
|
const pipe2jpeg = new Pipe2Jpeg();
|
||||||
|
|
||||||
const ffmpegParams = [
|
const ffmpegParams = [
|
||||||
|
@ -65,10 +64,7 @@ const ffmpegParams = [
|
||||||
async function process(jpegBuffer) {
|
async function process(jpegBuffer) {
|
||||||
if (busy) return; // skip processing if busy
|
if (busy) return; // skip processing if busy
|
||||||
busy = true;
|
busy = true;
|
||||||
const decoded = tf.node.decodeJpeg(jpegBuffer, 3); // decode jpeg buffer to raw tensor
|
const tensor = human.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);
|
|
||||||
|
|
||||||
log.state('input frame:', ++count, 'size:', jpegBuffer.length, 'decoded shape:', tensor.shape);
|
log.state('input frame:', ++count, 'size:', jpegBuffer.length, 'decoded shape:', tensor.shape);
|
||||||
const res = await human.detect(tensor);
|
const res = await human.detect(tensor);
|
||||||
log.data('gesture', JSON.stringify(res.gesture));
|
log.data('gesture', JSON.stringify(res.gesture));
|
||||||
|
|
|
@ -11,11 +11,11 @@ const log = require('@vladmandic/pilogger');
|
||||||
// @ts-ignore node-webcam is not installed by default
|
// @ts-ignore node-webcam is not installed by default
|
||||||
// eslint-disable-next-line node/no-missing-require
|
// eslint-disable-next-line node/no-missing-require
|
||||||
const nodeWebCam = require('node-webcam');
|
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
|
// eslint-disable-next-line import/no-extraneous-dependencies, no-unused-vars, @typescript-eslint/no-unused-vars
|
||||||
const tf = require('@tensorflow/tfjs-node'); // or const tf = require('@tensorflow/tfjs-node-gpu');
|
const tf = require('@tensorflow/tfjs-node'); // in nodejs environments tfjs-node is required to be loaded before human
|
||||||
// load specific version of Human library that matches TensorFlow mode
|
// 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').default; // or const Human = require('../dist/human.node-gpu.js').default;
|
const Human = require('../../dist/human.node.js'); // use this when using human in dev mode
|
||||||
|
|
||||||
// options for node-webcam
|
// 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
|
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
|
// options for human
|
||||||
const optionsHuman = {
|
const optionsHuman = {
|
||||||
backend: 'tensorflow',
|
|
||||||
modelBasePath: 'file://models/',
|
modelBasePath: 'file://models/',
|
||||||
};
|
};
|
||||||
const human = new Human(optionsHuman);
|
const human = new Human.Human(optionsHuman);
|
||||||
|
|
||||||
function buffer2tensor(buffer) {
|
function buffer2tensor(buffer) {
|
||||||
return human.tf.tidy(() => {
|
return human.tf.tidy(() => {
|
||||||
|
@ -67,8 +66,8 @@ async function detect() {
|
||||||
if (result && result.face && result.face.length > 0) {
|
if (result && result.face && result.face.length > 0) {
|
||||||
for (let i = 0; i < result.face.length; i++) {
|
for (let i = 0; i < result.face.length; i++) {
|
||||||
const face = result.face[i];
|
const face = result.face[i];
|
||||||
const emotion = face.emotion.reduce((prev, curr) => (prev.score > curr.score ? prev : curr));
|
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}`);
|
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 {
|
} else {
|
||||||
log.data(' Face: N/A');
|
log.data(' Face: N/A');
|
||||||
|
|
|
@ -9,12 +9,10 @@ const process = require('process');
|
||||||
|
|
||||||
let fetch; // fetch is dynamically imported later
|
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 import/no-extraneous-dependencies, no-unused-vars, @typescript-eslint/no-unused-vars
|
||||||
// eslint-disable-next-line 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 tf = require('@tensorflow/tfjs-node'); // or const tf = require('@tensorflow/tfjs-node-gpu');
|
// 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
|
||||||
// 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;
|
|
||||||
|
|
||||||
let human = null;
|
let human = null;
|
||||||
|
|
||||||
|
@ -45,7 +43,7 @@ const myConfig = {
|
||||||
|
|
||||||
async function init() {
|
async function init() {
|
||||||
// create instance of human
|
// create instance of human
|
||||||
human = new Human(myConfig);
|
human = new Human.Human(myConfig);
|
||||||
// wait until tf is ready
|
// wait until tf is ready
|
||||||
await human.tf.ready();
|
await human.tf.ready();
|
||||||
// pre-load models
|
// pre-load models
|
||||||
|
|
Loading…
Reference in New Issue