human/src/gear/emotion.ts

72 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-05-25 14:58:20 +02:00
/**
* Emotion model implementation
*
* [**Oarriaga**](https://github.com/oarriaga/face_classification)
2021-05-25 14:58:20 +02:00
*/
2021-10-22 22:09:52 +02:00
import { log, join, now } from '../util/util';
2021-09-13 19:28:35 +02:00
import type { Config } from '../config';
2021-09-17 17:23:00 +02:00
import type { GraphModel, Tensor } from '../tfjs/types';
2020-11-18 14:26:28 +01:00
import * as tf from '../../dist/tfjs.esm.js';
2021-09-27 19:58:13 +02:00
import { env } from '../util/env';
2021-11-17 02:16:49 +01:00
import { constants } from '../tfjs/constants';
2020-10-15 00:22:38 +02:00
2021-01-06 12:51:20 +01:00
const annotations = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral'];
2021-09-17 17:23:00 +02:00
let model: GraphModel | null;
const last: Array<Array<{ score: number, emotion: string }>> = [];
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-05 21:38:09 +01:00
2021-06-03 15:41:53 +02:00
export async function load(config: Config): Promise<GraphModel> {
2021-09-17 17:23:00 +02:00
if (env.initial) model = null;
2021-02-08 18:47:38 +01:00
if (!model) {
2021-09-17 17:23:00 +02:00
model = await tf.loadGraphModel(join(config.modelBasePath, config.face.emotion?.modelPath || '')) as unknown as GraphModel;
2021-10-13 16:56:56 +02:00
if (!model || !model['modelUrl']) log('load model failed:', config.face.emotion?.modelPath);
2021-09-17 17:23:00 +02:00
else if (config.debug) log('load model:', model['modelUrl']);
} else if (config.debug) log('cached model:', model['modelUrl']);
2021-02-08 18:47:38 +01:00
return model;
2020-10-15 00:22:38 +02:00
}
2021-11-17 22:50:21 +01:00
export async function predict(image: Tensor, config: Config, idx: number, count: number): Promise<Array<{ score: number, emotion: string }>> {
2021-11-13 18:23:32 +01:00
if (!model) return [];
2021-10-23 15:38:52 +02:00
const skipFrame = skipped < (config.face.emotion?.skipFrames || 0);
const skipTime = (config.face.emotion?.skipTime || 0) > (now() - lastTime);
if (config.skipAllowed && skipTime && skipFrame && (lastCount === count) && last[idx] && (last[idx].length > 0)) {
2020-12-11 16:11:49 +01:00
skipped++;
return last[idx];
2020-11-06 19:50:16 +01:00
}
skipped = 0;
2020-11-06 17:39:39 +01:00
return new Promise(async (resolve) => {
2021-02-08 18:47:38 +01:00
const obj: Array<{ score: number, emotion: string }> = [];
2021-09-12 05:54:35 +02:00
if (config.face.emotion?.enabled) {
2021-11-05 16:28:06 +01:00
const t: Record<string, Tensor> = {};
2021-11-03 21:32:07 +01:00
const inputSize = model?.inputs[0].shape ? model.inputs[0].shape[2] : 0;
2021-11-05 16:28:06 +01:00
t.resize = tf.image.resizeBilinear(image, [inputSize, inputSize], false);
2021-11-03 21:32:07 +01:00
// const box = [[0.15, 0.15, 0.85, 0.85]]; // empyrical values for top, left, bottom, right
// const resize = tf.image.cropAndResize(image, box, [0], [inputSize, inputSize]);
2021-11-17 00:31:07 +01:00
// [t.red, t.green, t.blue] = tf.split(t.resize, 3, 3);
2021-10-22 22:09:52 +02:00
// weighted rgb to grayscale: https://www.mathworks.com/help/matlab/ref/rgb2gray.html
2021-11-17 00:31:07 +01:00
// t.redNorm = tf.mul(t.red, rgb[0]);
// t.greenNorm = tf.mul(t.green, rgb[1]);
// t.blueNorm = tf.mul(t.blue, rgb[2]);
// t.grayscale = tf.addN([t.redNorm, t.greenNorm, t.blueNorm]);
t.channels = tf.mul(t.resize, constants.rgb);
t.grayscale = tf.sum(t.channels, 3, true);
t.grayscaleSub = tf.sub(t.grayscale, constants.tf05);
t.grayscaleMul = tf.mul(t.grayscaleSub, constants.tf2);
2021-11-05 16:28:06 +01:00
t.emotion = model?.execute(t.grayscaleMul) as Tensor; // result is already in range 0..1, no need for additional activation
2021-10-22 22:09:52 +02:00
lastTime = now();
2021-11-05 16:28:06 +01:00
const data = await t.emotion.data();
2020-11-06 17:39:39 +01:00
for (let i = 0; i < data.length; i++) {
2021-09-12 05:54:35 +02:00
if (data[i] > (config.face.emotion?.minConfidence || 0)) obj.push({ score: Math.min(0.99, Math.trunc(100 * data[i]) / 100), emotion: annotations[i] });
2020-11-06 17:39:39 +01:00
}
obj.sort((a, b) => b.score - a.score);
2021-11-05 16:28:06 +01:00
Object.keys(t).forEach((tensor) => tf.dispose(t[tensor]));
2020-10-15 00:22:38 +02:00
}
last[idx] = obj;
lastCount = count;
2020-11-06 17:39:39 +01:00
resolve(obj);
});
2020-10-15 00:22:38 +02:00
}