这两天莫名其妙想了解一下和人脸识别相关的技术方案然,然后想撘一个与前端交互的人脸识别系统,然后遇到了一个比较牛逼的前端插件,反正也是比较闲了,可以先学习一下,或许将来会有用啊。
一,材料准备
二,学习笔记
step 1
下载完成后我们可以在example文件夹下面创建一个 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>
<script>
// Start tracking here...
</script>
</body>
</html>
step 2
尝试第一个功能ColorTracker,颜色追踪。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>tracking.js - first tracking</title>
<script src="../build/tracking-min.js"></script>
</head>
<body>
<div>
<video id="myVideo" width="640" height="480" preload autoplay loop muted></video>
<script type="text/javascript">
//不使用下面代码有可能无法打开Chrome摄像头
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
navigator.getUserMedia({video:true}, gotStream, noStream);
function gotStream(stream) {
myVideo.src = URL.createObjectURL(stream);
myVideo.onerror = function () {
stream.stop();
};
stream.onended = noStream;
myVideo.onloadedmetadata = function () {
alert('摄像头成功打开!');
};
}
function noStream(err)
{
alert(err);
}
// 模板代码
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>
</div>
</body>
</html>
step 3
ColorTracker 只有默认的3中颜色追踪(magenta,cyan,yellow),如果想追踪自己设定的颜色,则需要在ColorTracker当中进行注册,主要是利用(r,g,b)来注册颜色,例如绿色(0,255,0)。主要使用下面代码
tracking.ColorTracker.registerColor('green', function(r, g, b) {
if (r < 50 && g > 200 && b < 50) {
return true;
}
return false;
});
完整代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>tracking.js - first tracking</title>
<script src="../build/tracking-min.js"></script>
</head>
<body>
<div>
<video id="myVideo" width="640" height="480" preload autoplay loop muted></video>
<script type="text/javascript">
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
navigator.getUserMedia({video:true}, gotStream, noStream);
function gotStream(stream) {
myVideo.src = URL.createObjectURL(stream);
myVideo.onerror = function () {
stream.stop();
};
stream.onended = noStream;
myVideo.onloadedmetadata = function () {
alert('摄像头成功打开!');
};
}
function noStream(err)
{
alert(err);
}
// 先注册颜色
tracking.ColorTracker.registerColor('green', function(r, g, b) {
if (r < 50 && g > 200 && b < 50) {
return true;
}
return false;
});
// 调用注册颜色
var colors = new tracking.ColorTracker(['magenta', 'cyan', 'yellow','green']);
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>
</div>
</body>
</html>
这是参考官网例子的自动跟踪颜色版本
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>tracking.js - first tracking</title>
<script src="../build/tracking-min.js"></script>
<style>
.rect {
width: 80px;
height: 80px;
position: absolute;
left: -1000px;
top: -1000px;
}
</style>
</head>
<body>
<div>
<div class="demo-container" id="demo-container">
<video id="myVideo" width="640" height="480" preload autoplay loop muted></video>
</div>
<script type="text/javascript">
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
navigator.getUserMedia({video:true}, gotStream, noStream);
function gotStream(stream) {
myVideo.src = URL.createObjectURL(stream);
myVideo.onerror = function () {
stream.stop();
};
stream.onended = noStream;
myVideo.onloadedmetadata = function () {
alert('摄像头成功打开!');
};
}
function noStream(err)
{
alert(err);
}
tracking.ColorTracker.registerColor('green', function(r, g, b) {
if (r < 200 && g > 200 && b < 200) {
return true;
}
return false;
});
var rect = undefined;
var colors = new tracking.ColorTracker(['magenta', 'cyan', 'yellow','green']);
colors.on('track', function(event)
{
if (event.data.length === 0)
{
// No colors were detected in this frame.
}
else
{
event.data.forEach(function(rect)
{
//如果rect不是undefined 代表有东西,则代表有框框,则可以删除
if(rect != undefined)
{
var elements = document.getElementsByClassName('rect');
while(elements.length > 0)
{
elements[0].parentNode.removeChild(elements[0]);
}
}
window.plot(rect.x, rect.y, rect.width, rect.height, rect.color);
});
}
});
// 删除框框
window.plot = function(x, y, w, h, color)
{
rect = document.createElement('div');
document.querySelector('.demo-container').appendChild(rect);
rect.classList.add('rect');
rect.style.border = '2px solid ' + color;
rect.style.width = w + 'px';
rect.style.height = h + 'px';
rect.style.left = (myVideo.offsetLeft + x) + 'px';
rect.style.top = (myVideo.offsetTop + y) + 'px';
};
tracking.track('#myVideo', colors);
</script>
</div>
</body>
</html>
step 4
Object Tracker 是经过训练的一系统物体追踪库,同样好像使用Color Tracker的套路,通过初始化‘track’,注册‘track’,开启‘track’,即可对眼睛,人脸,嘴巴等进行识别,不过个人感觉准确率其实不是特别的高,侦测范围还是比较小,受环境影响有点大。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>tracking.js - first tracking</title>
<script src="../build/tracking.js"></script>
<script src="../build/data/face.js"></script>
<script src="../build/data/eye.js"></script>
<script src="../build/data/mouth.js"></script>
<style>
.rect {
width: 80px;
height: 80px;
position: absolute;
left: -1000px;
top: -1000px;
}
</style>
</head>
<body>
<div>
<div class="demo-container" id="demo-container">
<video id="myVideo" width="320" height="240" preload autoplay loop muted style="position: absolute"></video>
<canvas id="canvas" width="320" height="240" style="position: absolute" ></canvas>
</div>
<script type="text/javascript">
//激活摄像头
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
navigator.getUserMedia({video:true}, gotStream, noStream);
function gotStream(stream) {
myVideo.src = URL.createObjectURL(stream);
myVideo.onerror = function () {
stream.stop();
};
stream.onended = noStream;
myVideo.onloadedmetadata = function () {
alert('摄像头成功打开!');
};
}
function noStream(err)
{
alert(err);
}
//获取画布
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
//检测人脸
var objects = new tracking.ObjectTracker(['face']);
objects.on('track',function (event)
{
context.clearRect(0, 0, canvas.width, canvas.height);
if(event.data.length === 0)
{
}
else
{
event.data.forEach(function(rect)
{
//在图中画框框
context.strokeStyle = '#a64ceb';
context.strokeRect(rect.x, rect.y, rect.width, rect.height);
context.font = '11px Helvetica';
context.fillStyle = "#fff";
context.fillText('x: ' + rect.x + 'px', rect.x + rect.width + 5, rect.y + 11);
context.fillText('y: ' + rect.y + 'px', rect.x + rect.width + 5, rect.y + 22);
});
}
});
//激活
tracking.track('#myVideo', objects);
</script>
</div>
</body>
</html>
除此之外,Tracking.js 还提供了特征的自训练接口,通过自训练,一样可以使用用户自己的侦查,通过配合Tracking.js 的强大接口,我想越来越多的计算将一点点的搬到前端去了,实在强大啊。更多摸索自己可以查看下tracking官网和一些大牛的博客吧。这只是几个简单的实验罢了,挺有趣,以后再深入研究这个东西。