使用 javascript 计算面数
Count number of faces using javascript
有什么方法可以计算实时摄像头中的人脸数量。
例如,我有三个人在网络摄像头前,我有 2 个页面说,Success.html 和 error。html 作为判断重定向到 Success 或 error 页面的基础方法的一部分,条件是,如果只检测到一张脸,应该发送到success,否则,应该发送到error。
我正在使用 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
。
请告诉我如何在此 window 中检测多张面孔。
谢谢
来自文档:
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
给出了跟踪元素的数量。
来自 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');
});
});
有什么方法可以计算实时摄像头中的人脸数量。
例如,我有三个人在网络摄像头前,我有 2 个页面说,Success.html 和 error。html 作为判断重定向到 Success 或 error 页面的基础方法的一部分,条件是,如果只检测到一张脸,应该发送到success,否则,应该发送到error。
我正在使用 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
。
请告诉我如何在此 window 中检测多张面孔。
谢谢
来自文档:
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
给出了跟踪元素的数量。
来自 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');
});
});