吾生也有涯,而知也无涯~欢迎优化补充、指正!
本文主要介绍基于百度的H5业务场景下人脸识别的创建与实现!
首先百度开放平台进入人脸实名认证控制台新建项目:
接着在方案清单页面点击「新建方案」按钮,方案类型选择「H5实名认证」
授权声明可以跳过
接着根据业务需求选择身份证信息录入等功能,官方提供多种认证过程,根据自身需求选用!
本例采用照片活体认证,效果图如下:
样本核验指标如下,分为严格、正常、宽松三个等级,越严格,图片对角度、模糊度、遮挡等信息参数把控越高,推荐使用宽松。
认证结果对比分数值官方推荐阀值80,可根据自身需求进行调整
完成项目配置提交后可扫码体验认证流程:
接下来进入验证实现步骤
先上代码
public function bd(){
global $_W,$_GPC;
if(empty($_W['openid'])){
header('location: ' . mobileUrl('account/login'));
}
$insertid = $_GPC['insertid']; //自定义参数,与实际认证业务处理无关
$url = 'https://aip.baidubce.com/oauth/2.0/token';
$post_data = [
'grant_type' => 'cli********ls',
'client_id' => 'L45W********n3MQ',
'client_secret' => '1Tq9********jGb6',
];
$res = $this->request_post($url, http_build_query($post_data));
$res = json_decode($res, true);
$accessToken = $res['access_token'];
/**
* 获取Verify Token
*/
$url = sprintf("https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/verifyToken/generate?access_token=%s", $accessToken);
$post_data = [
'plan_id' => "10671" // H5认证方案ID
];
$res = $this->request_post($url, json_encode($post_data));
$res = json_decode($res, true);
$verifyToken = $res['result']['verify_token'];
// 验证成功返回链接
$successUrl = mobileUrl("member/index/submit",['insertid' => $insertid,'access_token'=>$accessToken,'verify_token'=>$verifyToken],true);
// 验证失败返回链接
$failedUrl = mobileUrl("member/index/submit",['insertid' => $insertid,'access_token'=>$accessToken,'verify_token'=>$verifyToken],true);
// 生成H5验验跳转链接
$h5Url = sprintf("https://brain.baidu.com/face/print/?token=%s&successUrl=%s&failedUrl=%s", $verifyToken, urlencode($successUrl), urlencode($failedUrl));
header(sprintf("Location: %s", $h5Url));
//include($this->template());
}
public function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$curl = curl_init();//初始化curl
curl_setopt($curl, CURLOPT_URL, $postUrl);//抓取指定网页
curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($curl);//运行curl
curl_close($curl);
return $data;
}
如代码所示,先获取access_token(access_token的有效期为30天,建议每次请求重新获取),然后以此获取verify_token
业务H5网页通过获取token接口返回的verify_token信息请求认证H5页面,进行用户端流程操作
URL中的信息填写如下所示:
(1)token:填写verify_token
(2)successUrl:请求成功跳转的网址,网址需要加http/https前缀
(3)failedUrl:请求失败跳转的网址,网址需要加http/https前缀
用户端操作完成后回调上述业务地址,我们在此进行下一步处理
public function submit()
{
global $_W,$_GPC;
$accessToken = $_GPC['access_token'];
$verifyToken = $_GPC['verify_token'];
$insertid = $_GPC['insertid']; //自定义参数,与实际认证业务处理无关
/**
* 获取认证结果
*/
$apiUrl = sprintf('https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/result/detail?access_token=%s', $accessToken);
$post_data = [
'verify_token' => $verifyToken // H5认证方案ID
];
$res = $this->request_post($apiUrl, json_encode($post_data));
$res = json_decode($res, true);
if(!$res['success'] || $res['result']['verify_result']['score'] < 0.7){
$this->message("实名认证失败,请返回核对姓名和身份证号码",mobileUrl("member/dataform",[],true));
}
# other code
}
如上所示,根据verify_token进行认证查询,verify_token的独立生成方法见上文!
返回示例如下:
{
"success": true,
"result": {
"idcard_ocr_result": {
"address": "江苏省淮安市淮阴区XXX",
"birthday": "19691211",
"name": "孙XX",
"id_card_number": "320821XXXXXXX",
"gender": "女",
"nation": "汉",
"expire_time": "XXX",
"issue_authority": "XXX",
"issue_time": "XXX"
},
"verify_result": {
"liveness_score": 0.39,
"score": 11.65885925
},
"idcard_confirm": {
"name": "孙XX",
"idcard_number": "320821XXXXXXX"
}
},
"log_id": "1436111694"
}
示例根据score进行判断,数值越大则认证越严格!
以上就是人脸识别的全部流程,博文创建时间仓促,欢迎指正与讨论!
注:示例是在微擎下搭建的,其它框架请自行调整!