human/src/emotion/emotion.ts

69 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-05-25 14:58:20 +02:00
/**
* Emotion Module
*/
2021-04-09 14:07:58 +02:00
import { log, join } from '../helpers';
2021-06-03 15:41:53 +02:00
import { Config } from '../config';
import { Tensor, GraphModel } from '../tfjs/types';
2020-11-18 14:26:28 +01:00
import * as tf from '../../dist/tfjs.esm.js';
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-02-08 18:47:38 +01:00
let model;
// let last: Array<{ score: number, emotion: string }> = [];
const last: Array<Array<{ score: number, emotion: string }>> = [];
let lastCount = 0;
2020-12-11 16:11:49 +01:00
let skipped = Number.MAX_SAFE_INTEGER;
2020-11-05 21:38:09 +01:00
// tuning values
const rgb = [0.2989, 0.5870, 0.1140]; // factors for red/green/blue colors when converting to grayscale
2020-10-15 00:22:38 +02:00
2021-06-03 15:41:53 +02:00
export async function load(config: Config): Promise<GraphModel> {
2021-02-08 18:47:38 +01:00
if (!model) {
2021-09-12 05:54:35 +02:00
model = await tf.loadGraphModel(join(config.modelBasePath, config.face.emotion?.modelPath || ''));
if (!model || !model.modelUrl) log('load model failed:', config.face.emotion?.modelPath || '');
2021-04-09 14:07:58 +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-06-03 15:41:53 +02:00
export async function predict(image: Tensor, config: Config, idx, count) {
2021-02-08 18:47:38 +01:00
if (!model) return null;
2021-09-12 05:54:35 +02:00
if ((skipped < (config.face.emotion?.skipFrames || 0)) && config.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-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 [red, green, blue] = tf.split(resize, 3, 3);
2021-07-29 22:06:03 +02:00
tf.dispose(resize);
2020-11-06 17:39:39 +01:00
// weighted rgb to grayscale: https://www.mathworks.com/help/matlab/ref/rgb2gray.html
const redNorm = tf.mul(red, rgb[0]);
const greenNorm = tf.mul(green, rgb[1]);
const blueNorm = tf.mul(blue, rgb[2]);
2021-07-29 22:06:03 +02:00
tf.dispose(red);
tf.dispose(green);
tf.dispose(blue);
2020-11-06 17:39:39 +01:00
const grayscale = tf.addN([redNorm, greenNorm, blueNorm]);
2021-07-29 22:06:03 +02:00
tf.dispose(redNorm);
tf.dispose(greenNorm);
tf.dispose(blueNorm);
const normalize = tf.tidy(() => tf.mul(tf.sub(grayscale, 0.5), 2));
tf.dispose(grayscale);
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-04-25 19:16:04 +02:00
const emotionT = await model.predict(normalize); // result is already in range 0..1, no need for additional activation
2021-08-12 15:31:16 +02:00
const data = await emotionT.data();
2021-04-25 19:16:04 +02:00
tf.dispose(emotionT);
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);
2020-10-15 00:22:38 +02:00
}
2021-07-29 22:06:03 +02:00
tf.dispose(normalize);
last[idx] = obj;
lastCount = count;
2020-11-06 17:39:39 +01:00
resolve(obj);
});
2020-10-15 00:22:38 +02:00
}