搜尋
首頁web前端H5教程基於HTML5 的人臉辨識活體認證的實作方法

基於HTML5 的人臉辨識活體認證的實作方法

May 17, 2018 pm 03:28 PM
html5人臉辨識

近年來,人臉辨識技術在身分認證領域的應用已經有了較多應用,例如:支付寶、招行的提款、退休金領取等方面,但在杜絕假冒、認證安全性等方面,目前還是一個比較需要進一步解決的課題,特別是在行動端的活體認證技術方面。
本文介紹了在HTML5 環境下可以採用clmtrackr.js 偵測工具,結合人臉模型,實現人臉的追蹤偵測。同時採用動作辨識實現活體認證。
但本方案只能夠在Firefox 或Chrome中使用。且只適合研究學習,實際場景中較不理想,需要進一步優化才能夠應用。
如果有人有相關的技術,可以推薦介紹給我。

<!DOCTYPE html>  

    <!--  

    Ideally these elements aren&#39;t created until it&#39;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>  

    <p id="container">  

    <video id="video" width="600" height="400" autoplay >   

    您的浏览器不支持video标签  

    </video>  

    <canvas id="canvas" width="600" height="400"></canvas>  

    </p>      

      

    <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>  

      

      

    <p id="tip">  

    </p>  

    <p id="result">  

    </p>  

    <p id="msg">  

    </p>  

      

    <p id="positions">  

    </p>  

      

    <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(&#39;用户拒绝了浏览器请求媒体的权限&#39;, &#39;提示&#39;);  

    } else if (error.NOT_SUPPORTED_ERROR) {  

    jAlert(&#39;对不起,您的浏览器不支持拍照功能,请使用其他浏览器&#39;, &#39;提示&#39;);  

    } else if (error.MANDATORY_UNSATISFIED_ERROR) {  

    jAlert(&#39;指定的媒体类型未接收到媒体流&#39;, &#39;提示&#39;);  

    } else {  

    jAlert(&#39;系统未能获取到摄像头,请确保摄像头已正确安装。或尝试刷新页面,重试&#39;, &#39;提示&#39;);  

    }  

    };  

      

    // 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(&#39;result&#39;).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(&#39;result&#39;).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(&#39;result&#39;).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(&#39;video&#39;);  

      

    var ctracker = new clm.tracker();  

    ctracker.init(pModel);  

    ctracker.start(videoInput);  

      

      

    var canvasInput = document.getElementById(&#39;canvas&#39;);  

    var cc = canvasInput.getContext(&#39;2d&#39;);  

    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(&#39;positions&#39;).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 = &#39;#00FF00&#39;;  

    cc.fill();  

    }  

      

      

    //cc.strokeStyle = &#39;red&#39;;  

      

    //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 = &#39;red&#39;;  

    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 = &#39;red&#39;;  

    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 = &#39;red&#39;;  

    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 = &#39;red&#39;;  

    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(&#39;result&#39;).innerHTML = dis_left_right;  

      

      

    if(last_dis_left_right>0 && dis_left_right > dis_side/3){  

      

    document.getElementById(&#39;result&#39;).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(&#39;msg&#39;).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(&#39;result&#39;).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(&#39;msg&#39;).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(&#39;result&#39;).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 的人臉辨識活體認證的實作方法 的內容,更多相關內容請關注PHP中文網(www.php.cn )!


#
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
掌握microdata:HTML5的分步指南掌握microdata:HTML5的分步指南May 14, 2025 am 12:07 AM

Microdatainhtml5enhancesseoanduserexperienceByByBybyBystructuredDatatoSearchEngines.1)useIteMscope,itemType,anditempropattributestomarkupcontentlikeSoreRoductSssSssSoRorevents.2)

HTML5表格中有什麼新功能?探索新輸入類型HTML5表格中有什麼新功能?探索新輸入類型May 13, 2025 pm 03:45 PM

html5introducesnewinputtypesthatenhanceSerexperience,簡化開發和iMproveAccessibility.1)自動validatesemailformat.2)優化優化,優化OmportizeSmizesemizesemizesemizesemizesemizeSmobobileWithAnumericKeyPad.3)和Simimplifydateandtimeputientupits,並重新替代了Forcustemolcustemolcustene。

理解H5:含義和意義理解H5:含義和意義May 11, 2025 am 12:19 AM

H5是HTML5,是HTML的第五個版本。 HTML5提升了網頁的表現力和交互性,引入了語義化標籤、多媒體支持、離線存儲和Canvas繪圖等新特性,推動了Web技術的發展。

H5:可訪問性和網絡標準合規性H5:可訪問性和網絡標準合規性May 10, 2025 am 12:21 AM

無障礙訪問和網絡標準遵循對網站至關重要。 1)無障礙訪問確保所有用戶都能平等訪問網站,2)網絡標準遵循提高網站的可訪問性和一致性,3)實現無障礙訪問需使用語義化HTML、鍵盤導航、顏色對比度和替代文本,4)遵循這些原則不僅是道德和法律要求,還能擴大用戶群體。

HTML中的H5標籤是什麼?HTML中的H5標籤是什麼?May 09, 2025 am 12:11 AM

HTML中的H5標籤是第五級標題,用於標記較小的標題或子標題。 1)H5標籤幫助細化內容層次,提升可讀性和SEO。 2)結合CSS可定製樣式,增強視覺效果。 3)合理使用H5標籤,避免濫用,確保內容結構邏輯性。

H5代碼:Web結構的初學者指南H5代碼:Web結構的初學者指南May 08, 2025 am 12:15 AM

HTML5構建網站的方法包括:1.使用語義化標籤定義網頁結構,如、、等;2.嵌入多媒體內容,使用和標籤;3.應用表單驗證和本地存儲等高級功能。通過這些步驟,你可以創建一個結構清晰、功能豐富的現代網頁。

H5代碼結構:組織內容以實現可讀性H5代碼結構:組織內容以實現可讀性May 07, 2025 am 12:06 AM

通過合理的H5代碼結構可以讓頁面在眾多內容中脫穎而出。 1)使用語義化標籤如、、等組織內容,使結構清晰。 2)通過CSS佈局如Flexbox或Grid控制頁面在不同設備上的呈現效果。 3)實現響應式設計,確保頁面在不同屏幕尺寸上自適應。

H5與較舊的HTML版本:比較H5與較舊的HTML版本:比較May 06, 2025 am 12:09 AM

HTML5(H5)與舊版本HTML的主要區別包括:1)H5引入了語義化標籤,2)支持多媒體內容,3)提供離線存儲功能。 H5通過新標籤和API增強了網頁的功能和表現力,如和標籤,提高了用戶體驗和SEO效果,但需注意兼容性問題。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具