這兩天莫名其妙想了解一下和人臉識別相關的技術方案然馅精,然后想撘一個與前端交互的人臉識別系統(tǒng)蛾狗,然后遇到了一個比較牛逼的前端插件骨稿,反正也是比較閑了鳖目,可以先學習一下扮叨,或許將來會有用啊。
一领迈,材料準備
二彻磁,學習筆記
step 1
下載完成后我們可以在example文件夾下面創(chuàng)建一個 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;
});
// 調(diào)用注冊顏色
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>
這是參考官網(wǎng)例子的自動跟蹤顏色版本
<!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 是經(jīng)過訓練的一系統(tǒng)物體追蹤庫梁棠,同樣好像使用Color Tracker的套路置森,通過初始化‘track’斗埂,注冊‘track’符糊,開啟‘track’,即可對眼睛呛凶,人臉男娄,嘴巴等進行識別,不過個人感覺準確率其實不是特別的高,偵測范圍還是比較小模闲,受環(huán)境影響有點大建瘫。
<!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官網(wǎng)和一些大牛的博客吧荸实。這只是幾個簡單的實驗罷了,挺有趣缴淋,以后再深入研究這個東西准给。