近幾年,人臉識別技術在身份認證領域的應用已經有了較多應用,例如:支付寶、招行的取款、養老金領取等方面,但在杜絕假冒、認證安全性等方面,目前還是一個比較需要進一步解決的課題,特別是在移動端的活體認證技術方面。
本文介紹了在HTML5 環境下可以采用clmtrackr.js 檢測工具,結合人臉模型,實現人臉的跟蹤檢測。同時采用動作識別實現活體認證。
但本方案只能夠在Firefox 或者Chrome中使用。並且只適合研究學習,實際場景中不太理想,需要進一步優化才能夠應用。
如果有人有相關的技術,可以推薦介紹給我。
JavaScript Code復制內容到剪貼板
- <!DOCTYPE html>
- <!--
- Ideally these elements aren't created until it's confirmed that the
- client supports video/camera, but for the sake of illustrating the
- elements involved, they are created with markup (not JavaScript)
- -->
- <html>
- <meta charset="GBK">
- <style>
- #container {
- position : relative;
- }
-
- #canvas {
- position : absolute;
- left : 0;
- top : 0;
- }
- </style>
- <script src="utils.js"></script>
- <script src="clmtrackr.js"></script>
- <script src="./models/model_pca_20_svm.js"></script>
- <script src="numeric.js"></script>
- <script src="ccv.js"></script>
-
- <audio id="media">
- 你的浏覽器不支持audio標簽。
- </audio>
- <div id="container">
- <video id="video" width="600" height="400" autoplay >
- 您的浏覽器不支持video標簽
- </video>
- <canvas id="canvas" width="600" height="400"></canvas>
- </div>
-
- <button id="snap">Snap Photo</button>
-
- <button id="start">Start</button>
-
- <button id="showposition">顯示</button>
-
- <button id="hideposition">不顯示</button>
-
- <br/>
-
- <button id="mouse">張嘴驗證</button>
- <button id="head">搖頭驗證</button>
- <button id="eye">眨眼驗證</button>
-
-
- <div id="tip">
- </div>
- <div id="result">
- </div>
- <div id="msg">
- </div>
-
- <div id="positions">
- </div>
-
- <script>
-
- var showpos=false;
- // Put event listeners into place
- //window.addEventListener("DOMContentLoaded", function() {
-
- // Grab elements, create settings, etc.
- var canvas = document.getElementById("canvas"),
- context = canvas.getContext("2d"),
- video = document.getElementById("video"),
- videoObj = { "video": true },
- errBack = function(error) {
- if (error.PERMISSION_DENIED) {
- jAlert('用戶拒絕了浏覽器請求媒體的權限', '提示');
- } else if (error.NOT_SUPPORTED_ERROR) {
- jAlert('對不起,您的浏覽器不支持拍照功能,請使用其他浏覽器', '提示');
- } else if (error.MANDATORY_UNSATISFIED_ERROR) {
- jAlert('指定的媒體類型未接收到媒體流', '提示');
- } else {
- jAlert('系統未能獲取到攝像頭,請確保攝像頭已正確安裝。或嘗試刷新頁面,重試', '提示');
- }
- };
-
- // Put video listeners into place
- if(navigator.getUserMedia) { // Standard
-
- navigator.getUserMedia(videoObj, function(stream) {
-
- video.src = stream;
- video.play();
-
- }, errBack);
-
- } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
-
- try{
-
- navigator.webkitGetUserMedia(videoObj, function(stream){
- video.src = window.webkitURL.createObjectURL(stream);
- video.play();
- }, errBack);
-
- }catch(error){
- alert(error);
- }
-
- }
- else if(navigator.mozGetUserMedia) { // Firefox-prefixed
- navigator.mozGetUserMedia(videoObj, function(stream){
-
- video.src = window.URL.createObjectURL(stream);
- video.play();
- }, errBack);
- }
-
-
-
- // Trigger photo take
- document.getElementById("snap").addEventListener("click", function() {
- context.drawImage(video, 0, 0, 600, 400);
- });
- document.getElementById("start").addEventListener("click", function() {
- startTrack();
- });
-
-
- document.getElementById("showposition").addEventListener("click", function() {
- showpos=true;
- });
-
- document.getElementById("hideposition").addEventListener("click", function() {
- showpos=false;
- });
-
- document.getElementById("mouse").addEventListener("click", function() {
- alive_mouse();
- });
- document.getElementById("head").addEventListener("click", function() {
- alive_head();
- });
-
- document.getElementById("eye").addEventListener("click", function() {
- alive_eye();
- });
-
-
-
-
- //}, false);
-
-
- </script>
-
- <script>
-
- //////////////////////////////////////////////////////////////////////////////
- //活體
- var last_time=0;//時間因素
- var last_nose_left=0;
- var last_nose_top=0;
-
- //張嘴動作
- var is_mouse_ok=false;
- var is_alive_mouse=false;
- var last_dis_eye_norse=0;
- var last_dis_mouse=0;
- function alive_mouse(){
-
- var media = document.getElementById("media");
- media.src="mp3/alive_mouse.mp3";
- media.play();
-
- document.getElementById("tip").innerHTML="請張合嘴巴";
- document.getElementById('result').innerHTML = "";
-
- is_mouse_ok=false;
- last_dis_mouse=0;
- last_time=0;
- last_dis_eye_norse=100000000;
-
- is_alive_head=false;
- is_alive_mouse=true;
- is_alive_eye=false;
-
- }
- //搖頭動作
- var is_head_ok=false;
- var is_alive_head=false;
- var last_dis_left_right=100000000;
- function alive_head(){
-
- var media = document.getElementById("media");
- media.src="mp3/alive_head.mp3";
- media.play();
-
- document.getElementById("tip").innerHTML="請在水平方向左右搖頭";
- document.getElementById('result').innerHTML = "";
-
- is_head_ok=false;
- last_dis_left_right=100000000;
- last_time=0;
- is_alive_head=true;
- is_alive_mouse=false;
- is_alive_eye=false;
-
- }
-
- //眨眼動作
- var is_alive_eye=false;
- var is_eye_ok = false;
-
- function alive_eye(){
- var media = document.getElementById("media");
- media.src="mp3/alive_eye.mp3";
- media.play();
-
- document.getElementById("tip").innerHTML="請眨眼";
- document.getElementById('result').innerHTML = "";
-
- is_eye_ok=false;
- last_dis_eye_norse=100000000;
-
- last_nose_left=0;
- last_nose_top=0;
-
- last_time=0;
-
- is_alive_head=false;
- is_alive_mouse=false;
- is_alive_eye=true;
- }
-
-
- function startTrack(){
-
- var videoInput = document.getElementById('video');
-
- var ctracker = new clm.tracker();
- ctracker.init(pModel);
- ctracker.start(videoInput);
-
-
- var canvasInput = document.getElementById('canvas');
- var cc = canvasInput.getContext('2d');
- cc.lineWidth=3;
-
- function drawLoop() {
- //requestAnimationFrame(drawLoop);
-
-
- cc.clearRect(0, 0, canvasInput.width, canvasInput.height);
- //ctracker.draw(canvasInput );
- var positions = ctracker.getCurrentPosition();
- if (showpos && positions) {
-
- for (var p = 0;p < positions.length;p++) {
- positionString += "featurepoint "+p+" : ["+positions[p][0].toFixed(2)+","+positions[p][1].toFixed(2) +"]<br/>";
- }
- document.getElementById('positions').innerHTML = positionString;
-
-
- }
- if(positions){
-
- for (var p =0;p < 71;p++) {
- cc.beginPath();
- cc.arc(positions[p][0].toFixed(2), positions[p][1].toFixed(2),2, 0, Math.PI * 2, true);
- cc.closePath();
- cc.fillStyle = '#00FF00';
- cc.fill();
- }
-
-
- //cc.strokeStyle = 'red';
-
- //0-14 輪廓
- //7 下吧,最下
-
- //2 最左邊
- //12 最右邊
-
-
- //15-22 眉毛
-
-
- //23-27 左眼睛五個點
- //27 左眼中間
- //63-66 左眼四個點
-
- //28-32 右眼睛五個點
- //67-70 右眼四個點
-
-
- //33-43 鼻子
- //62 鼻中間
-
-
- //44-61 嘴巴
- //47 嘴巴上
- //53 嘴巴下
-
- ///////////////////////////////////////////////////////////////////////////////////////////////
-
- //左眼中間
- for (var p =27;p <=27;p++) {
- cc.beginPath();
- cc.arc(positions[p][0].toFixed(2), positions[p][1].toFixed(2), 2, 0, Math.PI * 2, true);
- cc.closePath();
- cc.fillStyle = 'red';
- cc.fill();
- }
-
- //鼻子中間
- for (var p =62;p <=62;p++) {
- cc.beginPath();
- cc.arc(positions[p][0].toFixed(2), positions[p][1].toFixed(2), 2, 0, Math.PI * 2, true);
- cc.closePath();
- cc.fillStyle = 'red';
- cc.fill();
- }
- //嘴巴上
- for (var p =57;p <=57;p++) {
- cc.beginPath();
- cc.arc(positions[p][0].toFixed(2), positions[p][1].toFixed(2), 2, 0, Math.PI * 2, true);
- cc.closePath();
- cc.fillStyle = 'red';
- cc.fill();
- }
- //嘴巴下
- for (var p =60;p <=60;p++) {
- cc.beginPath();
- cc.arc(positions[p][0].toFixed(2), positions[p][1].toFixed(2), 2, 0, Math.PI * 2, true);
- cc.closePath();
- cc.fillStyle = 'red';
- cc.fill();
- }
- //////////////////////////////////////
- //head
- if(is_alive_head==true){
- if(last_time==0 || (new Date().getTime()-last_time>500 && new Date().getTime()-last_time<10000 ) ){
- var xdiff_left = positions[62][0] - positions[2][0] ;
- var ydiff_left = positions[62][1] - positions[2][1] ;
- var dis_left = Math.pow((xdiff_left * xdiff_left + ydiff_left * ydiff_left), 0.5);
-
- var xdiff_right = positions[12][0] - positions[62][0] ;
- var ydiff_right = positions[12][1] - positions[62][1] ;
- var dis_right = Math.pow((xdiff_right * xdiff_right + ydiff_right * ydiff_right), 0.5);
-
- var xdiff_side = positions[12][0] - positions[2][0] ;
- var ydiff_side = positions[12][1] - positions[2][1] ;
- var dis_side = Math.pow((xdiff_side * xdiff_side + ydiff_side * ydiff_side), 0.5);
-
-
- var dis_left_right = dis_left - dis_right;
- document.getElementById('result').innerHTML = dis_left_right;
-
-
- if(last_dis_left_right>0 && dis_left_right > dis_side/3){
-
- document.getElementById('result').innerHTML = "通過";
-
- is_head_ok=true;
- is_alive_head=false;
-
- }
-
-
-
- last_dis_left_right=dis_left_right;
- last_time = new Date().getTime();
-
- }
- }
-
- /////////////////////////////////////
- //mouse
- if(is_alive_mouse==true){
- if(last_time==0 || (new Date().getTime()-last_time>500 && new Date().getTime()-last_time<10000 ) ){
-
- //研究和鼻子距離
- var xdiff = positions[62][0] - positions[27][0] ;
- var ydiff = positions[62][1] - positions[27][1] ;
- var dis_eye_norse = Math.pow((xdiff * xdiff + ydiff * ydiff), 0.5);
-
- //上嘴唇 和下嘴唇距離
- var xdiff_mouse = positions[53][0] - positions[47][0] ;
- var ydiff_mouse = positions[53][1] - positions[47][1] ;
- var dis_mouse = Math.pow((xdiff_mouse * xdiff_mouse + ydiff_mouse * ydiff_mouse), 0.5);
-
- //上次的眼鼻距離和這次的眼鼻距離差
- var dn= Math.abs(dis_eye_norse-last_dis_eye_norse);
-
- //上次的嘴距離和本次的嘴距離差
- var dm=Math.abs(dis_mouse - last_dis_mouse);
-
-
-
-
- //鼻子的位置確保變化不大
- if(last_nose_left>0 && last_nose_top>0
- && Math.abs(positions[62][0]-last_nose_left)<5
- && Math.abs(positions[62][1]-last_nose_top)<5
- ){
-
- document.getElementById('msg').innerHTML = dn;
-
- if(last_dis_eye_norse>0 && dn < dis_eye_norse*1/50){
-
- if(last_dis_mouse>0 && dm > dis_mouse/10){
-
- document.getElementById('result').innerHTML = "通過";
-
- is_alive_mouse=false;
- is_mouse_ok=true;
- }
-
- }
- }
-
-
- last_dis_mouse = dis_mouse;
- last_dis_eye_norse = dis_eye_norse;
- last_time = new Date().getTime();
-
- last_nose_left = positions[62][0];
- last_nose_top = positions[62][1];
-
- }
- }
-
- /////////////////////////////////////
- //eye
- if(is_alive_eye==true){
- if(last_time==0 || (new Date().getTime()-last_time>10 ) ){
-
-
- var xdiff1 = positions[62][0] - positions[27][0] ;
- var ydiff1 = positions[62][1] - positions[27][1] ;
- var dis_eye_norse1 = Math.pow((xdiff1 * xdiff1 + ydiff1 * ydiff1), 0.5);
-
- var xdiff2 = positions[62][0] - positions[32][0] ;
- var ydiff2 = positions[62][1] - positions[32][1] ;
- var dis_eye_norse2 = Math.pow((xdiff2 * xdiff2 + ydiff2 * ydiff2), 0.5);
-
- var dis_eye_norse = (dis_eye_norse1 + dis_eye_norse2);
-
-
-
- if(last_nose_left>0 && last_nose_top>0
- && Math.abs(positions[62][0]-last_nose_left)<0.5
- && Math.abs(positions[62][1]-last_nose_top)<0.5
- ){
- document.getElementById('msg').innerHTML = Math.abs(dis_eye_norse - last_dis_eye_norse) - dis_eye_norse*1/20;
-
- if(last_dis_eye_norse>0 && (Math.abs(dis_eye_norse - last_dis_eye_norse) > dis_eye_norse*1/20 ) ){
-
- document.getElementById('result').innerHTML = "通過";
-
- is_alive_eye=false;
- is_eye_ok=true;
-
- }
- }
-
-
- last_nose_left = positions[62][0];
- last_nose_top = positions[62][1];
-
- last_dis_eye_norse = dis_eye_norse;
- last_time = new Date().getTime();
-
- }
-
-
- }
-
-
- }
-
- requestAnimationFrame(drawLoop);
-
- }
-
- drawLoop();
-
- }
-
-
-
- </script>
- </html>
以上就是小編為大家帶來的基於HTML5 的人臉識別活體認證的實現方法全部內容了,希望大家多多支持~
原文地址:http://www.cnblogs.com/lilies/archive/2016/06/21/5604212.html