使用javascript

时间:2017-06-21 07:52:43

标签: javascript html tracking.js

有没有办法可以计算实时相机中的脸部数量。

例如我在网络摄像头前有三个人,我有2页说, Success.html 错误.html ,并且作为判断重定向到成功错误页面的基本方法,如果只检测到一个面,则应将其发送到成功 ,否则,它应该被发送到错误

我正在使用 tracking.js 来检测网页中的脸部,目前我的代码如下所示。

<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>tracking.js - face with camera</title>
<link rel="stylesheet" href="assets/demo.css">

<script src="js/tracking-min.js"></script>
<script src="js/data/face-min.js"></script>
<script src="js/dat.gui.min.js"></script>
<script src="assets/stats.min.js"></script>

<style>
video, canvas {
    margin-left: 230px;
    margin-top: 120px;
    position: absolute;
}
</style>
</head>

<body>
    <div class="demo-title">
        <p>
            <a href="http://trackingjs.com" target="_parent">tracking.js</a> -
            get user's webcam and detect faces
        </p>
    </div>

    <div class="demo-frame">
        <div class="demo-container">
            <video id="video" width="320" height="240" preload autoplay loop
                muted></video>
            <canvas id="canvas" width="320" height="240"></canvas>
        </div>
    </div>

    <script>
        window.onload = function() {
            var video = document.getElementById('video');
            var canvas = document.getElementById('canvas');
            var context = canvas.getContext('2d');

            var tracker = new tracking.ObjectTracker('face');
            tracker.setInitialScale(4);
            tracker.setStepSize(2);
            tracker.setEdgesDensity(0.1);

            tracking.track('#video', tracker, {
                camera : true
            });

            tracker.on('track', function(event) {
                context.clearRect(0, 0, canvas.width, canvas.height);

                event.data
                        .forEach(function(rect) {

                    console.log('face detected');
                });
            });

    </script>

</body>

</html>

在我的控制台中,我可以打印face detected

请告诉我如何在此窗口中检测多个面孔。

由于

2 个答案:

答案 0 :(得分:1)

来自文档:

myTracker.on('track', function(event) {
  if (event.data.length === 0) {
    // No targets were detected in this frame.
  } else {
    event.data.forEach(function(data) {
      // Plots the detected targets here.
    });
  }
});

似乎event.data.length为您提供了跟踪元素的数量。

答案 1 :(得分:1)

通过https://trackingjs.com/docs.html#trackers

了解文档
myTracker.on('track', function(event) {
  if (event.data.length === 0) {
    // No targets were detected in this frame.
  } else {
    event.data.forEach(function(data) {
      // Plots the detected targets here.
    });
  }
});

使用event.data.length计算面数。

在你的代码上:

tracker.on('track', function(event) {
    context.clearRect(0, 0, canvas.width, canvas.height);
    // console log amount of elements found
    console.log(event.data.length);
    event.data.forEach(function(rect) {
        console.log('face detected');
    });
});