首页 > 解决方案 > 使用 tensorflow-js 在网络摄像头上进行对象检测

问题描述

我正在尝试在网络摄像头流上使用 tensorflow-js 进行对象检测

下面是我的代码:

let model;

var modelPromise = new Promise(function(resolve, reject) {
      // Load the model.
      model = cocoSsd.load(); 
      $(".progress-bar").hide();

      if (model) {
        resolve("model loaded!");
      }
      else {
        reject(Error("problem loading model"));
      }
    });


const video = document.querySelector("#vid");

var camPromise = new Promise(function(resolve, reject) {
      if('mediaDevices' in navigator && 'getUserMedia' in navigator.mediaDevices){
        navigator.mediaDevices.getUserMedia({ audio: false, video: true })
          .then(function (stream) {
            video.srcObject = stream;
          })
          .catch(function (err){
            alert(JSON.stringify(error));
          });
      }
    });

// create function to detect objects in the image
const detection = (vid, mod) => {
        console.log("hi");
        mod.detect(vid).then(predictions => {
            //drawBBox(predictions); --> write function for this!
            console.log(predictions);
        });
        requestAnimationFrame(() => detection(vid, mod));
    };

Promise.all([modelPromise, camPromise])
      .then(values => detection(video, model))
      .catch(error => console.error(error));

生成并显示网络摄像头流,但 cocoSsd 没有生成预测;我在这里做错了吗?

标签: javascripttensorflow.js

解决方案


推荐阅读