不能在Chrome上运行第一个tracking.js的例子

Can't run very first tracking.js example on Chrome

本文关键字:js tracking 第一个 Chrome 运行 不能      更新时间:2023-09-26

我试图熟悉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>

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

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

我能够让这个例子与网络摄像头一起工作。这是如何。

首先,进入根目录tracking.js并启动一个简单的服务器。如果您已经安装了python,请尝试运行python -m SimpleHTTPServer,然后访问http://localhost:8000/examples/first_tracking.html以查看示例,假设您的文件位于解压缩文件的示例目录中。这很重要。如果您只是在浏览器中打开该文件,您将得到一个错误:Canvas被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

我最近遇到了这个问题,我的问题是缺少一个参数的跟踪。跟踪()调用…

在线示例显示如下:

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

当它应该是:

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

从技术上讲,Julia的帖子包括这个参数,所以我可以接受这个答案,但以防别人遇到这个问题,这就是为我解决的问题。有了相机标志,我就可以在不等待dom加载的情况下运行这个例子,如果这对其他人来说很重要的话。

{ camera: true }在示例代码中缺失。如前所述,包括tracking.track('#myVideo', colors, { camera: true });