human/src/index.js

131 lines
5.1 KiB
JavaScript
Raw Normal View History

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-10-12 01:22:43 +02:00
const defaults = require('./config.js').default;
2020-10-15 15:43:16 +02:00
// object that contains all initialized models
2020-10-12 01:22:43 +02:00
const models = {
facemesh: null,
2020-10-15 15:43:16 +02:00
posenet: null,
handpose: null,
2020-10-12 01:22:43 +02:00
iris: null,
2020-10-15 15:43:16 +02:00
age: null,
gender: null,
emotion: null,
2020-10-12 01:22:43 +02:00
};
2020-10-15 15:43:16 +02:00
// helper function that performs 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;
}, {});
}
async function detect(input, userConfig) {
2020-10-14 02:52:30 +02:00
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve) => {
const config = mergeDeep(defaults, userConfig);
2020-10-12 01:22:43 +02:00
2020-10-14 02:52:30 +02:00
// load models if enabled
2020-10-15 15:43:16 +02:00
if (config.face.enabled && !models.facemesh) models.facemesh = await facemesh.load(config.face);
2020-10-14 02:52:30 +02:00
if (config.body.enabled && !models.posenet) models.posenet = await posenet.load(config.body);
if (config.hand.enabled && !models.handpose) models.handpose = await handpose.load(config.hand);
2020-10-15 15:43:16 +02:00
if (config.face.enabled && config.face.age.enabled && !models.age) models.age = await ssrnet.loadAge(config);
if (config.face.enabled && config.face.gender.enabled && !models.gender) models.gender = await ssrnet.loadGender(config);
if (config.face.enabled && config.face.emotion.enabled && !models.emotion) models.emotion = await emotion.load(config);
2020-10-13 04:01:35 +02:00
2020-10-15 15:43:16 +02:00
// explictly enable depthwiseconv since it's diasabled by default due to issues with large shaders
2020-10-14 17:43:33 +02:00
let savedWebglPackDepthwiseConvFlag;
if (tf.getBackend() === 'webgl') {
savedWebglPackDepthwiseConvFlag = tf.env().get('WEBGL_PACK_DEPTHWISECONV');
tf.env().set('WEBGL_PACK_DEPTHWISECONV', true);
}
const perf = {};
let timeStamp;
2020-10-14 02:52:30 +02:00
// run posenet
timeStamp = performance.now();
2020-10-14 02:52:30 +02:00
let poseRes = [];
2020-10-15 15:43:16 +02:00
tf.engine().startScope();
2020-10-14 17:43:33 +02:00
if (config.body.enabled) poseRes = await models.posenet.estimatePoses(input, config.body);
2020-10-15 15:43:16 +02:00
tf.engine().endScope();
perf.body = Math.trunc(performance.now() - timeStamp);
2020-10-12 01:22:43 +02:00
2020-10-14 02:52:30 +02:00
// run handpose
timeStamp = performance.now();
2020-10-14 02:52:30 +02:00
let handRes = [];
2020-10-15 15:43:16 +02:00
tf.engine().startScope();
2020-10-14 02:52:30 +02:00
if (config.hand.enabled) handRes = await models.handpose.estimateHands(input, config.hand);
2020-10-15 15:43:16 +02:00
tf.engine().endScope();
perf.hand = Math.trunc(performance.now() - timeStamp);
2020-10-12 01:22:43 +02:00
2020-10-14 02:52:30 +02:00
// run facemesh, includes blazeface and iris
const faceRes = [];
if (config.face.enabled) {
timeStamp = performance.now();
2020-10-15 15:43:16 +02:00
tf.engine().startScope();
2020-10-14 02:52:30 +02:00
const faces = await models.facemesh.estimateFaces(input, config.face);
perf.face = Math.trunc(performance.now() - timeStamp);
2020-10-14 02:52:30 +02:00
for (const face of faces) {
// run ssr-net age & gender, inherits face from blazeface
timeStamp = performance.now();
2020-10-15 15:43:16 +02:00
const ssrData = (config.face.age.enabled || config.face.gender.enabled) ? await ssrnet.predict(face.image, config) : {};
perf.agegender = Math.trunc(performance.now() - timeStamp);
2020-10-15 00:22:38 +02:00
// run emotion, inherits face from blazeface
timeStamp = performance.now();
2020-10-15 15:43:16 +02:00
const emotionData = config.face.emotion.enabled ? await emotion.predict(face.image, config) : {};
2020-10-15 00:22:38 +02:00
perf.emotion = Math.trunc(performance.now() - timeStamp);
2020-10-14 02:52:30 +02:00
face.image.dispose();
2020-10-15 00:22:38 +02:00
// calculate iris distance
2020-10-14 02:52:30 +02:00
// 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-10-15 15:43:16 +02:00
age: ssrData.age,
gender: ssrData.gender,
emotion: emotionData,
2020-10-15 00:22:38 +02:00
iris: (iris !== 0) ? Math.trunc(100 * 11.7 /* human iris size in mm */ / iris) / 100 : 0,
2020-10-14 02:52:30 +02:00
});
}
2020-10-15 15:43:16 +02:00
tf.engine().endScope();
2020-10-12 01:22:43 +02:00
}
2020-10-13 04:01:35 +02:00
2020-10-15 15:43:16 +02:00
// set depthwiseconv to original value
2020-10-14 17:43:33 +02:00
tf.env().set('WEBGL_PACK_DEPTHWISECONV', savedWebglPackDepthwiseConvFlag);
2020-10-15 15:43:16 +02:00
// combine and return results
perf.total = Object.values(perf).reduce((a, b) => a + b);
resolve({ face: faceRes, body: poseRes, hand: handRes, performance: perf });
2020-10-14 02:52:30 +02:00
});
2020-10-12 01:22:43 +02:00
}
exports.detect = detect;
exports.defaults = defaults;
exports.models = models;
2020-10-12 03:21:41 +02:00
exports.facemesh = facemesh;
exports.ssrnet = ssrnet;
exports.posenet = posenet;
exports.handpose = handpose;
2020-10-12 16:08:00 +02:00
exports.tf = tf;