2021-05-25 14:58:20 +02:00
|
|
|
/**
|
2021-09-25 17:51:15 +02:00
|
|
|
* Age model implementation
|
|
|
|
*
|
|
|
|
* Based on: [**SSR-Net**](https://github.com/shamangary/SSR-Net)
|
2021-05-25 14:58:20 +02:00
|
|
|
*/
|
|
|
|
|
2021-10-22 22:09:52 +02:00
|
|
|
import { log, join, now } from '../util/util';
|
2020-11-18 14:26:28 +01:00
|
|
|
import * as tf from '../../dist/tfjs.esm.js';
|
2021-11-17 00:31:07 +01:00
|
|
|
import { env } from '../util/env';
|
2021-11-17 02:16:49 +01:00
|
|
|
import { constants } from '../tfjs/constants';
|
2021-09-13 19:28:35 +02:00
|
|
|
import type { Config } from '../config';
|
|
|
|
import type { GraphModel, Tensor } from '../tfjs/types';
|
2021-06-05 17:54:49 +02:00
|
|
|
|
2021-09-17 17:23:00 +02:00
|
|
|
let model: GraphModel | null;
|
2021-11-13 18:23:32 +01:00
|
|
|
const last: Array<{ age: number }> = [];
|
|
|
|
let lastCount = 0;
|
2021-10-22 22:09:52 +02:00
|
|
|
let lastTime = 0;
|
2020-12-11 16:11:49 +01:00
|
|
|
let skipped = Number.MAX_SAFE_INTEGER;
|
2020-11-06 17:39:39 +01:00
|
|
|
|
2021-06-05 17:54:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2021-11-13 18:23:32 +01:00
|
|
|
export async function load(config: Config) {
|
2021-09-17 17:23:00 +02:00
|
|
|
if (env.initial) model = null;
|
2021-02-08 18:47:38 +01:00
|
|
|
if (!model) {
|
2021-11-13 18:23:32 +01:00
|
|
|
model = await tf.loadGraphModel(join(config.modelBasePath, config.face['ssrnet'].modelPathAge)) as unknown as GraphModel;
|
|
|
|
if (!model || !model['modelUrl']) log('load model failed:', config.face['ssrnet'].modelPathAge);
|
2021-06-07 02:34:29 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-05 17:54:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2021-11-13 18:23:32 +01:00
|
|
|
export async function predict(image: Tensor, config: Config, idx, count): Promise<{ age: number }> {
|
|
|
|
if (!model) return { age: 0 };
|
|
|
|
const skipFrame = skipped < (config.face['ssrnet']?.skipFrames || 0);
|
|
|
|
const skipTime = (config.face['ssrnet']?.skipTime || 0) > (now() - lastTime);
|
|
|
|
if (config.skipAllowed && skipFrame && skipTime && (lastCount === count) && last[idx]?.age && (last[idx]?.age > 0)) {
|
2020-12-11 16:11:49 +01:00
|
|
|
skipped++;
|
2021-11-13 18:23:32 +01:00
|
|
|
return last[idx];
|
2020-11-06 19:50:16 +01:00
|
|
|
}
|
2021-05-18 17:26:16 +02:00
|
|
|
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-11-13 18:23:32 +01:00
|
|
|
const t: Record<string, Tensor> = {};
|
|
|
|
t.resize = tf.image.resizeBilinear(image, [model.inputs[0].shape[2], model.inputs[0].shape[1]], false);
|
2021-11-17 00:31:07 +01:00
|
|
|
t.enhance = tf.mul(t.resize, constants.tf255);
|
2021-02-08 18:47:38 +01:00
|
|
|
const obj = { age: 0 };
|
2021-11-13 18:23:32 +01:00
|
|
|
if (config.face['ssrnet'].enabled) t.age = model.execute(t.enhance) as Tensor;
|
|
|
|
if (t.age) {
|
|
|
|
const data = await t.age.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-11-13 18:23:32 +01:00
|
|
|
Object.keys(t).forEach((tensor) => tf.dispose(t[tensor]));
|
|
|
|
last[idx] = obj;
|
|
|
|
lastCount = count;
|
|
|
|
lastTime = now();
|
2020-11-06 17:39:39 +01:00
|
|
|
resolve(obj);
|
|
|
|
});
|
|
|
|
}
|