human/src/gear/ssrnet-age.ts

63 lines
1.9 KiB
TypeScript
Raw Normal View History

2021-05-25 14:58:20 +02:00
/**
* Age model implementation
*
* Based on: [**SSR-Net**](https://github.com/shamangary/SSR-Net)
*
* Obsolete and replaced by `faceres` that performs age/gender/descriptor analysis
2021-05-25 14:58:20 +02:00
*/
2021-09-27 19:58:13 +02:00
import { log, join } from '../util/util';
2020-11-18 14:26:28 +01: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-09-17 17:23:00 +02:00
let model: GraphModel | null;
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
// 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-02-08 18:47:38 +01:00
if (!model) {
2021-08-17 14:51:17 +02:00
model = await tf.loadGraphModel(join(config.modelBasePath, config.face.age.modelPath)) as unknown as GraphModel;
2021-06-07 02:34:29 +02:00
if (!model || !model['modelUrl']) log('load model failed:', config.face.age.modelPath);
else if (config.debug) log('load model:', model['modelUrl']);
2021-09-17 17:23:00 +02:00
} else {
if (config.debug) log('cached model:', model['modelUrl']);
}
2021-02-08 18:47:38 +01:00
return model;
2020-11-06 17:39:39 +01:00
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function predict(image: Tensor, config: Config | any) {
2021-02-08 18:47:38 +01:00
if (!model) return null;
if ((skipped < config.face.age.skipFrames) && config.skipFrame && last.age && (last.age > 0)) {
2020-12-11 16:11:49 +01:00
skipped++;
2020-11-06 19:50:16 +01:00
return last;
}
skipped = 0;
2020-11-06 17:39:39 +01:00
return new Promise(async (resolve) => {
2021-09-17 17:23:00 +02:00
if (!model?.inputs || !model.inputs[0] || !model.inputs[0].shape) return;
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-04-25 19:16:04 +02:00
if (config.face.age.enabled) ageT = await model.predict(enhance);
2021-07-29 22:06:03 +02:00
tf.dispose(enhance);
2020-11-06 17:39:39 +01:00
2021-02-08 18:47:38 +01:00
if (ageT) {
2021-08-12 15:31:16 +02:00
const data = await ageT.data();
2021-02-08 18:47:38 +01:00
obj.age = Math.trunc(10 * data[0]) / 10;
2021-02-08 17:39:09 +01:00
}
2021-07-29 22:06:03 +02:00
tf.dispose(ageT);
2021-02-08 18:47:38 +01:00
last = obj;
2020-11-06 17:39:39 +01:00
resolve(obj);
});
}