2021-11-17 14:33:03 +00:00
|
|
|
// @flow
|
|
|
|
import 'image-capture';
|
|
|
|
import './createImageBitmap';
|
|
|
|
|
2022-04-04 13:09:14 +00:00
|
|
|
import { getCurrentConference } from '../base/conference';
|
|
|
|
import { getLocalParticipant, getParticipantCount } from '../base/participants';
|
2022-03-02 12:39:50 +00:00
|
|
|
import { getLocalVideoTrack } from '../base/tracks';
|
|
|
|
import { getBaseUrl } from '../base/util';
|
|
|
|
|
2021-11-17 14:33:03 +00:00
|
|
|
import {
|
2022-04-06 09:10:31 +00:00
|
|
|
ADD_FACE_EXPRESSION,
|
|
|
|
ADD_TO_FACE_EXPRESSIONS_BUFFER,
|
|
|
|
CLEAR_FACE_EXPRESSIONS_BUFFER,
|
|
|
|
START_FACE_LANDMARKS_DETECTION,
|
|
|
|
STOP_FACE_LANDMARKS_DETECTION,
|
2022-04-04 13:09:14 +00:00
|
|
|
UPDATE_FACE_COORDINATES
|
2021-11-17 14:33:03 +00:00
|
|
|
} from './actionTypes';
|
2021-12-21 11:46:54 +00:00
|
|
|
import {
|
2022-04-04 13:09:14 +00:00
|
|
|
DETECTION_TYPES,
|
2022-02-04 07:14:52 +00:00
|
|
|
INIT_WORKER,
|
2021-12-21 11:46:54 +00:00
|
|
|
WEBHOOK_SEND_TIME_INTERVAL
|
|
|
|
} from './constants';
|
2022-04-04 13:09:14 +00:00
|
|
|
import {
|
|
|
|
getDetectionInterval,
|
|
|
|
sendDataToWorker,
|
|
|
|
sendFaceBoxToParticipants,
|
2022-04-06 09:10:31 +00:00
|
|
|
sendFaceExpressionsWebhook
|
2022-04-04 13:09:14 +00:00
|
|
|
} from './functions';
|
2021-11-17 14:33:03 +00:00
|
|
|
import logger from './logger';
|
|
|
|
|
2022-04-08 07:12:38 +00:00
|
|
|
declare var APP: Object;
|
|
|
|
|
2021-11-17 14:33:03 +00:00
|
|
|
/**
|
|
|
|
* Object containing a image capture of the local track.
|
|
|
|
*/
|
|
|
|
let imageCapture;
|
|
|
|
|
|
|
|
/**
|
2022-04-06 09:10:31 +00:00
|
|
|
* Object where the face landmarks worker is stored.
|
2021-11-17 14:33:03 +00:00
|
|
|
*/
|
|
|
|
let worker;
|
|
|
|
|
|
|
|
/**
|
2022-04-06 09:10:31 +00:00
|
|
|
* The last face expression received from the worker.
|
2021-11-17 14:33:03 +00:00
|
|
|
*/
|
2022-04-06 09:10:31 +00:00
|
|
|
let lastFaceExpression;
|
2021-11-17 14:33:03 +00:00
|
|
|
|
2021-12-21 11:46:54 +00:00
|
|
|
/**
|
2022-04-06 09:10:31 +00:00
|
|
|
* The last face expression timestamp.
|
2021-12-21 11:46:54 +00:00
|
|
|
*/
|
2022-04-06 09:10:31 +00:00
|
|
|
let lastFaceExpressionTimestamp;
|
2021-12-21 11:46:54 +00:00
|
|
|
|
2021-11-17 14:33:03 +00:00
|
|
|
/**
|
|
|
|
* How many duplicate consecutive expression occurred.
|
|
|
|
* If a expression that is not the same as the last one it is reset to 0.
|
|
|
|
*/
|
|
|
|
let duplicateConsecutiveExpressions = 0;
|
|
|
|
|
2021-12-21 11:46:54 +00:00
|
|
|
/**
|
|
|
|
* Variable that keeps the interval for sending expressions to webhook.
|
|
|
|
*/
|
2022-04-04 13:09:14 +00:00
|
|
|
let webhookSendInterval;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Variable that keeps the interval for detecting faces in a frame.
|
|
|
|
*/
|
|
|
|
let detectionInterval;
|
2021-12-21 11:46:54 +00:00
|
|
|
|
2021-11-17 14:33:03 +00:00
|
|
|
/**
|
2022-04-06 09:10:31 +00:00
|
|
|
* Loads the worker that detects the face landmarks.
|
2021-11-17 14:33:03 +00:00
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
export function loadWorker() {
|
2022-04-04 13:09:14 +00:00
|
|
|
return function(dispatch: Function, getState: Function) {
|
|
|
|
if (worker) {
|
|
|
|
logger.info('Worker has already been initialized');
|
2021-11-17 14:33:03 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2021-12-08 07:27:17 +00:00
|
|
|
|
2022-04-04 13:09:14 +00:00
|
|
|
if (navigator.product === 'ReactNative') {
|
|
|
|
logger.warn('Unsupported environment for face recognition');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const baseUrl = `${getBaseUrl()}libs/`;
|
2022-04-06 09:10:31 +00:00
|
|
|
let workerUrl = `${baseUrl}face-landmarks-worker.min.js`;
|
2021-12-08 07:27:17 +00:00
|
|
|
|
|
|
|
const workerBlob = new Blob([ `importScripts("${workerUrl}");` ], { type: 'application/javascript' });
|
|
|
|
|
|
|
|
workerUrl = window.URL.createObjectURL(workerBlob);
|
2022-04-04 13:09:14 +00:00
|
|
|
worker = new Worker(workerUrl, { name: 'Face Recognition Worker' });
|
2021-11-17 14:33:03 +00:00
|
|
|
worker.onmessage = function(e: Object) {
|
2022-04-04 13:09:14 +00:00
|
|
|
const { faceExpression, faceBox } = e.data;
|
2021-11-17 14:33:03 +00:00
|
|
|
|
2022-04-04 13:09:14 +00:00
|
|
|
if (faceExpression) {
|
2022-04-06 09:10:31 +00:00
|
|
|
if (faceExpression === lastFaceExpression) {
|
2021-11-17 14:33:03 +00:00
|
|
|
duplicateConsecutiveExpressions++;
|
|
|
|
} else {
|
2022-04-06 09:10:31 +00:00
|
|
|
if (lastFaceExpression && lastFaceExpressionTimestamp) {
|
|
|
|
dispatch(addFaceExpression(
|
|
|
|
lastFaceExpression,
|
2021-12-21 11:46:54 +00:00
|
|
|
duplicateConsecutiveExpressions + 1,
|
2022-04-06 09:10:31 +00:00
|
|
|
lastFaceExpressionTimestamp
|
2022-04-04 13:09:14 +00:00
|
|
|
));
|
2021-12-21 11:46:54 +00:00
|
|
|
}
|
2022-04-06 09:10:31 +00:00
|
|
|
lastFaceExpression = faceExpression;
|
|
|
|
lastFaceExpressionTimestamp = Date.now();
|
2021-11-17 14:33:03 +00:00
|
|
|
duplicateConsecutiveExpressions = 0;
|
|
|
|
}
|
|
|
|
}
|
2022-04-04 13:09:14 +00:00
|
|
|
|
|
|
|
if (faceBox) {
|
|
|
|
const state = getState();
|
|
|
|
const conference = getCurrentConference(state);
|
|
|
|
const localParticipant = getLocalParticipant(state);
|
|
|
|
|
|
|
|
if (getParticipantCount(state) > 1) {
|
|
|
|
sendFaceBoxToParticipants(conference, faceBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: UPDATE_FACE_COORDINATES,
|
|
|
|
faceBox,
|
|
|
|
id: localParticipant.id
|
|
|
|
});
|
|
|
|
}
|
2022-04-08 07:12:38 +00:00
|
|
|
|
|
|
|
APP.API.notifyFaceLandmarkDetected(faceBox, faceExpression);
|
2021-11-17 14:33:03 +00:00
|
|
|
};
|
2022-04-04 13:09:14 +00:00
|
|
|
|
2022-04-06 09:10:31 +00:00
|
|
|
const { faceLandmarks } = getState()['features/base/config'];
|
2022-04-04 13:09:14 +00:00
|
|
|
const detectionTypes = [
|
2022-04-06 09:10:31 +00:00
|
|
|
faceLandmarks?.enableFaceCentering && DETECTION_TYPES.FACE_BOX,
|
|
|
|
faceLandmarks?.enableFaceExpressionsDetection && DETECTION_TYPES.FACE_EXPRESSIONS
|
2022-04-04 13:09:14 +00:00
|
|
|
].filter(Boolean);
|
|
|
|
|
2021-12-08 07:27:17 +00:00
|
|
|
worker.postMessage({
|
2022-02-04 07:14:52 +00:00
|
|
|
type: INIT_WORKER,
|
2022-04-04 13:09:14 +00:00
|
|
|
baseUrl,
|
|
|
|
detectionTypes
|
2021-12-08 07:27:17 +00:00
|
|
|
});
|
2022-04-04 13:09:14 +00:00
|
|
|
|
2022-04-06 09:10:31 +00:00
|
|
|
dispatch(startFaceLandmarksDetection());
|
2021-11-17 14:33:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the recognition and detection of face expressions.
|
|
|
|
*
|
2022-04-04 13:09:14 +00:00
|
|
|
* @param {Track | undefined} track - Track for which to start detecting faces.
|
2021-11-17 14:33:03 +00:00
|
|
|
* @returns {Function}
|
|
|
|
*/
|
2022-04-06 09:10:31 +00:00
|
|
|
export function startFaceLandmarksDetection(track) {
|
2021-11-17 14:33:03 +00:00
|
|
|
return async function(dispatch: Function, getState: Function) {
|
2022-03-02 12:39:50 +00:00
|
|
|
if (!worker) {
|
2021-11-17 14:33:03 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-03-02 12:39:50 +00:00
|
|
|
|
2021-11-17 14:33:03 +00:00
|
|
|
const state = getState();
|
2022-04-06 09:10:31 +00:00
|
|
|
const { recognitionActive } = state['features/face-landmarks'];
|
2021-11-17 14:33:03 +00:00
|
|
|
|
|
|
|
if (recognitionActive) {
|
2022-04-04 13:09:14 +00:00
|
|
|
logger.log('Face recognition already active.');
|
|
|
|
|
2021-11-17 14:33:03 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-04-04 13:09:14 +00:00
|
|
|
|
|
|
|
const localVideoTrack = track || getLocalVideoTrack(state['features/base/tracks']);
|
2021-11-17 14:33:03 +00:00
|
|
|
|
|
|
|
if (localVideoTrack === undefined) {
|
2022-04-06 09:10:31 +00:00
|
|
|
logger.warn('Face landmarks detection is disabled due to missing local track.');
|
2022-04-04 13:09:14 +00:00
|
|
|
|
2021-11-17 14:33:03 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-04-04 13:09:14 +00:00
|
|
|
|
2021-11-17 14:33:03 +00:00
|
|
|
const stream = localVideoTrack.jitsiTrack.getOriginalStream();
|
|
|
|
|
2022-04-06 09:10:31 +00:00
|
|
|
dispatch({ type: START_FACE_LANDMARKS_DETECTION });
|
2021-11-17 14:33:03 +00:00
|
|
|
logger.log('Start face recognition');
|
2022-04-04 13:09:14 +00:00
|
|
|
|
2021-11-17 14:33:03 +00:00
|
|
|
const firstVideoTrack = stream.getVideoTracks()[0];
|
2022-04-06 09:10:31 +00:00
|
|
|
const { faceLandmarks } = state['features/base/config'];
|
2021-11-17 14:33:03 +00:00
|
|
|
|
|
|
|
imageCapture = new ImageCapture(firstVideoTrack);
|
|
|
|
|
2022-04-04 13:09:14 +00:00
|
|
|
detectionInterval = setInterval(() => {
|
|
|
|
sendDataToWorker(
|
|
|
|
worker,
|
|
|
|
imageCapture,
|
2022-04-06 09:10:31 +00:00
|
|
|
faceLandmarks?.faceCenteringThreshold
|
2022-04-04 13:09:14 +00:00
|
|
|
);
|
|
|
|
}, getDetectionInterval(state));
|
|
|
|
|
2022-04-06 09:10:31 +00:00
|
|
|
if (faceLandmarks?.enableFaceExpressionsDetection) {
|
2022-04-04 13:09:14 +00:00
|
|
|
webhookSendInterval = setInterval(async () => {
|
2022-04-06 09:10:31 +00:00
|
|
|
const result = await sendFaceExpressionsWebhook(getState());
|
2022-04-04 13:09:14 +00:00
|
|
|
|
|
|
|
if (result) {
|
2022-04-06 09:10:31 +00:00
|
|
|
dispatch(clearFaceExpressionBuffer());
|
2022-04-04 13:09:14 +00:00
|
|
|
}
|
|
|
|
}, WEBHOOK_SEND_TIME_INTERVAL);
|
2021-12-21 11:46:54 +00:00
|
|
|
}
|
2021-11-17 14:33:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the recognition and detection of face expressions.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-04-06 09:10:31 +00:00
|
|
|
export function stopFaceLandmarksDetection() {
|
2022-04-04 13:09:14 +00:00
|
|
|
return function(dispatch: Function) {
|
2022-04-06 09:10:31 +00:00
|
|
|
if (lastFaceExpression && lastFaceExpressionTimestamp) {
|
2021-12-21 11:46:54 +00:00
|
|
|
dispatch(
|
2022-04-06 09:10:31 +00:00
|
|
|
addFaceExpression(
|
|
|
|
lastFaceExpression,
|
2022-04-04 13:09:14 +00:00
|
|
|
duplicateConsecutiveExpressions + 1,
|
2022-04-06 09:10:31 +00:00
|
|
|
lastFaceExpressionTimestamp
|
2022-04-04 13:09:14 +00:00
|
|
|
)
|
2021-12-21 11:46:54 +00:00
|
|
|
);
|
|
|
|
}
|
2022-04-04 13:09:14 +00:00
|
|
|
|
|
|
|
clearInterval(webhookSendInterval);
|
|
|
|
clearInterval(detectionInterval);
|
|
|
|
|
2021-11-17 14:33:03 +00:00
|
|
|
duplicateConsecutiveExpressions = 0;
|
2022-04-04 13:09:14 +00:00
|
|
|
webhookSendInterval = null;
|
|
|
|
detectionInterval = null;
|
|
|
|
imageCapture = null;
|
2021-12-21 11:46:54 +00:00
|
|
|
|
2022-04-06 09:10:31 +00:00
|
|
|
dispatch({ type: STOP_FACE_LANDMARKS_DETECTION });
|
2021-11-17 14:33:03 +00:00
|
|
|
logger.log('Stop face recognition');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-06 09:10:31 +00:00
|
|
|
* Adds a new face expression and its duration.
|
2021-11-17 14:33:03 +00:00
|
|
|
*
|
2022-04-06 09:10:31 +00:00
|
|
|
* @param {string} faceExpression - Face expression to be added.
|
|
|
|
* @param {number} duration - Duration in seconds of the face expression.
|
|
|
|
* @param {number} timestamp - Duration in seconds of the face expression.
|
2021-11-17 14:33:03 +00:00
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2022-04-06 09:10:31 +00:00
|
|
|
function addFaceExpression(faceExpression: string, duration: number, timestamp: number) {
|
2021-11-17 14:33:03 +00:00
|
|
|
return function(dispatch: Function, getState: Function) {
|
2022-04-04 13:09:14 +00:00
|
|
|
const finalDuration = duration * getDetectionInterval(getState()) / 1000;
|
2021-11-17 14:33:03 +00:00
|
|
|
|
|
|
|
dispatch({
|
2022-04-06 09:10:31 +00:00
|
|
|
type: ADD_FACE_EXPRESSION,
|
|
|
|
faceExpression,
|
2021-12-21 11:46:54 +00:00
|
|
|
duration: finalDuration,
|
|
|
|
timestamp
|
2021-11-17 14:33:03 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-21 11:46:54 +00:00
|
|
|
/**
|
2022-04-06 09:10:31 +00:00
|
|
|
* Adds a face expression with its timestamp to the face expression buffer.
|
2021-12-21 11:46:54 +00:00
|
|
|
*
|
2022-04-06 09:10:31 +00:00
|
|
|
* @param {Object} faceExpression - Object containing face expression string and its timestamp.
|
2021-12-21 11:46:54 +00:00
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2022-04-06 09:10:31 +00:00
|
|
|
export function addToFaceExpressionsBuffer(faceExpression: Object) {
|
2021-12-21 11:46:54 +00:00
|
|
|
return {
|
2022-04-06 09:10:31 +00:00
|
|
|
type: ADD_TO_FACE_EXPRESSIONS_BUFFER,
|
|
|
|
faceExpression
|
2021-12-21 11:46:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-06 09:10:31 +00:00
|
|
|
* Clears the face expressions array in the state.
|
2021-12-21 11:46:54 +00:00
|
|
|
*
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2022-04-06 09:10:31 +00:00
|
|
|
function clearFaceExpressionBuffer() {
|
2021-12-21 11:46:54 +00:00
|
|
|
return {
|
2022-04-06 09:10:31 +00:00
|
|
|
type: CLEAR_FACE_EXPRESSIONS_BUFFER
|
2021-12-21 11:46:54 +00:00
|
|
|
};
|
|
|
|
}
|