加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

微信小程序实现人脸检测功能

发布时间:2020-12-14 20:08:14 所属栏目:资源 来源:网络整理
导读:本文为大家分享了微信小程序实现人脸检测的具体代码,供大家参考,具体内容如下 因为本文章的人脸检测技术运用的是百度云人工智能,首先要有百度云的账号。 近期,人脸识别已经升级到了V3,开启了测试,所以也依照v3文档进行了更新; 1、人脸识别的每个接口

本文为大家分享了微信小程序实现人脸检测的具体代码,供大家参考,具体内容如下

因为本文章的人脸检测技术运用的是百度云人工智能,首先要有百度云的账号。

近期,人脸识别已经升级到了V3,开启了测试,所以也依照v3文档进行了更新;

1、人脸识别的每个接口,都需要用到百度云的access_token,首先获取 access-token ,一个月之后access_token过期;可以将获取的存入文件,再次引用时可以判断其是否过期,然后引用或者重新获取:

$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);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,CURLOPT_SSLVERSION,1);
$data = curl_exec($curl);//运行curl
curl_close($curl);

return $data;
}

function access_token(){
$file= DIR .'access_token';
if(file_exists($file)){
$str=file_get_contents($file);
try{
$arr=json_decode($str,true);
if(is_array($arr)){
$totime=$arr['totime'];
if($totime>time()){
return $arr['access_token'];
exit;
}
}
}catch(Exception $e){

} 

}

$url = 'https://aip.baidubce.com/oauth/2.0/token';
$post_data['grant_type'] = 'client_credentials';
$post_data['client_id'] = 'fRuY7eOPxBzIHf4qxiYeQOHT';
$post_data['client_secret'] = 'oe7L7aPc5rcKfSewvb5h6xFX2a8dEQN1';
$o = "";
foreach ( $post_data as $k => $v )
{
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,-1);

$res = request_post($url,$post_data);

$arr=json_decode($res,true);
if(isset($arr['access_token']) && isset($arr['expires_in'])){
$data['access_token'] = $arr['access_token'];
$data['totime']= time() + $arr['expires_in'] - 86400;
file_put_contents($file,json_encode($data));
return $arr['access_token'];
}else{
return false;
}

}

2、创建初始化方法,需要用到 Secret_Key、API_Key、App_ID,为用户基本资料;

$dir = APP_PATH.'/face-sdk/'; require_once $dir."AipFace.php"; return new AipFace($App_ID,$API_Key,$Secret_Key);

}

(thinkPHP框架)

将所需检测图片放入文件;具体参数可依照百度云人脸识别v3文档查看。

$client = $this->init_face(); $ret = $client->detect($image,$imageType,$options); // print_r($ret); if ($ret['error_code'] == 0) {//有人脸 $result = $ret['result']; $face_num = $result['face_num']; if ($face_num==1) { //人脸数量为1 $face_probability = $result['face_list'][0]['face_probability']; if ($face_probability==1) { //可靠性为1 $user_id = myguid(); $group_id = $this->face_group(); $res = $client->addUser($image,'BASE64',$group_id,$user_id); // print_r($res); if ($res['error_code']==0) { echo "人脸检测完成,并入库"; } }else{ die('可靠性为:'.$face_probability); } }else{ die('人脸数大于1'); } }else{ die('没有人脸'); }

}
// 获取组
private function face_group(){
$groupname = '10001';
$client = $this->init_face();
$ret = $client->getGroupList();
if ($ret['error_code'] == 0) {
$grouplist = $ret['result']['group_id_list'];
if (in_array($groupname,$grouplist)) {
return $groupname;
}else{
$ret = $client->groupAdd($groupname);
if ($ret['error_code'] == 0) {
return $groupname;
}else{
return false;
}
}
}else{
return false;
}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读