fix(facial-recognition) avoid image data conversion

TF 3.0 supports getting an ImageBitmap in fromPixels: https://js.tensorflow.org/api/3.0.0/#browser.fromPixels
This commit is contained in:
Saúl Ibarra Corretgé 2022-02-25 12:16:39 +01:00 committed by Saúl Ibarra Corretgé
parent 1d275e1976
commit c2a3d29353
2 changed files with 3 additions and 12 deletions

View File

@ -58,7 +58,7 @@ onmessage = async function(message) {
} }
case SET_TIMEOUT : { case SET_TIMEOUT : {
if (!message.data.imageData || !modelsURL) { if (!message.data.imageBitmap || !modelsURL) {
self.postMessage({ self.postMessage({
type: FACIAL_EXPRESSION_MESSAGE, type: FACIAL_EXPRESSION_MESSAGE,
value: null value: null
@ -72,7 +72,7 @@ onmessage = async function(message) {
modelsLoaded = true; modelsLoaded = true;
} }
faceapi.tf.engine().startScope(); faceapi.tf.engine().startScope();
const tensor = faceapi.tf.browser.fromPixels(message.data.imageData); const tensor = faceapi.tf.browser.fromPixels(message.data.imageBitmap);
const detections = await faceapi.detectSingleFace( const detections = await faceapi.detectSingleFace(
tensor, tensor,
new faceapi.TinyFaceDetectorOptions() new faceapi.TinyFaceDetectorOptions()

View File

@ -131,17 +131,8 @@ export async function sendDataToWorker(
return; return;
} }
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = imageBitmap.width;
canvas.height = imageBitmap.height;
context.drawImage(imageBitmap, 0, 0);
const imageData = context.getImageData(0, 0, imageBitmap.width, imageBitmap.height);
worker.postMessage({ worker.postMessage({
type: SET_TIMEOUT, type: SET_TIMEOUT,
imageData imageBitmap
}); });
} }