search
HomeWeb Front-endH5 TutorialImplementation method of face recognition live authentication based on HTML5

In recent years, face recognition technology has been used in many applications in the field of identity authentication, such as withdrawals from Alipay and China Merchants Bank, pension collection, etc. However, in terms of preventing counterfeiting and authentication security, currently It is still a subject that needs to be further solved, especially in terms of live authentication technology on the mobile side.
This article introduces the clmtrackr.js detection tool that can be used in the HTML5 environment, combined with the face model, to achieve face tracking and detection. At the same time, motion recognition is used to achieve live authentication.
But this solution can only be used in Firefox or Chrome. And it is only suitable for research and study. It is not ideal in actual scenarios and needs further optimization before it can be applied.
If anyone has relevant technology, you can recommend it to me.

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


The above is the content of the implementation method of face recognition live authentication based on HTML5. For more related content, please pay attention to the PHP Chinese website (www.php.cn )!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Does H5 Refer To? Exploring the ContextWhat Does H5 Refer To? Exploring the ContextApr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

H5: Tools, Frameworks, and Best PracticesH5: Tools, Frameworks, and Best PracticesApr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

The Legacy of HTML5: Understanding H5 in the PresentThe Legacy of HTML5: Understanding H5 in the PresentApr 10, 2025 am 09:28 AM

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 Code: Accessibility and Semantic HTMLH5 Code: Accessibility and Semantic HTMLApr 09, 2025 am 12:05 AM

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.

Is h5 same as HTML5?Is h5 same as HTML5?Apr 08, 2025 am 12:16 AM

"h5" and "HTML5" are the same in most cases, but they may have different meanings in certain specific scenarios. 1. "HTML5" is a W3C-defined standard that contains new tags and APIs. 2. "h5" is usually the abbreviation of HTML5, but in mobile development, it may refer to a framework based on HTML5. Understanding these differences helps to use these terms accurately in your project.

What is the function of H5?What is the function of H5?Apr 07, 2025 am 12:10 AM

H5, or HTML5, is the fifth version of HTML. It provides developers with a stronger tool set, making it easier to create complex web applications. The core functions of H5 include: 1) elements that allow drawing graphics and animations on web pages; 2) semantic tags such as, etc. to make the web page structure clear and conducive to SEO optimization; 3) new APIs such as GeolocationAPI support location-based services; 4) Cross-browser compatibility needs to be ensured through compatibility testing and Polyfill library.

How to do h5 linkHow to do h5 linkApr 06, 2025 pm 12:39 PM

How to create an H5 link? Determine the link target: Get the URL of the H5 page or application. Create HTML anchors: Use the <a> tag to create an anchor and specify the link target URL. Set link properties (optional): Set target, title, and onclick properties as needed. Add to webpage: Add HTML anchor code to the webpage where you want the link to appear.

How to solve the h5 compatibility problemHow to solve the h5 compatibility problemApr 06, 2025 pm 12:36 PM

Solutions to H5 compatibility issues include: using responsive design that allows web pages to adjust layouts according to screen size. Use cross-browser testing tools to test compatibility before release. Use Polyfill to provide support for new APIs for older browsers. Follow web standards and use effective code and best practices. Use CSS preprocessors to simplify CSS code and improve readability. Optimize images, reduce web page size and speed up loading. Enable HTTPS to ensure the security of the website.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function