2021-08-01 02:42:28 +02:00
|
|
|
/**
|
2021-09-25 17:51:15 +02:00
|
|
|
* GEAR [gender/emotion/age/race] model implementation
|
|
|
|
*
|
|
|
|
* Based on: [**GEAR Predictor**](https://github.com/Udolf15/GEAR-Predictor)
|
|
|
|
*
|
|
|
|
* Obsolete and replaced by `faceres` that performs age/gender/descriptor analysis
|
|
|
|
* Config placeholder: agegenderrace: { enabled: true, modelPath: 'gear.json' },
|
2021-08-01 02:42:28 +02:00
|
|
|
*/
|
|
|
|
|
2021-10-22 22:09:52 +02:00
|
|
|
import { log, join, now } from '../util/util';
|
2021-08-01 02:42:28 +02:00
|
|
|
import * as tf from '../../dist/tfjs.esm.js';
|
2021-09-13 19:28:35 +02:00
|
|
|
import type { Config } from '../config';
|
|
|
|
import type { GraphModel, Tensor } from '../tfjs/types';
|
2021-09-27 19:58:13 +02:00
|
|
|
import { env } from '../util/env';
|
2021-08-01 02:42:28 +02:00
|
|
|
|
2021-09-17 17:23:00 +02:00
|
|
|
let model: GraphModel | null;
|
2021-08-01 02:42:28 +02:00
|
|
|
|
|
|
|
let last = { age: 0 };
|
2021-10-22 22:09:52 +02:00
|
|
|
let lastTime = 0;
|
2021-08-01 02:42:28 +02:00
|
|
|
let skipped = Number.MAX_SAFE_INTEGER;
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
export async function load(config: Config | any) {
|
2021-09-17 17:23:00 +02:00
|
|
|
if (env.initial) model = null;
|
2021-08-01 02:42:28 +02:00
|
|
|
if (!model) {
|
2021-08-17 14:51:17 +02:00
|
|
|
model = await tf.loadGraphModel(join(config.modelBasePath, config.face.agegenderrace.modelPath)) as unknown as GraphModel;
|
2021-08-01 02:42:28 +02:00
|
|
|
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
|
2021-10-22 22:09:52 +02:00
|
|
|
if ((skipped < config.face.agegenderrace?.skipFrames) && ((config.face.agegenderrace?.skipTime || 0) <= (now() - lastTime)) && config.skipFrame && last.age && (last.age > 0)) {
|
2021-08-01 02:42:28 +02:00
|
|
|
skipped++;
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
skipped = 0;
|
|
|
|
return new Promise(async (resolve) => {
|
2021-09-17 17:23:00 +02:00
|
|
|
if (!model?.inputs[0].shape) return;
|
|
|
|
const resize = tf.image.resizeBilinear(image, [model?.inputs[0].shape[2], model?.inputs[0].shape[1]], false);
|
2021-08-01 02:42:28 +02:00
|
|
|
// 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']);
|
2021-10-22 22:09:52 +02:00
|
|
|
lastTime = now();
|
2021-08-01 02:42:28 +02:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|