From 7e5ffdb3906d80bdbf2976d5f85d3e79f7e6455a Mon Sep 17 00:00:00 2001 From: Avram Tudor Date: Mon, 28 Mar 2022 14:13:00 +0300 Subject: [PATCH] fix(face-centering) fix face centering on browsers with no offscreencanvas support (#11234) --- .../face-centering/faceCenteringWorker.js | 14 +++++++------- react/features/face-centering/functions.js | 18 +++++++++++++++++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/react/features/face-centering/faceCenteringWorker.js b/react/features/face-centering/faceCenteringWorker.js index ad16eb62b..9472e1ade 100644 --- a/react/features/face-centering/faceCenteringWorker.js +++ b/react/features/face-centering/faceCenteringWorker.js @@ -35,7 +35,7 @@ const queue = []; let lastValidFaceBox; const detect = async message => { - const { baseUrl, imageBitmap, isHorizontallyFlipped, threshold } = message.data; + const { baseUrl, image, isHorizontallyFlipped, threshold } = message.data; if (initInProgress || initError) { return; @@ -70,8 +70,8 @@ const detect = async message => { tf.engine().startScope(); - const image = tf.browser.fromPixels(imageBitmap); - const detections = await model.estimateFaces(image, false, isHorizontallyFlipped, false); + const imageTensor = tf.browser.fromPixels(image); + const detections = await model.estimateFaces(imageTensor, false, isHorizontallyFlipped, false); tf.engine().endScope(); @@ -80,10 +80,10 @@ const detect = async message => { if (detections.length) { faceBox = { // normalize to percentage based - left: Math.round(Math.min(...detections.map(d => d.topLeft[0])) * 100 / imageBitmap.width), - right: Math.round(Math.max(...detections.map(d => d.bottomRight[0])) * 100 / imageBitmap.width), - top: Math.round(Math.min(...detections.map(d => d.topLeft[1])) * 100 / imageBitmap.height), - bottom: Math.round(Math.max(...detections.map(d => d.bottomRight[1])) * 100 / imageBitmap.height) + left: Math.round(Math.min(...detections.map(d => d.topLeft[0])) * 100 / image.width), + right: Math.round(Math.max(...detections.map(d => d.bottomRight[0])) * 100 / image.width), + top: Math.round(Math.min(...detections.map(d => d.topLeft[1])) * 100 / image.height), + bottom: Math.round(Math.max(...detections.map(d => d.bottomRight[1])) * 100 / image.height) }; if (lastValidFaceBox && Math.abs(lastValidFaceBox.left - faceBox.left) < threshold) { diff --git a/react/features/face-centering/functions.js b/react/features/face-centering/functions.js index 663934a53..9f214978f 100644 --- a/react/features/face-centering/functions.js +++ b/react/features/face-centering/functions.js @@ -44,6 +44,7 @@ export async function sendDataToWorker( } let imageBitmap; + let image; try { imageBitmap = await imageCapture.grabFrame(); @@ -53,13 +54,28 @@ export async function sendDataToWorker( return; } + if (typeof OffscreenCanvas === 'undefined') { + const canvas = document.createElement('canvas'); + const context = canvas.getContext('2d'); + + canvas.width = imageBitmap.width; + canvas.height = imageBitmap.height; + context.drawImage(imageBitmap, 0, 0); + + image = context.getImageData(0, 0, imageBitmap.width, imageBitmap.height); + } else { + image = imageBitmap; + } + worker.postMessage({ id: DETECT_FACE_BOX, baseUrl: getBaseUrl(), - imageBitmap, + image, threshold, isHorizontallyFlipped }); + + imageBitmap.close(); } /**