-1

有没有一种方法可以计算实时摄像机中的人脸数量。

例如,我在网络摄像头前有三个人,我有 2 个页面,分别是Success.htmlerror .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.

请让我知道如何在此窗口中检测多个面孔。

谢谢

4

2 回答 2

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为您提供了跟踪元素的数量。

于 2017-06-21T07:56:25.997 回答
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');
    });
});
于 2017-06-21T07:57:33.803 回答