Browse Source

fix(face-landmarks): dispose tensors to avoid memory leaks

Also send only expressions with score grater than 50%.
master
Gabriel Borlea 3 years ago
parent
commit
8a503e7b40
No account linked to committer's email address
1 changed files with 11 additions and 2 deletions
  1. 11
    2
      react/features/face-landmarks/faceLandmarksWorker.ts

+ 11
- 2
react/features/face-landmarks/faceLandmarksWorker.ts View File

@@ -99,8 +99,14 @@ const detectFaceBox = async ({ detections, threshold }: Detection) => {
99 99
     return faceBox;
100 100
 };
101 101
 
102
-const detectFaceExpression = async ({ detections }: Detection) => 
103
-    detections[0]?.emotion && FACE_EXPRESSIONS_NAMING_MAPPING[detections[0]?.emotion[0].emotion];
102
+const detectFaceExpression = async ({ detections }: Detection) => {
103
+    if (!detections[0]?.emotion || detections[0]?.emotion[0].score < 0.5) {
104
+        return;
105
+    }
106
+
107
+    return FACE_EXPRESSIONS_NAMING_MAPPING[detections[0]?.emotion[0].emotion];
108
+}
109
+    
104 110
 
105 111
 const detect = async ({ image, threshold } : DetectInput) => {
106 112
     let detections;
@@ -108,6 +114,7 @@ const detect = async ({ image, threshold } : DetectInput) => {
108 114
     let faceBox;
109 115
 
110 116
     detectionInProgress = true;
117
+    human.tf.engine().startScope();
111 118
 
112 119
     const imageTensor = human.tf.browser.fromPixels(image);
113 120
 
@@ -131,6 +138,8 @@ const detect = async ({ image, threshold } : DetectInput) => {
131 138
         });
132 139
     }
133 140
 
141
+    human.tf.engine().endScope()
142
+
134 143
     if (faceBox || faceExpression) {
135 144
         self.postMessage({ 
136 145
             faceBox, 

Loading…
Cancel
Save