作者:mobiledu2502860487 | 来源:互联网 | 2023-08-22 11:51
首先: 注册百度云,并搜索人脸实名认证 第二步查看自己要请求的api
创建步骤: 添加一个人脸识别的人脸库 添加一个人脸用户组 添加一个人脸用户 给这个用户添加一张人像图
然后 渲染出 拍照页 前端:
< html> < head> < meta charset = " utf-8" > < title> layui title> < meta name = " renderer" content = " webkit" > < meta http-equiv = " X-UA-Compatible" content = " IE=edge,chrome=1" > < meta name = " viewport" content = " token punctuation">"> < link rel = " stylesheet" href = " /static/lib/layui-v2.5.4/css/layui.css" media = " all" > < link rel = " stylesheet" href = " /static/css/public.css" media = " all" > < script src = " https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" > script> < style> * { margin : 0 auto; padding : 0; } button { width : 100px; height : 40px; background-color : black; color : white; border-radius : 10px; } #video { background-color : black; border-radius : 50%; } .layuimini-container { margin : 10%; } style> head> < body> < div class = " layuimini-container" > < div align = " center" > < video id = " video" width = " 300px" height = " 300px" autoplay > video> < canvas hidden = " hidden" id = " canvas" width = " 626" height = " 800" > canvas> div> < div align = " center" > < button id = " open" > 授权摄像头 button> < button id = " close" > 关闭摄像头 button> < button id = " CatchCode" > 人脸检测 button> div> div> < script src = " /static/lib/layui-v2.5.4/layui.js" charset = " utf-8" > script> < script src = " /static/js/wk.js" charset = " utf-8" > script> < script type = " text/Javascript" > var video; var context; var canvas; $ ( function ( ) { var flag = false ; $ ( "#open" ) . click ( function ( ) { if ( ! flag) { open ( ) ; flag = true ; } } ) ; $ ( "#close" ) . click ( function ( ) { if ( flag) { video. srcObject. getTracks ( ) [ 0 ] . stop ( ) ; flag = false ; } } ) ; $ ( "#CatchCode" ) . click ( function ( ) { if ( flag) { context. drawImage ( video, 0 , 0 , 330 , 250 ) ; CatchCode ( ) ; } else alert ( "请先开启摄像头!" ) ; } ) ; } ) ; function CatchCode ( ) { var img = getBase64 ( ) ; $. ajax ( { type: "POST" , async : false , url: "{:url(&#39;faceVerify&#39;)}" , data: { img: img, } , dataType: "JSON" , success: function ( data) { console. log ( data) ; return false ; if ( data. code == 0 ) { location. href = "{:url(&#39;paySuccess&#39;)}" } else { location. href = "{:url(&#39;payFail&#39;)}" return false ; } } } ) ; } ; function open ( ) { canvas = document. getElementById ( "canvas" ) ; context = canvas. getContext ( "2d" ) ; video = document. getElementById ( "video" ) ; var videoObj = { "video" : true } , errBack = function ( error) { console. log ( "Video capture error: " , error. code) ; } ; context. drawImage ( video, 0 , 0 , 330 , 250 ) ; if ( navigator. getUserMedia || navigator. webkitGetUserMedia || navigator. mozGetUserMedia) { navigator. getUserMedia = navigator. getUserMedia || navigator. webkitGetUserMedia || navigator. mozGetUserMedia; navigator. getUserMedia ( videoObj, function ( stream) { video. srcObject = stream; video. play ( ) ; } , errBack) ; } } function getBase64 ( ) { var imgSrc = canvas. toDataURL ( "image/png" ) ; return imgSrc. substring ( 22 ) ; } ; script> body> html>
curl请求代码:
function request_post ( $url = &#39;&#39; , $param = &#39;&#39; ) { if ( empty ( $url ) || empty ( $param ) ) { return false ; } $postUrl = $url ; $curlPost = $param ; $curl = curl_init ( ) ; curl_setopt ( $curl , CURLOPT_URL , $postUrl ) ; curl_setopt ( $curl , CURLOPT_HEADER , 0 ) ; curl_setopt ( $curl , CURLOPT_RETURNTRANSFER , 1 ) ; curl_setopt ( $curl , CURLOPT_POST , 1 ) ; curl_setopt ( $curl , CURLOPT_POSTFIELDS , $curlPost ) ; $data = curl_exec ( $curl ) ; curl_close ( $curl ) ; return $data ; } function request_posts ( $url = &#39;&#39; , $param = &#39;&#39; ) { if ( empty ( $url ) || empty ( $param ) ) { return false ; } $postUrl = $url ; $curlPost = $param ; $curl = curl_init ( ) ; curl_setopt ( $curl , CURLOPT_URL , $postUrl ) ; curl_setopt ( $curl , CURLOPT_HEADER , 0 ) ; curl_setopt ( $curl , CURLOPT_RETURNTRANSFER , 1 ) ; curl_setopt ( $curl , CURLOPT_SSL_VERIFYPEER , false ) ; curl_setopt ( $curl , CURLOPT_POST , 1 ) ; curl_setopt ( $curl , CURLOPT_POSTFIELDS , $curlPost ) ; $data = curl_exec ( $curl ) ; curl_close ( $curl ) ; return $data ; }
后端: 第一步:获取access_token
public function getToken ( ) { $url = &#39;https://aip.baidubce.com/oauth/2.0/token&#39; ; $post_data [ &#39;grant_type&#39; ] = &#39;client_credentials&#39; ; $post_data [ &#39;client_id&#39; ] = &#39;你创建应用给的ak&#39; ; $post_data [ &#39;client_secret&#39; ] = &#39;你创建应用给的sk&#39; ; $o = "" ; foreach ( $post_data as $k = > $v ) { $o . = "$k =" . urlencode ( $v ) . "&" ; } $post_data = substr ( $o , 0 , - 1 ) ; $res = request_post ( $url , $post_data ) ; $res = json_decode ( $res , true ) ; print_r ( $res ) ; cache ( &#39;token&#39; , $res [ &#39;access_token&#39; ] ) ; }
第二部:开始识别
public function faceVerify ( Request $request ) { $token = cache ( &#39;token&#39; ) ; $url = &#39;https://aip.baidubce.com/rest/2.0/face/v3/search?access_token=&#39; . $token ; $image = $request - > param ( &#39;img&#39; ) ; $bodys = "{\"image\":\"$image \",\"image_type\":\"BASE64\",\"group_id_list\":\"face\",\"quality_control\":\"LOW\",\"liveness_control\":\"NORMAL\"}" ; $res = request_posts ( $url , $bodys ) ; $req = json_decode ( $res , true ) ; $userList = $req [ &#39;result&#39; ] [ &#39;user_list&#39; ] [ 0 ] ; print_r ( $userList ) ; }
大功告成!!!!!!!!!!感谢观看~~