浏览代码

实时检测:重试检测;离开当前页面停止检测。

Michael Wang 5 年之前
父节点
当前提交
4268d46e87
共有 2 个文件被更改,包括 22 次插入1 次删除
  1. 21 1
      src/features/OnlineExam/Examing/FaceTracking.vue
  2. 1 0
      src/store.js

+ 21 - 1
src/features/OnlineExam/Examing/FaceTracking.vue

@@ -58,6 +58,7 @@ function tensorFlowWebPackStatus() {
 
 // if (os.isWin10) alert("是win10");
 
+let routeId = null;
 let __inputSize = 128;
 
 async function detectTest() {
@@ -77,6 +78,9 @@ async function detectTest() {
         });
         // const result = await faceapi.detectAllFaces(videoEl, options);
         // console.log(result);
+        if (!routeId) {
+          return;
+        }
         const result = await Promise.race([
           faceapi.detectAllFaces(videoEl, options),
           new Promise(resolve => setTimeout(resolve, 10 * 1000)),
@@ -135,6 +139,7 @@ export default {
     ...mapState(["isDoingFaceLiveness"]),
   },
   async created() {
+    routeId = "created";
     await faceapi.nets.tinyFaceDetector.load(FACE_API_MODEL_PATH);
     // faceapi.nets.faceRecognitionNet.load(modelsPath);
     await faceapi.loadFaceLandmarkModel(FACE_API_MODEL_PATH);
@@ -172,6 +177,7 @@ export default {
     }
   },
   beforeDestroy() {
+    routeId = null;
     clearInterval(this.trackHeadInterval);
     clearTimeout(this.warningTimeout);
     clearTimeout(this.detectFacesTimeout);
@@ -291,6 +297,8 @@ export default {
           duration: 5,
           closable: true,
         });
+        this.failTimes = this.failTimes || 1;
+        this.failTimes++;
       }
 
       if (
@@ -303,7 +311,19 @@ export default {
         }, 3000);
       }
 
-      this.detectFacesTimeout = setTimeout(() => this.detectFaces(), 10 * 1000);
+      this.detectFacesTimeout = setTimeout(async () => {
+        if (this.failTimes > 5) {
+          this.$Message.warning({
+            content: "请保持正确坐姿,确保脸部在摄像头内,背景无强光。",
+            duration: 10,
+            closable: true,
+          });
+          this.failTimes = 1;
+          await this.detectTest();
+        }
+        if (!routeId) return;
+        await this.detectFaces();
+      }, 60 * 1000);
     },
   },
 };

+ 1 - 0
src/store.js

@@ -44,6 +44,7 @@ const examingHomeModule = {
       state = Object.assign(state, { remainTime });
     },
     toggleSnapNow(state) {
+      // FIXME: 这里对多次调用不够严谨,比如,下次调用时本意是要抓拍,但别处的watch不知道是否已经执行。最好改成event的形式。
       state.snapNow = !state.snapNow;
       if (state.snapNow) {
         state.snapProcessingCount = state.snapProcessingCount + 1;