Skip to content

Commit

Permalink
fix(face-landmarks): dispose tensors to avoid memory leaks
Browse files Browse the repository at this point in the history
Also send only expressions with score grater than 50%.
  • Loading branch information
gabiborlea authored May 18, 2022
1 parent 4f49cde commit 8a503e7
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions react/features/face-landmarks/faceLandmarksWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,22 @@ const detectFaceBox = async ({ detections, threshold }: Detection) => {
return faceBox;
};

const detectFaceExpression = async ({ detections }: Detection) =>
detections[0]?.emotion && FACE_EXPRESSIONS_NAMING_MAPPING[detections[0]?.emotion[0].emotion];
const detectFaceExpression = async ({ detections }: Detection) => {
if (!detections[0]?.emotion || detections[0]?.emotion[0].score < 0.5) {
return;
}

return FACE_EXPRESSIONS_NAMING_MAPPING[detections[0]?.emotion[0].emotion];
}


const detect = async ({ image, threshold } : DetectInput) => {
let detections;
let faceExpression;
let faceBox;

detectionInProgress = true;
human.tf.engine().startScope();

const imageTensor = human.tf.browser.fromPixels(image);

Expand All @@ -131,6 +138,8 @@ const detect = async ({ image, threshold } : DetectInput) => {
});
}

human.tf.engine().endScope()

if (faceBox || faceExpression) {
self.postMessage({
faceBox,
Expand Down

0 comments on commit 8a503e7

Please sign in to comment.