2021-04-09 14:07:58 +02:00
|
|
|
import { log, join } from '../helpers';
|
2020-11-18 14:26:28 +01:00
|
|
|
import * as tf from '../../dist/tfjs.esm.js';
|
2021-02-13 15:16:41 +01:00
|
|
|
import * as profile from '../profile';
|
2020-11-06 17:39:39 +01:00
|
|
|
|
2021-02-08 18:47:38 +01:00
|
|
|
let model;
|
2020-11-06 17:39:39 +01:00
|
|
|
let last = { age: 0 };
|
2020-12-11 16:11:49 +01:00
|
|
|
let skipped = Number.MAX_SAFE_INTEGER;
|
2020-11-06 17:39:39 +01:00
|
|
|
|
2021-02-08 17:39:09 +01:00
|
|
|
export async function load(config) {
|
2021-02-08 18:47:38 +01:00
|
|
|
if (!model) {
|
2021-04-09 14:07:58 +02:00
|
|
|
model = await tf.loadGraphModel(join(config.modelBasePath, config.face.age.modelPath));
|
|
|
|
if (!model || !model.modelUrl) log('load model failed:', config.face.age.modelPath);
|
|
|
|
else if (config.debug) log('load model:', model.modelUrl);
|
2020-11-07 16:37:19 +01:00
|
|
|
}
|
2021-02-08 18:47:38 +01:00
|
|
|
return model;
|
2020-11-06 17:39:39 +01:00
|
|
|
}
|
|
|
|
|
2021-02-08 17:39:09 +01:00
|
|
|
export async function predict(image, config) {
|
2021-02-08 18:47:38 +01:00
|
|
|
if (!model) return null;
|
2020-12-11 16:11:49 +01:00
|
|
|
if ((skipped < config.face.age.skipFrames) && config.videoOptimized && last.age && (last.age > 0)) {
|
|
|
|
skipped++;
|
2020-11-06 19:50:16 +01:00
|
|
|
return last;
|
|
|
|
}
|
2020-12-11 16:11:49 +01:00
|
|
|
if (config.videoOptimized) skipped = 0;
|
|
|
|
else skipped = Number.MAX_SAFE_INTEGER;
|
2020-11-06 17:39:39 +01:00
|
|
|
return new Promise(async (resolve) => {
|
2021-03-11 16:26:14 +01:00
|
|
|
const resize = tf.image.resizeBilinear(image, [model.inputs[0].shape[2], model.inputs[0].shape[1]], false);
|
2020-11-06 17:39:39 +01:00
|
|
|
const enhance = tf.mul(resize, [255.0]);
|
|
|
|
tf.dispose(resize);
|
|
|
|
|
|
|
|
let ageT;
|
2021-02-08 18:47:38 +01:00
|
|
|
const obj = { age: 0 };
|
2020-11-06 17:39:39 +01:00
|
|
|
|
2021-02-08 18:47:38 +01:00
|
|
|
if (!config.profile) {
|
|
|
|
if (config.face.age.enabled) ageT = await model.predict(enhance);
|
|
|
|
} else {
|
|
|
|
const profileAge = config.face.age.enabled ? await tf.profile(() => model.predict(enhance)) : {};
|
|
|
|
ageT = profileAge.result.clone();
|
|
|
|
profileAge.result.dispose();
|
|
|
|
profile.run('age', profileAge);
|
|
|
|
}
|
|
|
|
enhance.dispose();
|
2020-11-06 17:39:39 +01:00
|
|
|
|
2021-02-08 18:47:38 +01:00
|
|
|
if (ageT) {
|
|
|
|
const data = ageT.dataSync();
|
|
|
|
obj.age = Math.trunc(10 * data[0]) / 10;
|
2021-02-08 17:39:09 +01:00
|
|
|
}
|
2021-02-08 18:47:38 +01:00
|
|
|
ageT.dispose();
|
|
|
|
|
|
|
|
last = obj;
|
2020-11-06 17:39:39 +01:00
|
|
|
resolve(obj);
|
|
|
|
});
|
|
|
|
}
|