浏览代码

活检和实时人脸:共享摄像头

Michael Wang 5 年之前
父节点
当前提交
e6111a9053

+ 6 - 2
src/components/FaceRecognition/FaceRecognition.vue

@@ -68,6 +68,7 @@ export default {
     closeCamera: function(newValue) {
       if (newValue) {
         console.log("关闭摄像头");
+        window.__stream = null;
         if (this.$refs.video.srcObject) {
           this.$refs.video.srcObject.getTracks().forEach(function(track) {
             track.stop();
@@ -80,6 +81,7 @@ export default {
     },
   },
   async mounted() {
+    window.__stream = null;
     this.openCamera();
 
     this.checkFaceDetectorTimeout = setTimeout(() => {
@@ -90,6 +92,7 @@ export default {
     clearTimeout(this.retrySnapTimeout);
     clearTimeout(this.showSnapResultTimeout);
     clearTimeout(this.checkFaceDetectorTimeout);
+    window.__stream = null;
     if (this.$refs.video.srcObject) {
       this.$refs.video.srcObject.getTracks().forEach(function(track) {
         track.stop();
@@ -115,8 +118,8 @@ export default {
               facingMode: "user",
               aspectRatio: 4 / 3,
               resizeMode: "crop-and-scale",
-              width: 400,
-              height: 300,
+              width: 640,
+              height: 480,
             },
           });
           if (stream) {
@@ -127,6 +130,7 @@ export default {
               getSettings: vt0.getSettings && vt0.getSettings(),
             });
             video.srcObject = stream;
+            window.__stream = stream;
             try {
               await video.play();
               this.disableSnap = false;

+ 9 - 1
src/features/OnlineExam/Examing/FaceMotion/FaceMotion.vue

@@ -399,6 +399,11 @@ export default {
       };
     },
     async run() {
+      const videoEl = document.getElementById("inputVideo");
+      if (window.__stream) {
+        videoEl.srcObject = window.__stream;
+        return;
+      }
       // load face detection and face landmark models
       // await changeFaceDetector(TINY_FACE_DETECTOR)
       // changeInputSize(224);
@@ -428,7 +433,7 @@ export default {
       //   "video stream capabilities",
       //   stream.getVideoTracks()[0].getCapabilities()
       // );
-      const videoEl = document.getElementById("inputVideo");
+
       videoEl.srcObject = stream;
     },
     async intro() {
@@ -1068,6 +1073,9 @@ export default {
     },
     closeCamera: function() {
       console.log("关闭摄像头");
+      if (window.__stream) {
+        return;
+      }
       const video = document.getElementById("inputVideo");
       if (video && video.srcObject) {
         video.srcObject.getTracks().forEach(function(track) {