tracking.js實現前端人臉識別功能
阿新 • • 發佈:2020-04-19
1.下載
https://trackingjs.com/
2.執行例子
納總一下
發現效果
裡面的程式碼為
<!doctype html> <html> <head> <meta charset="utf-8"> <title>tracking.js - face hello world</title> <link rel="stylesheet" href="assets/demo.css" rel="external nofollow" rel="external nofollow" > <script src="build/tracking-min.js"></script> <script src="build/data/face-min.js"></script> <script src="build/data/eye-min.js"></script> <script src="build/data/mouth-min.js"></script> <style> .rect { border: 2px solid #a64ceb; left: -1000px; position: absolute; top: -1000px; } #img { position: absolute; top: 50%; left: 50%; margin: -173px 0 0 -300px; } </style> </head> <body> <!-- <div class="demo-title"> <p><a href="http://trackingjs.com" rel="external nofollow" target="_parent">tracking.js</a> - detect faces,eyes and mouths in a image</p> </div> --> <!-- <div class="demo-frame"> --> <div class="demo-container"> <img id="img" src=" http://img3.imgtn.bdimg.com/it/u=2939771753,2928311039&fm=214&gp=0.jpg " /> </div> <!-- </div> --> <script> window.onload = function() { var img = document.getElementById('img'); var tracker = new tracking.ObjectTracker(['face','eye','mouth']); tracker.setStepSize(1.7); tracking.track('#img',tracker); tracker.on('track',function(event) { event.data.forEach(function(rect) { window.plot(rect.x,rect.y,rect.width,rect.height); }); }); window.plot = function(x,y,w,h) { var rect = document.createElement('div'); document.querySelector('.demo-container').appendChild(rect); rect.classList.add('rect'); rect.style.width = w + 'px'; rect.style.height = h + 'px'; rect.style.left = (img.offsetLeft + x) + 'px'; rect.style.top = (img.offsetTop + y) + 'px'; }; }; </script> </body> </html>
這裡要注意,人臉必須是解決跨域了的,讀取本地圖片可能會包跨域問題
讀取攝像頭
<!doctype html> <html> <head> <meta charset="utf-8"> <title>tracking.js - face with camera</title> <link rel="stylesheet" href="assets/demo.css" rel="external nofollow" rel="external nofollow" > <script src="build/tracking-min.js"></script> <script src="build/data/face-min.js"></script> <script src="node_modules/dat.gui/build/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-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,canvas.width,canvas.height); event.data.forEach(function(rect) { context.strokeStyle = '#a64ceb'; context.strokeRect(rect.x,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.y + 22); }); }); var gui = new dat.GUI(); gui.add(tracker,'edgesDensity',0.1,0.5).step(0.01); gui.add(tracker,'initialScale',1.0,10.0).step(0.1); gui.add(tracker,'stepSize',1,5).step(0.1); }; </script> </body> </html>
總結
到此這篇關於tracking.js實現前端人臉識別功能的文章就介紹到這了,更多相關tracking.js人臉識別內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!