human/src/age/age.js

64 lines
1.9 KiB
JavaScript
Raw Normal View History

import { tf, loadGraphModel } from '../tf.js';
2020-11-10 02:13:38 +01:00
import * as profile from '../profile.js';
2020-11-06 17:39:39 +01:00
const models = {};
let last = { age: 0 };
let frame = Number.MAX_SAFE_INTEGER;
// tuning values
const zoom = [0, 0]; // 0..1 meaning 0%..100%
async function load(config) {
2020-11-07 16:37:19 +01:00
if (!models.age) {
models.age = await loadGraphModel(config.face.age.modelPath);
2020-11-07 16:37:19 +01:00
// eslint-disable-next-line no-console
console.log(`Human: load model: ${config.face.age.modelPath.match(/\/(.*)\./)[1]}`);
}
2020-11-06 17:39:39 +01:00
return models.age;
}
async function predict(image, config) {
2020-11-06 19:50:16 +01:00
if ((frame < config.face.age.skipFrames) && last.age && (last.age > 0)) {
frame += 1;
return last;
}
frame = 0;
2020-11-06 17:39:39 +01:00
return new Promise(async (resolve) => {
const box = [[
(image.shape[1] * zoom[0]) / image.shape[1],
(image.shape[2] * zoom[1]) / image.shape[2],
(image.shape[1] - (image.shape[1] * zoom[0])) / image.shape[1],
(image.shape[2] - (image.shape[2] * zoom[1])) / image.shape[2],
]];
const resize = tf.image.cropAndResize(image, box, [0], [config.face.age.inputSize, config.face.age.inputSize]);
// const resize = tf.image.resizeBilinear(image, [config.face.age.inputSize, config.face.age.inputSize], false);
const enhance = tf.mul(resize, [255.0]);
tf.dispose(resize);
let ageT;
const obj = {};
if (!config.profile) {
if (config.face.age.enabled) ageT = await models.age.predict(enhance);
} else {
const profileAge = config.face.age.enabled ? await tf.profile(() => models.age.predict(enhance)) : {};
ageT = profileAge.result.clone();
profileAge.result.dispose();
profile.run('age', profileAge);
}
enhance.dispose();
if (ageT) {
const data = ageT.dataSync();
obj.age = Math.trunc(10 * data[0]) / 10;
}
ageT.dispose();
last = obj;
resolve(obj);
});
}
exports.predict = predict;
exports.load = load;