2020-10-15 00:22:38 +02:00
|
|
|
const tf = require('@tensorflow/tfjs');
|
2020-11-01 19:07:53 +01:00
|
|
|
const profile = require('../profile.js');
|
2020-10-15 00:22:38 +02:00
|
|
|
|
|
|
|
const annotations = ['angry', 'discust', 'fear', 'happy', 'sad', 'surpise', 'neutral'];
|
|
|
|
const models = {};
|
|
|
|
let last = [];
|
2020-11-03 16:55:33 +01:00
|
|
|
let frame = Number.MAX_SAFE_INTEGER;
|
2020-11-05 21:38:09 +01:00
|
|
|
|
|
|
|
// tuning values
|
|
|
|
const zoom = [0, 0]; // 0..1 meaning 0%..100%
|
|
|
|
const rgb = [0.2989, 0.5870, 0.1140]; // factors for red/green/blue colors when converting to grayscale
|
|
|
|
const scale = 1; // score multiplication factor
|
2020-10-15 00:22:38 +02:00
|
|
|
|
|
|
|
async function load(config) {
|
|
|
|
if (!models.emotion) models.emotion = await tf.loadGraphModel(config.face.emotion.modelPath);
|
2020-10-15 15:43:16 +02:00
|
|
|
return models.emotion;
|
2020-10-15 00:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function predict(image, config) {
|
2020-10-18 14:07:45 +02:00
|
|
|
if (frame < config.face.emotion.skipFrames) {
|
|
|
|
frame += 1;
|
2020-10-15 00:22:38 +02:00
|
|
|
return last;
|
|
|
|
}
|
2020-10-18 14:07:45 +02:00
|
|
|
frame = 0;
|
2020-11-05 21:38:09 +01:00
|
|
|
const box = [[
|
|
|
|
(image.shape[1] * zoom[0]) / image.shape[1],
|
|
|
|
(image.shape[2] * zoom[1]) / image.shape[2],
|
|
|
|
(image.shape[1] - (image.shape[1] * zoom[0])) / image.shape[1],
|
|
|
|
(image.shape[2] - (image.shape[2] * zoom[1])) / image.shape[2],
|
|
|
|
]];
|
|
|
|
const resize = tf.image.cropAndResize(image, box, [0], [config.face.emotion.inputSize, config.face.emotion.inputSize]);
|
|
|
|
// const resize = tf.image.resizeBilinear(image, [config.face.emotion.inputSize, config.face.emotion.inputSize], false);
|
2020-10-18 15:21:53 +02: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
|
2020-11-05 21:38:09 +01:00
|
|
|
const redNorm = tf.mul(red, rgb[0]);
|
|
|
|
const greenNorm = tf.mul(green, rgb[1]);
|
|
|
|
const blueNorm = tf.mul(blue, rgb[2]);
|
2020-10-18 15:21:53 +02:00
|
|
|
red.dispose();
|
|
|
|
green.dispose();
|
|
|
|
blue.dispose();
|
|
|
|
const grayscale = tf.addN([redNorm, greenNorm, blueNorm]);
|
2020-11-05 21:38:09 +01:00
|
|
|
const normalize = tf.tidy(() => grayscale.sub(0.5).mul(2));
|
2020-10-18 15:21:53 +02:00
|
|
|
redNorm.dispose();
|
|
|
|
greenNorm.dispose();
|
|
|
|
blueNorm.dispose();
|
2020-10-15 00:22:38 +02:00
|
|
|
const obj = [];
|
|
|
|
if (config.face.emotion.enabled) {
|
2020-11-01 19:07:53 +01:00
|
|
|
let data;
|
|
|
|
if (!config.profile) {
|
2020-11-05 21:38:09 +01:00
|
|
|
const emotionT = await models.emotion.predict(normalize);
|
2020-11-03 00:54:03 +01:00
|
|
|
data = emotionT.dataSync();
|
2020-11-01 19:07:53 +01:00
|
|
|
tf.dispose(emotionT);
|
|
|
|
} else {
|
|
|
|
const profileData = await tf.profile(() => models.emotion.predict(grayscale));
|
2020-11-03 00:54:03 +01:00
|
|
|
data = profileData.result.dataSync();
|
2020-11-01 19:07:53 +01:00
|
|
|
profileData.result.dispose();
|
|
|
|
profile.run('emotion', profileData);
|
|
|
|
}
|
2020-10-15 00:22:38 +02:00
|
|
|
for (let i = 0; i < data.length; i++) {
|
2020-11-05 21:38:09 +01:00
|
|
|
if (scale * data[i] > config.face.emotion.minConfidence) obj.push({ score: Math.min(0.99, Math.trunc(100 * scale * data[i]) / 100), emotion: annotations[i] });
|
2020-10-15 00:22:38 +02:00
|
|
|
}
|
|
|
|
obj.sort((a, b) => b.score - a.score);
|
|
|
|
}
|
2020-10-18 15:21:53 +02:00
|
|
|
tf.dispose(grayscale);
|
2020-10-15 00:22:38 +02:00
|
|
|
last = obj;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.predict = predict;
|
|
|
|
exports.load = load;
|