_baiDuAccount = $this->getKey($this->_apiCode); //验证是否具有访问这个接口的权限 if(!isset($this->_baiDuAccount['baiDu_face_key']) || !isset($this->_baiDuAccount['baiDu_face_secret'])){ HelperService::returnJson(['code'=>400,'msg'=>'baidu interface unauthorized access','data'=>[]]); } } /** * 获取百度人脸识别的token并且缓存一个月 * @param bool $force * @return mixed */ private function getBaiduToken($force=false){ $key = $this->_baiDuAccount['baiDu_face_key']; $secret = $this->_baiDuAccount['baiDu_face_secret']; $baiDuKey = Cache::get("baiDuKey_$key"); if(!empty($baiDuKey) && $force==false){ return $baiDuKey; } $url = $this->_baseUrl."/oauth/2.0/token?grant_type=client_credentials&client_id=$key&client_secret=$secret&"; $res = HelperService::httpPost($url,'',true); $resultArr = @json_decode($res,true); if(isset($resultArr['error'])){ HelperService::returnJson([ 'code'=>400, 'msg'=>"baidu err msg", 'data'=>$res ]); } Cache::set("baiDuKey_$key",$resultArr['access_token'],$resultArr['expires_in']); return $resultArr['access_token']; } /** * 请求百度人脸识别匹配人脸接口 */ public function match(){ $params = $this->_params; $rule = [ 'old_image|原始图片'=>'require', 'old_image_type|原始图片类型'=>'require', 'now_image|现在的图片'=>'require', 'now_image_type|现在的图片类型'=>'require', ]; $validate = new Validate($rule); if(!$validate->check($params)){ HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>$params]); } $imageType = ['URL','FACE_TOKEN','BASE64']; if(!in_array($params['old_image_type'],$imageType) || !in_array($params['now_image_type'],$imageType)){ HelperService::returnJson(['code'=>400,'msg'=>'image_type value is error','data'=>[]]); } $matchImage = [ [ 'image'=>"{$params['old_image']}", 'image_type'=>"{$params['old_image_type']}", "face_type"=>"LIVE", "quality_control"=>"NORMAL", "liveness_control"=>"NORMAL" ],[ 'image'=>"{$params['now_image']}", 'image_type'=>"{$params['now_image_type']}", "face_type"=>"LIVE", "quality_control"=>"NORMAL", "liveness_control"=>"NORMAL" ] ]; $token = $this->getBaiduToken(); //重试2次 $times = 2; requestMatch: $url = $this->_baseUrl.'/rest/2.0/face/v3/match?access_token='.$token; $res = HelperService::httpPost($url,json_encode($matchImage),true); $resultArr = @json_decode($res,true); if(isset($resultArr['error_code']) && $resultArr['error_code'] == 110 && $times>0){ $token = $this->getBaiduToken(true); $times--; goto requestMatch; } if(isset($resultArr['error_code']) && $resultArr['error_code'] == 222209){ $data = $this->register("{$params['now_image']}", "{$params['now_image_type']}"); HelperService::returnJson(['code'=>201,'msg'=>"success",'data'=>$data]); } if(stripos($res,'cheek ') >0){ HelperService::returnJson(['code'=>400,'msg'=>'请检查脸颊是否被遮挡或光线太弱!','data'=>$res]); } if(stripos($res,'eye ') >0){ HelperService::returnJson(['code'=>400,'msg'=>'请检查眼睛是否被遮挡或闭眼!','data'=>$res]); } if(stripos($res,'face ') >0){ HelperService::returnJson(['code'=>400,'msg'=>'请检查脸部是否被遮挡或光线太弱!','data'=>$res]); } if(stripos($res,'pic ') >0){ HelperService::returnJson(['code'=>400,'msg'=>'请检查图片是否完整或不是脸部图片!','data'=>$res]); } if(!isset($resultArr['result']['score'])){ HelperService::returnJson(['code'=>400,'msg'=>'验证失败','data'=>$res]); } HelperService::returnJson(['code'=>200,'msg'=>'success','data'=>['score'=>strval(round("{$resultArr['result']['score']}",2)),'res'=>$resultArr['result']['face_list']]]); } /** * 注册人脸 * @param type $img * @param type $type * @param type $group_id */ private function register($img,$type,$group_id="default",$userId=null){ $userId = empty($userId)?$userId:uniqid(); $token = $this->getBaiduToken(true); //重试2次 $times = 2; requestRegister: $url = $this->_baseUrl.'/rest/2.0/face/v3/faceset/user/add?access_token='.$token; \think\Log::record("requestRegister:".$url."==>".$times); $res = HelperService::httpPost($url,json_encode([ "image"=>$img, 'image_type'=>$type, "group_id"=>$group_id, "user_id"=> $userId, ]),true); $resultArr = @json_decode($res,true); if(isset($resultArr['error_code']) && $resultArr['error_code'] == 110 && $times>0){ $token = $this->getBaiduToken(true); $times--; goto requestRegister; } if(empty($resultArr) || $resultArr['error_code']>0){ HelperService::returnJson(['code'=>400,'msg'=>'注册人脸失败','data'=>$res]); } return ['res'=>$resultArr,'face_token'=>"{$resultArr['result']['face_token']}"]; } /** * 人脸活体检测接口 */ public function detect(){ $params = $this->_params; $rule = [ 'image|原始图片'=>'require|url', ]; $validate = new Validate($rule); if(!$validate->check($params)){ HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]); } $detectImage = [ 'image'=>"{$params['image']}", 'image_type'=>'URL', 'face_field'=>'quality' ]; $token = $this->getBaiduToken(); //重试2次 $times = 2; requestDetect: $url = $this->_baseUrl.'/rest/2.0/face/v3/faceverify?access_token='.$token; $res = HelperService::httpPost($url,json_encode([$detectImage]),true); $resultArr = @json_decode($res,true); if(isset($resultArr['error_code']) && $resultArr['error_code'] == 110 && $times>0){ $token = $this->getBaiduToken(true); $times--; goto requestDetect; } if(!isset($resultArr['error_msg']) || $resultArr['error_msg']!="SUCCESS" ){ HelperService::returnJson(['code'=>400,'msg'=>'人脸活体检测失败','data'=>$res]); } if(!isset($resultArr['result']['face_liveness']) || round("{$resultArr['result']['face_liveness']}",2)< 0.97){ HelperService::returnJson(['code'=>400,'msg'=>'请采集活体人脸照片','data'=>$resultArr,'liveness'=>round("{$resultArr['result']['face_liveness']}",2)]); } if(!isset($resultArr['result']['face_list'][0]['quality']['completeness']) || $resultArr['result']['face_list'][0]['quality']['completeness']<1){ HelperService::returnJson(['code'=>400,'msg'=>'照片脸部不完整','data'=>$resultArr,'completeness'=>$resultArr['result']['face_list'][0]['quality']['completeness']]); } if(!isset($resultArr['result']['face_list'][0]['quality']['illumination']) || $resultArr['result']['face_list'][0]['quality']['illumination']<80){ HelperService::returnJson(['code'=>400,'msg'=>'照片不够清晰度','data'=>$resultArr,'illumination'=>$resultArr['result']['face_list'][0]['quality']['illumination']]); } HelperService::returnJson(['code'=>200,'msg'=>'success','data'=>['res'=>$resultArr,'face_token'=>"{$resultArr['result']['face_list'][0]['face_token']}"]]); } }