瀏覽代碼

实时检测参数优化

Michael Wang 5 年之前
父節點
當前提交
e3004a624e

+ 13 - 20
src/features/OnlineExam/Examing/FaceMotion/FaceMotion.vue

@@ -156,33 +156,26 @@ window.faceapi = faceapi;
 
 // let withBoxes = true;
 
-const os = (function() {
-  const ua = navigator.userAgent.toLowerCase();
-  return {
-    isWin2K: /windows nt 5.0/.test(ua),
-    isXP: /windows nt 5.1/.test(ua),
-    isVista: /windows nt 6.0/.test(ua),
-    isWin7: /windows nt 6.1/.test(ua),
-    isWin8: /windows nt 6.2/.test(ua),
-    isWin81: /windows nt 6.3/.test(ua),
-    isWin10: /windows nt 10.0/.test(ua),
-  };
-})();
+// const os = (function() {
+//   const ua = navigator.userAgent.toLowerCase();
+//   return {
+//     isWin2K: /windows nt 5.0/.test(ua),
+//     isXP: /windows nt 5.1/.test(ua),
+//     isVista: /windows nt 6.0/.test(ua),
+//     isWin7: /windows nt 6.1/.test(ua),
+//     isWin8: /windows nt 6.2/.test(ua),
+//     isWin81: /windows nt 6.3/.test(ua),
+//     isWin10: /windows nt 10.0/.test(ua),
+//   };
+// })();
 
 // tiny_face_detector options
 
 function getFaceDetectorOptions() {
-  let inputSize = 160;
-  if (os.isWin7) {
-    inputSize = 256; // 在win7上无bug,速度快,效果较好
-  } else if (os.isWin10) {
-    inputSize = 320; // 在win10上,效果较好
-  }
-
   window.____hideMe =
     window.____hideMe ||
     new faceapi.TinyFaceDetectorOptions({
-      inputSize, // 这行是解决Box.constructor - expected box to be IBoundingBox | IRect, instead have 问题的关键
+      inputSize: window.__inputSize || 128, // 这行是解决Box.constructor - expected box to be IBoundingBox | IRect, instead have 问题的关键
       scoreThreshold: 0.5,
     });
   return window.____hideMe;

+ 15 - 7
src/features/OnlineExam/Examing/FaceTracking.vue

@@ -58,14 +58,15 @@ function tensorFlowWebPackStatus() {
 
 // if (os.isWin10) alert("是win10");
 
-let __inputSize = 320;
+let __inputSize = 128;
 
 async function detectTest() {
   const inputSizeList = [128, 160, 224, 320, 416, 512, 608];
   const succRate = [0, 0, 0, 0, 0, 0, 0];
+  const detectTimes = 6;
 
   for (let idx = 0; idx < inputSizeList.length; idx++) {
-    for (let n = 0; n < 10; n++) {
+    for (let n = 0; n < detectTimes; n++) {
       await new Promise(resolve => setTimeout(resolve, 3 * 1000));
       const inputSize = inputSizeList[idx];
       const videoEl = document.getElementById("video");
@@ -74,7 +75,12 @@ async function detectTest() {
           inputSize: inputSize,
           scoreThreshold: 0.5,
         });
-        const result = await faceapi.detectAllFaces(videoEl, options);
+        // const result = await faceapi.detectAllFaces(videoEl, options);
+        // console.log(result);
+        const result = await Promise.race([
+          faceapi.detectAllFaces(videoEl, options),
+          new Promise(resolve => setTimeout(resolve, 10 * 1000)),
+        ]);
 
         if (result && result.length >= 1) {
           console.log(`inputSize: ${inputSize} ${result.length}`);
@@ -83,14 +89,15 @@ async function detectTest() {
           console.log(`inputSize: ${inputSize} 检测失败`);
         }
       } catch (error) {
+        console.log(error);
         console.log(`inputSize: ${inputSize} 检测失败-异常`);
       }
     }
 
-    // if (succRate[idx] === 10) {
-    //   console.log(`inputSize: ${inputSizeList[idx]} 提前选中`);
-    //   break;
-    // }
+    if (succRate[idx] === detectTimes) {
+      console.log(`inputSize: ${inputSizeList[idx]} 提前选中`);
+      break;
+    }
   }
 
   console.log({ succRate });
@@ -99,6 +106,7 @@ async function detectTest() {
   const idx = succRate.indexOf(max);
 
   __inputSize = inputSizeList[idx];
+  window.__inputSize = __inputSize;
   console.log("最好的 inputSize 为: " + __inputSize);
   window._hmt.push([
     "_trackEvent",