2021-02-08 17:39:09 +01:00
|
|
|
import { log } from '../log';
|
2020-11-18 14:26:28 +01:00
|
|
|
import * as tf from '../../dist/tfjs.esm.js';
|
2021-02-13 15:16:41 +01:00
|
|
|
import * as profile from '../profile';
|
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 }> = [];
|
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-02-08 17:39:09 +01:00
|
|
|
export async function load(config) {
|
2021-02-08 18:47:38 +01:00
|
|
|
if (!model) {
|
|
|
|
model = await tf.loadGraphModel(config.face.emotion.modelPath);
|
2021-03-02 17:27:42 +01:00
|
|
|
if (config.debug) log(`load model: ${config.face.emotion.modelPath.match(/\/(.*)\./)[1]}`);
|
2020-11-07 16:37:19 +01:00
|
|
|
}
|
2021-02-08 18:47:38 +01:00
|
|
|
return model;
|
2020-10-15 00:22:38 +02:00
|
|
|
}
|
|
|
|
|
2021-02-08 17:39:09 +01:00
|
|
|
export async function predict(image, config) {
|
2021-02-08 18:47:38 +01:00
|
|
|
if (!model) return null;
|
2020-12-11 16:11:49 +01:00
|
|
|
if ((skipped < config.face.emotion.skipFrames) && config.videoOptimized && (last.length > 0)) {
|
|
|
|
skipped++;
|
2020-11-06 19:50:16 +01:00
|
|
|
return last;
|
|
|
|
}
|
2020-12-11 16:11:49 +01:00
|
|
|
if (config.videoOptimized) skipped = 0;
|
|
|
|
else skipped = Number.MAX_SAFE_INTEGER;
|
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);
|
|
|
|
resize.dispose();
|
|
|
|
// 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]);
|
|
|
|
red.dispose();
|
|
|
|
green.dispose();
|
|
|
|
blue.dispose();
|
|
|
|
const grayscale = tf.addN([redNorm, greenNorm, blueNorm]);
|
|
|
|
redNorm.dispose();
|
|
|
|
greenNorm.dispose();
|
|
|
|
blueNorm.dispose();
|
|
|
|
const normalize = tf.tidy(() => grayscale.sub(0.5).mul(2));
|
|
|
|
grayscale.dispose();
|
2021-02-08 18:47:38 +01:00
|
|
|
const obj: Array<{ score: number, emotion: string }> = [];
|
2020-11-06 17:39:39 +01:00
|
|
|
if (config.face.emotion.enabled) {
|
|
|
|
let data;
|
|
|
|
if (!config.profile) {
|
2021-03-10 15:44:45 +01:00
|
|
|
const emotionT = await model.predict(normalize); // result is already in range 0..1, no need for additional activation
|
2020-11-06 17:39:39 +01:00
|
|
|
data = emotionT.dataSync();
|
|
|
|
tf.dispose(emotionT);
|
|
|
|
} else {
|
2021-02-08 18:47:38 +01:00
|
|
|
const profileData = await tf.profile(() => model.predict(normalize));
|
2020-11-06 17:39:39 +01:00
|
|
|
data = profileData.result.dataSync();
|
|
|
|
profileData.result.dispose();
|
|
|
|
profile.run('emotion', profileData);
|
|
|
|
}
|
|
|
|
for (let i = 0; i < data.length; i++) {
|
2021-03-10 15:44:45 +01:00
|
|
|
if (data[i] > config.face.emotion.minConfidence) 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
|
|
|
}
|
2020-11-06 17:39:39 +01:00
|
|
|
normalize.dispose();
|
|
|
|
last = obj;
|
|
|
|
resolve(obj);
|
|
|
|
});
|
2020-10-15 00:22:38 +02:00
|
|
|
}
|