2020-10-12 16:08:00 +02:00
|
|
|
const tf = require('@tensorflow/tfjs');
|
2020-10-14 17:43:33 +02:00
|
|
|
const facemesh = require('./facemesh/facemesh.js');
|
|
|
|
const ssrnet = require('./ssrnet/ssrnet.js');
|
2020-10-15 00:22:38 +02:00
|
|
|
const emotion = require('./emotion/emotion.js');
|
2020-10-14 17:43:33 +02:00
|
|
|
const posenet = require('./posenet/posenet.js');
|
|
|
|
const handpose = require('./handpose/handpose.js');
|
2020-11-04 16:18:22 +01:00
|
|
|
const gesture = require('./gesture.js');
|
|
|
|
const image = require('./image.js');
|
2020-11-01 19:07:53 +01:00
|
|
|
const profile = require('./profile.js');
|
2020-10-17 12:30:00 +02:00
|
|
|
const defaults = require('../config.js').default;
|
2020-10-15 21:25:58 +02:00
|
|
|
const app = require('../package.json');
|
|
|
|
|
2020-10-19 17:03:48 +02:00
|
|
|
// static config override for non-video detection
|
2020-10-18 14:07:45 +02:00
|
|
|
const override = {
|
|
|
|
face: { detector: { skipFrames: 0 }, age: { skipFrames: 0 }, emotion: { skipFrames: 0 } },
|
|
|
|
hand: { skipFrames: 0 },
|
|
|
|
};
|
|
|
|
|
2020-10-17 16:06:02 +02:00
|
|
|
// helper function: gets elapsed time on both browser and nodejs
|
2020-10-16 16:12:12 +02:00
|
|
|
const now = () => {
|
|
|
|
if (typeof performance !== 'undefined') return performance.now();
|
|
|
|
return parseInt(Number(process.hrtime.bigint()) / 1000 / 1000);
|
|
|
|
};
|
2020-10-12 01:22:43 +02:00
|
|
|
|
2020-10-17 16:06:02 +02:00
|
|
|
// helper function: perform deep merge of multiple objects so it allows full inheriance with overrides
|
2020-10-12 01:22:43 +02:00
|
|
|
function mergeDeep(...objects) {
|
|
|
|
const isObject = (obj) => obj && typeof obj === 'object';
|
|
|
|
return objects.reduce((prev, obj) => {
|
2020-10-12 03:21:41 +02:00
|
|
|
Object.keys(obj || {}).forEach((key) => {
|
2020-10-12 01:22:43 +02:00
|
|
|
const pVal = prev[key];
|
|
|
|
const oVal = obj[key];
|
|
|
|
if (Array.isArray(pVal) && Array.isArray(oVal)) {
|
|
|
|
prev[key] = pVal.concat(...oVal);
|
|
|
|
} else if (isObject(pVal) && isObject(oVal)) {
|
|
|
|
prev[key] = mergeDeep(pVal, oVal);
|
|
|
|
} else {
|
|
|
|
prev[key] = oVal;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return prev;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2020-10-19 17:03:48 +02:00
|
|
|
class Human {
|
|
|
|
constructor() {
|
|
|
|
this.tf = tf;
|
|
|
|
this.version = app.version;
|
|
|
|
this.defaults = defaults;
|
|
|
|
this.config = defaults;
|
2020-11-03 00:54:03 +01:00
|
|
|
this.fx = null;
|
2020-10-19 17:03:48 +02:00
|
|
|
this.state = 'idle';
|
|
|
|
this.numTensors = 0;
|
|
|
|
this.analyzeMemoryLeaks = false;
|
2020-11-03 16:55:33 +01:00
|
|
|
this.checkSanity = false;
|
|
|
|
this.firstRun = true;
|
2020-10-19 17:03:48 +02:00
|
|
|
// object that contains all initialized models
|
|
|
|
this.models = {
|
|
|
|
facemesh: null,
|
|
|
|
posenet: null,
|
|
|
|
handpose: null,
|
|
|
|
iris: null,
|
|
|
|
age: null,
|
|
|
|
gender: null,
|
|
|
|
emotion: null,
|
|
|
|
};
|
|
|
|
// export raw access to underlying models
|
|
|
|
this.facemesh = facemesh;
|
|
|
|
this.ssrnet = ssrnet;
|
|
|
|
this.emotion = emotion;
|
|
|
|
this.posenet = posenet;
|
|
|
|
this.handpose = handpose;
|
2020-10-18 18:12:09 +02:00
|
|
|
}
|
2020-10-19 17:03:48 +02:00
|
|
|
|
|
|
|
// helper function: wrapper around console output
|
|
|
|
log(...msg) {
|
|
|
|
// eslint-disable-next-line no-console
|
2020-10-30 16:57:23 +01:00
|
|
|
if (msg && this.config.console) console.log('Human:', ...msg);
|
2020-10-18 18:12:09 +02:00
|
|
|
}
|
2020-10-19 17:03:48 +02:00
|
|
|
|
2020-11-01 19:07:53 +01:00
|
|
|
profile() {
|
|
|
|
if (this.config.profile) return profile.data;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-10-19 17:03:48 +02:00
|
|
|
// helper function: measure tensor leak
|
|
|
|
analyze(...msg) {
|
|
|
|
if (!this.analyzeMemoryLeaks) return;
|
|
|
|
const current = tf.engine().state.numTensors;
|
|
|
|
const previous = this.numTensors;
|
|
|
|
this.numTensors = current;
|
|
|
|
const leaked = current - previous;
|
|
|
|
if (leaked !== 0) this.log(...msg, leaked);
|
2020-10-18 18:12:09 +02:00
|
|
|
}
|
2020-10-17 13:15:23 +02:00
|
|
|
|
2020-11-04 16:18:22 +01:00
|
|
|
// quick sanity check on inputs
|
2020-11-03 16:55:33 +01:00
|
|
|
sanity(input) {
|
|
|
|
if (!this.checkSanity) return null;
|
|
|
|
if (!input) return 'input is not defined';
|
|
|
|
if (tf.ENV.flags.IS_NODE && !(input instanceof tf.Tensor)) {
|
|
|
|
return 'input must be a tensor';
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
tf.getBackend();
|
|
|
|
} catch {
|
|
|
|
return 'backend not loaded';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-04 16:18:22 +01:00
|
|
|
// preload models, not explicitly required as it's done automatically on first use
|
2020-10-19 17:03:48 +02:00
|
|
|
async load(userConfig) {
|
|
|
|
if (userConfig) this.config = mergeDeep(defaults, userConfig);
|
2020-11-03 15:34:36 +01:00
|
|
|
|
2020-11-03 16:55:33 +01:00
|
|
|
if (this.firstRun) {
|
2020-11-04 16:18:22 +01:00
|
|
|
this.checkBackend(true);
|
2020-11-03 15:34:36 +01:00
|
|
|
this.log(`version: ${this.version} TensorFlow/JS version: ${tf.version_core}`);
|
|
|
|
this.log('configuration:', this.config);
|
|
|
|
this.log('flags:', tf.ENV.flags);
|
2020-11-03 16:55:33 +01:00
|
|
|
this.firstRun = false;
|
2020-11-03 15:34:36 +01:00
|
|
|
}
|
|
|
|
|
2020-10-19 17:03:48 +02:00
|
|
|
if (this.config.face.enabled && !this.models.facemesh) {
|
2020-11-03 15:34:36 +01:00
|
|
|
this.log('load model: face');
|
2020-10-19 17:03:48 +02:00
|
|
|
this.models.facemesh = await facemesh.load(this.config.face);
|
|
|
|
}
|
|
|
|
if (this.config.body.enabled && !this.models.posenet) {
|
2020-11-03 15:34:36 +01:00
|
|
|
this.log('load model: body');
|
2020-10-19 17:03:48 +02:00
|
|
|
this.models.posenet = await posenet.load(this.config.body);
|
|
|
|
}
|
|
|
|
if (this.config.hand.enabled && !this.models.handpose) {
|
2020-11-03 15:34:36 +01:00
|
|
|
this.log('load model: hand');
|
2020-10-19 17:03:48 +02:00
|
|
|
this.models.handpose = await handpose.load(this.config.hand);
|
|
|
|
}
|
|
|
|
if (this.config.face.enabled && this.config.face.age.enabled && !this.models.age) {
|
2020-11-03 15:34:36 +01:00
|
|
|
this.log('load model: age');
|
2020-10-19 17:03:48 +02:00
|
|
|
this.models.age = await ssrnet.loadAge(this.config);
|
|
|
|
}
|
|
|
|
if (this.config.face.enabled && this.config.face.gender.enabled && !this.models.gender) {
|
2020-11-03 15:34:36 +01:00
|
|
|
this.log('load model: gender');
|
2020-10-19 17:03:48 +02:00
|
|
|
this.models.gender = await ssrnet.loadGender(this.config);
|
|
|
|
}
|
|
|
|
if (this.config.face.enabled && this.config.face.emotion.enabled && !this.models.emotion) {
|
2020-11-03 15:34:36 +01:00
|
|
|
this.log('load model: emotion');
|
2020-10-19 17:03:48 +02:00
|
|
|
this.models.emotion = await emotion.load(this.config);
|
2020-10-18 18:12:09 +02:00
|
|
|
}
|
2020-10-18 15:21:53 +02:00
|
|
|
}
|
2020-10-17 17:38:24 +02:00
|
|
|
|
2020-11-04 16:18:22 +01:00
|
|
|
// check if backend needs initialization if it changed
|
|
|
|
async checkBackend(force) {
|
|
|
|
if (force || (tf.getBackend() !== this.config.backend)) {
|
2020-10-30 15:23:49 +01:00
|
|
|
this.state = 'backend';
|
2020-11-01 19:07:53 +01:00
|
|
|
/* force backend reload
|
2020-10-30 16:57:23 +01:00
|
|
|
if (this.config.backend in tf.engine().registry) {
|
2020-11-01 19:07:53 +01:00
|
|
|
const backendFactory = tf.findBackendFactory(this.config.backend);
|
|
|
|
tf.removeBackend(this.config.backend);
|
|
|
|
tf.registerBackend(this.config.backend, backendFactory);
|
2020-10-30 15:23:49 +01:00
|
|
|
} else {
|
2020-10-30 16:57:23 +01:00
|
|
|
this.log('Backend not registred:', this.config.backend);
|
2020-10-30 15:23:49 +01:00
|
|
|
}
|
2020-11-01 19:07:53 +01:00
|
|
|
*/
|
2020-11-04 16:18:22 +01:00
|
|
|
this.log('setting backend:', this.config.backend);
|
2020-11-01 19:07:53 +01:00
|
|
|
await tf.setBackend(this.config.backend);
|
|
|
|
tf.enableProdMode();
|
|
|
|
/* debug mode is really too mcuh
|
|
|
|
if (this.config.profile) tf.enableDebugMode();
|
|
|
|
else tf.enableProdMode();
|
|
|
|
*/
|
|
|
|
if (this.config.deallocate && this.config.backend === 'webgl') {
|
|
|
|
this.log('Changing WebGL: WEBGL_DELETE_TEXTURE_THRESHOLD:', this.config.deallocate);
|
|
|
|
tf.ENV.set('WEBGL_DELETE_TEXTURE_THRESHOLD', this.config.deallocate ? 0 : -1);
|
|
|
|
}
|
2020-11-04 16:18:22 +01:00
|
|
|
// tf.ENV.set('WEBGL_CPU_FORWARD', true);
|
|
|
|
// tf.ENV.set('WEBGL_FORCE_F16_TEXTURES', true);
|
|
|
|
// tf.ENV.set('WEBGL_PACK_DEPTHWISECONV', true);
|
2020-11-01 19:07:53 +01:00
|
|
|
await tf.ready();
|
2020-10-30 15:23:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 16:18:22 +01:00
|
|
|
// main detect function
|
2020-10-19 17:03:48 +02:00
|
|
|
async detect(input, userConfig = {}) {
|
|
|
|
this.state = 'config';
|
|
|
|
const perf = {};
|
|
|
|
let timeStamp;
|
2020-10-17 17:38:24 +02:00
|
|
|
|
2020-11-04 16:18:22 +01:00
|
|
|
// update configuration
|
2020-10-19 17:03:48 +02:00
|
|
|
this.config = mergeDeep(defaults, userConfig);
|
|
|
|
if (!this.config.videoOptimized) this.config = mergeDeep(this.config, override);
|
2020-10-17 17:43:04 +02:00
|
|
|
|
2020-10-19 17:03:48 +02:00
|
|
|
// sanity checks
|
|
|
|
this.state = 'check';
|
2020-11-03 16:55:33 +01:00
|
|
|
const error = this.sanity(input);
|
2020-10-19 17:03:48 +02:00
|
|
|
if (error) {
|
|
|
|
this.log(error, input);
|
|
|
|
return { error };
|
2020-10-17 17:43:04 +02:00
|
|
|
}
|
2020-10-14 17:43:33 +02:00
|
|
|
|
2020-11-04 16:18:22 +01:00
|
|
|
// detection happens inside a promise
|
2020-10-19 17:03:48 +02:00
|
|
|
// eslint-disable-next-line no-async-promise-executor
|
|
|
|
return new Promise(async (resolve) => {
|
2020-11-03 00:54:03 +01:00
|
|
|
let poseRes;
|
|
|
|
let handRes;
|
|
|
|
let ssrRes;
|
|
|
|
let emotionRes;
|
|
|
|
|
2020-10-19 17:03:48 +02:00
|
|
|
const timeStart = now();
|
2020-10-14 19:23:02 +02:00
|
|
|
|
2020-10-19 17:03:48 +02:00
|
|
|
// configure backend
|
|
|
|
timeStamp = now();
|
2020-10-30 16:57:23 +01:00
|
|
|
await this.checkBackend();
|
2020-10-19 17:03:48 +02:00
|
|
|
perf.backend = Math.trunc(now() - timeStamp);
|
2020-10-17 16:06:02 +02:00
|
|
|
|
2020-10-19 17:03:48 +02:00
|
|
|
// load models if enabled
|
|
|
|
timeStamp = now();
|
|
|
|
this.state = 'load';
|
|
|
|
await this.load();
|
|
|
|
perf.load = Math.trunc(now() - timeStamp);
|
2020-10-18 15:21:53 +02:00
|
|
|
|
2020-10-19 17:03:48 +02:00
|
|
|
if (this.config.scoped) tf.engine().startScope();
|
2020-10-12 01:22:43 +02:00
|
|
|
|
2020-10-19 17:03:48 +02:00
|
|
|
this.analyze('Start Detect:');
|
2020-10-12 01:22:43 +02:00
|
|
|
|
2020-10-16 16:12:12 +02:00
|
|
|
timeStamp = now();
|
2020-11-04 16:18:22 +01:00
|
|
|
const process = image.process(input, this.config);
|
2020-10-19 17:03:48 +02:00
|
|
|
perf.image = Math.trunc(now() - timeStamp);
|
|
|
|
|
|
|
|
// run facemesh, includes blazeface and iris
|
|
|
|
const faceRes = [];
|
|
|
|
if (this.config.face.enabled) {
|
|
|
|
this.state = 'run:face';
|
2020-10-16 16:12:12 +02:00
|
|
|
timeStamp = now();
|
2020-10-19 17:03:48 +02:00
|
|
|
this.analyze('Start FaceMesh:');
|
2020-11-04 16:18:22 +01:00
|
|
|
const faces = await this.models.facemesh.estimateFaces(process.tensor, this.config.face);
|
2020-10-19 17:03:48 +02:00
|
|
|
perf.face = Math.trunc(now() - timeStamp);
|
|
|
|
for (const face of faces) {
|
|
|
|
// is something went wrong, skip the face
|
|
|
|
if (!face.image || face.image.isDisposedInternal) {
|
2020-10-30 16:57:23 +01:00
|
|
|
this.log('Face object is disposed:', face.image);
|
2020-10-19 17:03:48 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// run ssr-net age & gender, inherits face from blazeface
|
|
|
|
this.state = 'run:agegender';
|
|
|
|
timeStamp = now();
|
2020-11-03 00:54:03 +01:00
|
|
|
ssrRes = (this.config.face.age.enabled || this.config.face.gender.enabled) ? await ssrnet.predict(face.image, this.config) : {};
|
2020-10-19 17:03:48 +02:00
|
|
|
perf.agegender = Math.trunc(now() - timeStamp);
|
|
|
|
// run emotion, inherits face from blazeface
|
|
|
|
this.state = 'run:emotion';
|
|
|
|
timeStamp = now();
|
2020-11-03 00:54:03 +01:00
|
|
|
emotionRes = this.config.face.emotion.enabled ? await emotion.predict(face.image, this.config) : {};
|
2020-10-19 17:03:48 +02:00
|
|
|
perf.emotion = Math.trunc(now() - timeStamp);
|
2020-10-17 17:38:24 +02:00
|
|
|
|
2020-10-19 17:03:48 +02:00
|
|
|
// dont need face anymore
|
|
|
|
face.image.dispose();
|
|
|
|
// calculate iris distance
|
|
|
|
// iris: array[ bottom, left, top, right, center ]
|
|
|
|
const iris = (face.annotations.leftEyeIris && face.annotations.rightEyeIris)
|
|
|
|
? Math.max(face.annotations.leftEyeIris[3][0] - face.annotations.leftEyeIris[1][0], face.annotations.rightEyeIris[3][0] - face.annotations.rightEyeIris[1][0])
|
|
|
|
: 0;
|
|
|
|
faceRes.push({
|
|
|
|
confidence: face.confidence,
|
|
|
|
box: face.box,
|
|
|
|
mesh: face.mesh,
|
|
|
|
annotations: face.annotations,
|
2020-11-03 00:54:03 +01:00
|
|
|
age: ssrRes.age,
|
|
|
|
gender: ssrRes.gender,
|
|
|
|
agConfidence: ssrRes.confidence,
|
|
|
|
emotion: emotionRes,
|
2020-10-19 17:03:48 +02:00
|
|
|
iris: (iris !== 0) ? Math.trunc(100 * 11.7 /* human iris size in mm */ / iris) / 100 : 0,
|
|
|
|
});
|
|
|
|
this.analyze('End FaceMesh:');
|
|
|
|
}
|
2020-10-14 02:52:30 +02:00
|
|
|
}
|
2020-10-13 04:01:35 +02:00
|
|
|
|
2020-11-04 07:11:24 +01:00
|
|
|
// run posenet
|
|
|
|
if (this.config.async) {
|
2020-11-04 16:18:22 +01:00
|
|
|
poseRes = this.config.body.enabled ? this.models.posenet.estimatePoses(process.tensor, this.config.body) : [];
|
2020-11-04 07:11:24 +01:00
|
|
|
} else {
|
|
|
|
this.state = 'run:body';
|
|
|
|
timeStamp = now();
|
|
|
|
this.analyze('Start PoseNet');
|
2020-11-04 16:18:22 +01:00
|
|
|
poseRes = this.config.body.enabled ? await this.models.posenet.estimatePoses(process.tensor, this.config.body) : [];
|
2020-11-04 07:11:24 +01:00
|
|
|
this.analyze('End PoseNet:');
|
|
|
|
perf.body = Math.trunc(now() - timeStamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// run handpose
|
|
|
|
if (this.config.async) {
|
2020-11-04 16:18:22 +01:00
|
|
|
handRes = this.config.hand.enabled ? this.models.handpose.estimateHands(process.tensor, this.config.hand) : [];
|
2020-11-04 07:11:24 +01:00
|
|
|
} else {
|
|
|
|
this.state = 'run:hand';
|
|
|
|
timeStamp = now();
|
|
|
|
this.analyze('Start HandPose:');
|
2020-11-04 16:18:22 +01:00
|
|
|
handRes = this.config.hand.enabled ? await this.models.handpose.estimateHands(process.tensor, this.config.hand) : [];
|
2020-11-04 07:11:24 +01:00
|
|
|
this.analyze('End HandPose:');
|
|
|
|
perf.hand = Math.trunc(now() - timeStamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.config.async) [poseRes, handRes] = await Promise.all([poseRes, handRes]);
|
|
|
|
|
2020-11-04 16:18:22 +01:00
|
|
|
process.tensor.dispose();
|
2020-10-19 17:03:48 +02:00
|
|
|
this.state = 'idle';
|
2020-10-14 17:43:33 +02:00
|
|
|
|
2020-10-19 17:03:48 +02:00
|
|
|
if (this.config.scoped) tf.engine().endScope();
|
|
|
|
this.analyze('End Scope:');
|
2020-10-17 13:15:23 +02:00
|
|
|
|
2020-11-04 16:18:22 +01:00
|
|
|
let gestureRes = [];
|
|
|
|
if (this.config.gesture.enabled) {
|
|
|
|
timeStamp = now();
|
|
|
|
gestureRes = { body: gesture.body(poseRes), hand: gesture.hand(handRes), face: gesture.face(faceRes) };
|
|
|
|
perf.gesture = Math.trunc(now() - timeStamp);
|
|
|
|
}
|
|
|
|
|
2020-10-19 17:03:48 +02:00
|
|
|
perf.total = Math.trunc(now() - timeStart);
|
2020-11-04 16:18:22 +01:00
|
|
|
resolve({ face: faceRes, body: poseRes, hand: handRes, gesture: gestureRes, performance: perf, canvas: process.canvas });
|
2020-10-19 17:03:48 +02:00
|
|
|
});
|
|
|
|
}
|
2020-10-12 01:22:43 +02:00
|
|
|
}
|
|
|
|
|
2020-10-19 17:03:48 +02:00
|
|
|
export { Human as default };
|