2021-08-01 02:42:28 +02:00
|
|
|
/**
|
|
|
|
* Module that analyzes person age
|
|
|
|
* Obsolete
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
to enable, add to config.ts
|
|
|
|
|
|
|
|
agegenderrace: {
|
|
|
|
enabled: boolean,
|
|
|
|
modelPath: string,
|
|
|
|
skipFrames: number,
|
|
|
|
}
|
|
|
|
|
|
|
|
agegenderrace: {
|
|
|
|
enabled: true,
|
|
|
|
modelPath: 'gear.json',
|
|
|
|
skipFrames: 1,
|
|
|
|
},
|
|
|
|
|
|
|
|
and enable model loading in models.ts
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { log, join } from '../helpers';
|
|
|
|
import * as tf from '../../dist/tfjs.esm.js';
|
|
|
|
import { Config } from '../config';
|
|
|
|
import { GraphModel, Tensor } from '../tfjs/types';
|
|
|
|
|
|
|
|
let model: GraphModel;
|
|
|
|
|
|
|
|
let last = { age: 0 };
|
|
|
|
let skipped = Number.MAX_SAFE_INTEGER;
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
export async function load(config: Config | any) {
|
|
|
|
if (!model) {
|
|
|
|
// @ts-ignore type mismatch on GraphModel
|
|
|
|
model = await tf.loadGraphModel(join(config.modelBasePath, config.face.agegenderrace.modelPath));
|
|
|
|
if (!model || !model['modelUrl']) log('load model failed:', config.face.agegenderrace.modelPath);
|
|
|
|
else if (config.debug) log('load model:', model['modelUrl']);
|
|
|
|
} else if (config.debug) log('cached model:', model['modelUrl']);
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
export async function predict(image: Tensor, config: Config) {
|
|
|
|
if (!model) return null;
|
|
|
|
// @ts-ignore config disabled
|
|
|
|
if ((skipped < config.face.agegenderrace.skipFrames) && config.skipFrame && last.age && (last.age > 0)) {
|
|
|
|
skipped++;
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
skipped = 0;
|
|
|
|
return new Promise(async (resolve) => {
|
|
|
|
if (!model.inputs[0].shape) return;
|
|
|
|
const resize = tf.image.resizeBilinear(image, [model.inputs[0].shape[2], model.inputs[0].shape[1]], false);
|
|
|
|
// const enhance = tf.mul(resize, [255.0]);
|
|
|
|
|
|
|
|
let ageT;
|
|
|
|
let genderT;
|
|
|
|
let raceT;
|
|
|
|
const obj = { age: 0 };
|
|
|
|
|
|
|
|
// @ts-ignore array definition unavailable at compile time
|
|
|
|
if (config.face.agegenderrace.enabled) [ageT, genderT, raceT] = await model.execute(resize, ['age_output', 'gender_output', 'race_output']);
|
|
|
|
tf.dispose(resize);
|
|
|
|
// tf.dispose(enhance);
|
|
|
|
|
|
|
|
if (ageT) {
|
2021-08-14 17:16:26 +02:00
|
|
|
// const data = await ageT.data();
|
2021-08-01 02:42:28 +02:00
|
|
|
// {0: 'below_20', 1: '21-25', 2: '26-30', 3: '31-40',4: '41-50', 5: '51-60', 6: 'Above60'}
|
|
|
|
}
|
|
|
|
if (genderT) {
|
2021-08-14 17:16:26 +02:00
|
|
|
// const data = await genderT.data();
|
2021-08-01 02:42:28 +02:00
|
|
|
}
|
|
|
|
if (raceT) {
|
2021-08-14 17:16:26 +02:00
|
|
|
// const data = await raceT.data();
|
2021-08-01 02:42:28 +02:00
|
|
|
// {0: 'white', 1: 'black', 2: 'asian', 3: 'indian', 4: 'others'}
|
|
|
|
}
|
|
|
|
|
|
|
|
tf.dispose(ageT);
|
|
|
|
tf.dispose(genderT);
|
|
|
|
tf.dispose(raceT);
|
|
|
|
|
|
|
|
last = obj;
|
|
|
|
resolve(obj);
|
|
|
|
});
|
|
|
|
}
|