html5

关注公众号 jb51net

关闭
网页制作 > html5 >

基于HTML5 的人脸识别认证的实现方法

脚本之家

本文介绍了在HTML5 环境下可以采用clmtrackr.js 检测工具,结合人脸模型,实现人脸的检测。同时采用动作识别实现认证。

但本方案只能够在Firefox 或者Chrome中使用。并且只适合研究学习,实际场景中不太理想,需要进一步优化才能够应用。

如果有人有相关的技术,可以推荐介绍给我。

JavaScript Code复制内容到剪贴板
  1. <!DOCTYPE html>  
  2. <!--  
  3. Ideally these elements aren't created until it's confirmed that the   
  4. client supports video/camera, but for the sake of illustrating the   
  5. elements involved, they are created with markup (not JavaScript)  
  6. -->  
  7. <html>  
  8. <meta charset="GBK">  
  9. <style>  
  10. #container {  
  11. position : relative;  
  12. }  
  13. #canvas {  
  14. position : absolute;  
  15. left : 0;  
  16. top : 0;  
  17. }  
  18. </style>  
  19. <script src="utils.js"></script>  
  20. <script src="clmtrackr.js"></script>  
  21. <script src="./models/model_pca_20_svm.js"></script>  
  22. <script src="numeric.js"></script>  
  23. <script src="ccv.js"></script>  
  24.   
  25. <audio id="media">   
  26. 你的浏览器不支持audio标签。  
  27. </audio>  
  28. <div id="container">  
  29. <video id="video" width="600" height="400" autoplay >   
  30. 您的浏览器不支持video标签  
  31. </video>  
  32. <canvas id="canvas" width="600" height="400"></canvas>  
  33. </div>      
  34.   
  35. <button id="snap">Snap Photo</button>  
  36.   
  37. <button id="start">Start</button>  
  38.   
  39. <button id="showposition">显示</button>  
  40.   
  41. <button id="hideposition">不显示</button>  
  42.   
  43. <br/>  
  44.   
  45. <button id="mouse">张嘴验证</button>   
  46. <button id="head">摇头验证</button>   
  47. <button id="eye">眨眼验证</button>  
  48.   
  49.   
  50. <div id="tip">  
  51. </div>  
  52. <div id="result">  
  53. </div>  
  54. <div id="msg">  
  55. </div>  
  56.   
  57. <div id="positions">  
  58. </div>  
  59.   
  60. <script>  
  61.   
  62. var showpos=false;  
  63. // Put event listeners into place  
  64. //window.addEventListener("DOMContentLoaded", function() {  
  65.   
  66. // Grab elements, create settings, etc.  
  67. var canvas = document.getElementById("canvas"),  
  68. context = canvas.getContext("2d"),  
  69. video = document.getElementById("video"),  
  70. videoObj = { "video"true },  
  71. errBack = function(error) {  
  72. if (error.PERMISSION_DENIED) {  
  73. jAlert('用户拒绝了浏览器请求媒体的权限''提示');  
  74. else if (error.NOT_SUPPORTED_ERROR) {  
  75. jAlert('对不起,您的浏览器不支持拍照功能,请使用其他浏览器''提示');  
  76. else if (error.MANDATORY_UNSATISFIED_ERROR) {  
  77. jAlert('指定的媒体类型未接收到媒体流''提示');  
  78. else {  
  79. jAlert('系统未能获取到摄像头,请确保摄像头已正确安装。或尝试刷新页面,重试''提示');  
  80. }  
  81. };  
  82.   
  83. // Put video listeners into place  
  84. if(navigator.getUserMedia) { // Standard  
  85.   
  86. navigator.getUserMedia(videoObj, function(stream) {  
  87.   
  88. video.src = stream;  
  89. video.play();  
  90.   
  91. }, errBack);  
  92.   
  93. else if(navigator.webkitGetUserMedia) { // WebKit-prefixed  
  94.   
  95. try{  
  96.   
  97. navigator.webkitGetUserMedia(videoObj, function(stream){   
  98. video.src = window.webkitURL.createObjectURL(stream);  
  99. video.play();  
  100. }, errBack);  
  101.   
  102. }catch(error){  
  103. alert(error);  
  104. }  
  105.   
  106. }  
  107. else if(navigator.mozGetUserMedia) { // Firefox-prefixed  
  108. navigator.mozGetUserMedia(videoObj, function(stream){  
  109.   
  110. video.src = window.URL.createObjectURL(stream);  
  111. video.play();  
  112. }, errBack);  
  113. }  
  114.   
  115.   
  116.   
  117. // Trigger photo take  
  118. document.getElementById("snap").addEventListener("click"function() {  
  119. context.drawImage(video, 0, 0, 600, 400);  
  120. });  
  121. document.getElementById("start").addEventListener("click"function() {  
  122. startTrack();  
  123. });  
  124.   
  125.   
  126. document.getElementById("showposition").addEventListener("click"function() {  
  127. showpos=true;  
  128. });  
  129.   
  130. document.getElementById("hideposition").addEventListener("click"function() {  
  131. showpos=false;  
  132. });  
  133.   
  134. document.getElementById("mouse").addEventListener("click"function() {  
  135. alive_mouse();  
  136. });  
  137. document.getElementById("head").addEventListener("click"function() {  
  138. alive_head();  
  139. });  
  140.   
  141. document.getElementById("eye").addEventListener("click"function() {  
  142. alive_eye();  
  143. });  
  144.   
  145.   
  146.   
  147.   
  148. //}, false);  
  149.   
  150.   
  151. </script>  
  152.   
  153. <script>  
  154.   
  155. //////////////////////////////////////////////////////////////////////////////  
  156. var last_time=0;//时间因素  
  157. var last_nose_left=0;  
  158. var last_nose_top=0;  
  159.   
  160. //张嘴动作  
  161. var is_mouse_ok=false;   
  162. var is_alive_mouse=false;  
  163. var last_dis_eye_norse=0;  
  164. var last_dis_mouse=0;  
  165. function alive_mouse(){  
  166.   
  167. var media = document.getElementById("media");  
  168. media.src="mp3/alive_mouse.mp3";  
  169. media.play();  
  170.   
  171. document.getElementById("tip").innerHTML="请张合嘴巴";  
  172. document.getElementById('result').innerHTML = "";  
  173.   
  174. is_mouse_ok=false;  
  175. last_dis_mouse=0;  
  176. last_time=0;  
  177. last_dis_eye_norse=100000000;   
  178.   
  179. is_alive_head=false;  
  180. is_alive_mouse=true;  
  181. is_alive_eye=false;  
  182.   
  183. }  
  184. //摇头动作  
  185. var is_head_ok=false;   
  186. var is_alive_head=false;  
  187. var last_dis_left_right=100000000;   
  188. function alive_head(){  
  189.   
  190. var media = document.getElementById("media");  
  191. media.src="mp3/alive_head.mp3";  
  192. media.play();  
  193.   
  194. document.getElementById("tip").innerHTML="请在水平方向左右摇头";  
  195. document.getElementById('result').innerHTML = "";  
  196.   
  197. is_head_ok=false;  
  198. last_dis_left_right=100000000;   
  199. last_time=0;   
  200. is_alive_head=true;  
  201. is_alive_mouse=false;  
  202. is_alive_eye=false;  
  203.   
  204. }  
  205.   
  206. //眨眼动作  
  207. var is_alive_eye=false;  
  208. var is_eye_ok = false;  
  209.   
  210. function alive_eye(){  
  211. var media = document.getElementById("media");  
  212. media.src="mp3/alive_eye.mp3";  
  213. media.play();  
  214.   
  215. document.getElementById("tip").innerHTML="请眨眼";  
  216. document.getElementById('result').innerHTML = "";  
  217.   
  218. is_eye_ok=false;  
  219. last_dis_eye_norse=100000000;   
  220.   
  221. last_nose_left=0;  
  222. last_nose_top=0;  
  223.   
  224. last_time=0;   
  225.   
  226. is_alive_head=false;  
  227. is_alive_mouse=false;  
  228. is_alive_eye=true;  
  229. }  
  230.   
  231.   
  232. function startTrack(){  
  233.   
  234. var videoInput = document.getElementById('video');  
  235.   
  236. var ctracker = new clm.tracker();  
  237. ctracker.init(pModel);  
  238. ctracker.start(videoInput);  
  239.   
  240.   
  241. var canvasInput = document.getElementById('canvas');  
  242. var cc = canvasInput.getContext('2d');  
  243. cc.lineWidth=3;  
  244.   
  245. function drawLoop() {  
  246. //requestAnimationFrame(drawLoop);  
  247.   
  248.   
  249. cc.clearRect(0, 0, canvasInput.width, canvasInput.height);  
  250. //ctracker.draw(canvasInput );  
  251. var positions = ctracker.getCurrentPosition();  
  252. if (showpos && positions) {  
  253.   
  254. for (var p = 0;p < positions.length;p++) {  
  255. positionString += "featurepoint "+p+" : ["+positions[p][0].toFixed(2)+","+positions[p][1].toFixed(2) +"]<br/>";  
  256. }  
  257. document.getElementById('positions').innerHTML = positionString;  
  258.   
  259.   
  260. }  
  261. if(positions){  
  262.   
  263. for (var p =0;p < 71;p++) {      
  264. cc.beginPath();  
  265. cc.arc(positions[p][0].toFixed(2), positions[p][1].toFixed(2),2, 0, Math.PI * 2, true);  
  266. cc.closePath();  
  267. cc.fillStyle = '#00FF00';  
  268. cc.fill();  
  269. }  
  270.   
  271.   
  272. //cc.strokeStyle = 'red';  
  273.   
  274. //0-14 轮廓  
  275. //7 下吧,最下  
  276.   
  277. //2 最左边  
  278. //12 最右边  
  279.   
  280.   
  281. //15-22 眉毛  
  282.   
  283.   
  284. //23-27 左眼睛五个点  
  285. //27 左眼中间  
  286. //63-66 左眼四个点  
  287.   
  288. //28-32 右眼睛五个点  
  289. //67-70 右眼四个点  
  290.   
  291.   
  292. //33-43 鼻子  
  293. //62 鼻中间  
  294.   
  295.   
  296. //44-61 嘴巴  
  297. //47 嘴巴上  
  298. //53 嘴巴下  
  299.   
  300. ///////////////////////////////////////////////////////////////////////////////////////////////  
  301.   
  302. //左眼中间  
  303. for (var p =27;p <=27;p++) {      
  304. cc.beginPath();  
  305. cc.arc(positions[p][0].toFixed(2), positions[p][1].toFixed(2), 2, 0, Math.PI * 2, true);  
  306. cc.closePath();  
  307. cc.fillStyle = 'red';  
  308. cc.fill();  
  309. }  
  310.   
  311. //鼻子中间  
  312. for (var p =62;p <=62;p++) {      
  313. cc.beginPath();  
  314. cc.arc(positions[p][0].toFixed(2), positions[p][1].toFixed(2), 2, 0, Math.PI * 2, true);  
  315. cc.closePath();  
  316. cc.fillStyle = 'red';  
  317. cc.fill();  
  318. }  
  319. //嘴巴上  
  320. for (var p =57;p <=57;p++) {      
  321. cc.beginPath();  
  322. cc.arc(positions[p][0].toFixed(2), positions[p][1].toFixed(2), 2, 0, Math.PI * 2, true);  
  323. cc.closePath();  
  324. cc.fillStyle = 'red';  
  325. cc.fill();  
  326. }  
  327. //嘴巴下  
  328. for (var p =60;p <=60;p++) {      
  329. cc.beginPath();  
  330. cc.arc(positions[p][0].toFixed(2), positions[p][1].toFixed(2), 2, 0, Math.PI * 2, true);  
  331. cc.closePath();  
  332. cc.fillStyle = 'red';  
  333. cc.fill();  
  334. }  
  335. //////////////////////////////////////  
  336. //head  
  337. if(is_alive_head==true){  
  338. if(last_time==0 || (new Date().getTime()-last_time>500 && new Date().getTime()-last_time<10000 ) ){  
  339. var xdiff_left = positions[62][0] - positions[2][0] ;  
  340. var ydiff_left = positions[62][1] - positions[2][1] ;  
  341. var dis_left = Math.pow((xdiff_left * xdiff_left + ydiff_left * ydiff_left), 0.5);  
  342.   
  343. var xdiff_right = positions[12][0] - positions[62][0] ;  
  344. var ydiff_right = positions[12][1] - positions[62][1] ;  
  345. var dis_right = Math.pow((xdiff_right * xdiff_right + ydiff_right * ydiff_right), 0.5);  
  346.   
  347. var xdiff_side = positions[12][0] - positions[2][0] ;  
  348. var ydiff_side = positions[12][1] - positions[2][1] ;  
  349. var dis_side = Math.pow((xdiff_side * xdiff_side + ydiff_side * ydiff_side), 0.5);  
  350.   
  351.   
  352. var dis_left_right = dis_left - dis_right;  
  353. document.getElementById('result').innerHTML = dis_left_right;  
  354.   
  355.   
  356. if(last_dis_left_right>0 && dis_left_right > dis_side/3){  
  357.   
  358. document.getElementById('result').innerHTML = "通过";  
  359.   
  360. is_head_ok=true;  
  361. is_alive_head=false;  
  362.   
  363. }  
  364.   
  365.   
  366.   
  367. last_dis_left_right=dis_left_right;   
  368. last_time = new Date().getTime();  
  369.   
  370. }  
  371. }  
  372.   
  373. /////////////////////////////////////  
  374. //mouse   
  375. if(is_alive_mouse==true){  
  376. if(last_time==0 || (new Date().getTime()-last_time>500 && new Date().getTime()-last_time<10000 ) ){  
  377.   
  378. //研究和鼻子距离  
  379. var xdiff = positions[62][0] - positions[27][0] ;  
  380. var ydiff = positions[62][1] - positions[27][1] ;   
  381. var dis_eye_norse = Math.pow((xdiff * xdiff + ydiff * ydiff), 0.5);  
  382.   
  383. //上嘴唇 和下嘴唇距离  
  384. var xdiff_mouse = positions[53][0] - positions[47][0] ;  
  385. var ydiff_mouse = positions[53][1] - positions[47][1] ;   
  386. var dis_mouse = Math.pow((xdiff_mouse * xdiff_mouse + ydiff_mouse * ydiff_mouse), 0.5);  
  387.   
  388. //上次的眼鼻距离和这次的眼鼻距离差  
  389. var dn= Math.abs(dis_eye_norse-last_dis_eye_norse);  
  390.   
  391. //上次的嘴距离和本次的嘴距离差  
  392. var dm=Math.abs(dis_mouse - last_dis_mouse);  
  393.   
  394.   
  395.   
  396.   
  397. //鼻子的位置确保变化不大  
  398. if(last_nose_left>0 && last_nose_top>0  
  399. && Math.abs(positions[62][0]-last_nose_left)<5  
  400. && Math.abs(positions[62][1]-last_nose_top)<5  
  401. ){  
  402.   
  403. document.getElementById('msg').innerHTML = dn;  
  404.   
  405. if(last_dis_eye_norse>0 && dn < dis_eye_norse*1/50){   
  406.   
  407. if(last_dis_mouse>0 && dm > dis_mouse/10){  
  408.   
  409. document.getElementById('result').innerHTML = "通过";  
  410.   
  411. is_alive_mouse=false;  
  412. is_mouse_ok=true;  
  413. }  
  414.   
  415. }  
  416. }  
  417.   
  418.   
  419. last_dis_mouse = dis_mouse;  
  420. last_dis_eye_norse = dis_eye_norse;  
  421. last_time = new Date().getTime();   
  422.   
  423. last_nose_left = positions[62][0];  
  424. last_nose_top = positions[62][1];  
  425.   
  426. }  
  427. }  
  428.   
  429. /////////////////////////////////////  
  430. //eye   
  431. if(is_alive_eye==true){  
  432. if(last_time==0 || (new Date().getTime()-last_time>10 ) ){  
  433.   
  434.   
  435. var xdiff1 = positions[62][0] - positions[27][0] ;  
  436. var ydiff1 = positions[62][1] - positions[27][1] ;   
  437. var dis_eye_norse1 = Math.pow((xdiff1 * xdiff1 + ydiff1 * ydiff1), 0.5);  
  438.   
  439. var xdiff2 = positions[62][0] - positions[32][0] ;  
  440. var ydiff2 = positions[62][1] - positions[32][1] ;   
  441. var dis_eye_norse2 = Math.pow((xdiff2 * xdiff2 + ydiff2 * ydiff2), 0.5);  
  442.   
  443. var dis_eye_norse = (dis_eye_norse1 + dis_eye_norse2);  
  444. if(last_nose_left>0 && last_nose_top>0  
  445. && Math.abs(positions[62][0]-last_nose_left)<0.5  
  446. && Math.abs(positions[62][1]-last_nose_top)<0.5  
  447. ){  
  448. document.getElementById('msg').innerHTML = Math.abs(dis_eye_norse - last_dis_eye_norse) - dis_eye_norse*1/20;  
  449.   
  450. if(last_dis_eye_norse>0 && (Math.abs(dis_eye_norse - last_dis_eye_norse) > dis_eye_norse*1/20 ) ){  
  451.   
  452. document.getElementById('result').innerHTML = "通过";  
  453.   
  454. is_alive_eye=false;  
  455. is_eye_ok=true;  
  456.   
  457. }  
  458. }  
  459.   
  460.   
  461. last_nose_left = positions[62][0];  
  462. last_nose_top = positions[62][1];  
  463.   
  464. last_dis_eye_norse = dis_eye_norse;  
  465. last_time = new Date().getTime();   
  466.   
  467. }  
  468.   
  469.   
  470. }  
  471.   
  472.   
  473. }  
  474.   
  475. requestAnimationFrame(drawLoop);  
  476.   
  477. }  
  478.   
  479. drawLoop();  
  480.   
  481. }  
  482.   
  483.    
  484.   
  485. </script>  
  486. </html>