2

我正在尝试熟悉 tracking.js,但是当我尝试运行项目页面 ( http://trackingjs.com/docs.html#introduction ) 上给出的相同示例时,什么也没有发生。Google-chrome 甚至没有显示询问我是否要允许该页面访问我的网络摄像头的提示。同样的事情发生在 Firefox 上。

我已经运行了 tracking.js-master/ 包中的其他示例,唯一失败的是我添加的教程中描述的示例。

下面是我从 tracking.js 介绍页面复制并粘贴到我的 first_tracking.html 文件的代码。

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>tracking.js - first tracking</title>
  <script src="../build/tracking-min.js"></script>
</head>
<body>
  <p>This shows in the browser</p>
  <video id="myVideo" width="400" height="300" preload autoplay loop muted></video>
  <script>
  var colors = new tracking.ColorTracker(['magenta', 'cyan', 'yellow']);

  colors.on('track', function(event) {
      if (event.data.length === 0) {
      // No colors were detected in this frame.
      } else {
      event.data.forEach(function(rect) {
        console.log(rect.x, rect.y, rect.height, rect.width, rect.color);
        });
      }
      });

  tracking.track('#myVideo', colors);
  </script>
</body>
</html>

有没有人尝试并成功运行了 tracking.js 页面上列出的介绍示例并有任何指示?

研究:在谷歌搜索中找到的大部分内容都与谷歌分析、Node.js 和其他 JS 框架有关。我还发现有人建议将preload选项设置为auto,导致preload="auto",但它不起作用。

4

3 回答 3

4

我能够获得使用网络摄像头的示例。就是这样。

首先,进入根 tracking.js 目录并启动一个简单的服务器。如果您安装了 python,请尝试运行python -m SimpleHTTPServer,然后访问http://localhost:8000/examples/first_tracking.html以查看您的示例,假设您的文件位于解压缩文件的示例目录中。这很重要。如果只是在浏览器中打开文件,会报错:Canvas tainted by cross-origin data

将以下代码保存到 first_tracking.html:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>tracking.js - first tracking</title>
  <script src="../build/tracking-min.js"></script>
</head>
<body>
<video id="myVideo" width="400" height="300" preload autoplay loop muted></video>
<script>
var colors = new tracking.ColorTracker(['magenta', 'cyan', 'yellow']);

colors.on('track', function(event) {
  if (event.data.length === 0) {
    // No colors were detected in this frame.
  } else {
    event.data.forEach(function(rect) {
      console.log(rect.x, rect.y, rect.height, rect.width, rect.color);
    });
  }
});

window.onload = function() {
  // note here that 'camera' is set to true, I believe this tells tracking.js to use
  // the webcam.
  tracking.track('#myVideo', colors, {camera: true});  
}

</script>
</body>
</html>

确保您的服务器仍在运行,然后访问http://localhost:8000/examples/first_tracking.html并检查控制台。您应该看到如下输出

260 47 37 47 "cyan" first_tracking.html:18

于 2014-08-22T03:37:20.187 回答
2

我最近自己遇到了这个问题,对我来说,这个问题是 tracking.track() 调用缺少参数...

网上的例子说明了这一点:

tracking.track('#myVideo', colors);

什么时候应该是这样的:

tracking.track('#myVideo', colors, { camera: true });

从技术上讲,Julia 的帖子包含这个参数,所以我可以接受它作为公认的答案,但以防万一其他人遇到这个问题,这就是为我解决的问题。使用相机标志,我也可以在不等待 dom 加载的情况下运行示例,如果这对其他人很重要的话。

于 2017-05-19T18:33:12.860 回答
1

{ camera: true }示例代码中缺少。正如之前的答案所提到的,包括tracking.track('#myVideo', colors, { camera: true });

于 2017-07-05T02:25:40.640 回答