Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

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

来源:中文源码网    浏览:457 次    日期:2024-03-27 23:54:07
【下载文档:  微信小程序实现人脸检测功能.txt 】


微信小程序实现人脸检测功能
本文为大家分享了微信小程序实现人脸检测的具体代码,供大家参考,具体内容如下
因为本文章的人脸检测技术运用的是百度云人工智能,首先要有百度云的账号。
近期,人脸识别已经升级到了V3,开启了测试,所以也依照v3文档进行了更新;
1、人脸识别的每个接口,都需要用到百度云的access_token,首先获取 access-token ,一个月之后access_token过期;可以将获取的存入文件,再次引用时可以判断其是否过期,然后引用或者重新获取:
//获取access_token
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);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, 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 = 'http://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,0,-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,为用户基本资料;
private function init_face(){
$App_ID = '用户appid';
$API_Key = '用户api_key';
$Secret_Key = '用户secret_key';
$dir = APP_PATH.'/face-sdk/';
require_once $dir."AipFace.php";
return new \AipFace($App_ID, $API_Key, $Secret_Key);
}
(thinkPHP框架)
将所需检测图片放入文件;具体参数可依照百度云人脸识别v3文档查看。
// 人脸检测
public function facevalid(){
$file = './Upload/2018-05-17/1.png';
if (!file_exists($file)) {
die('文件不存在!');
}
$image = base64_encode(file_get_contents($file));
$imageType = 'BASE64';
//如果有可选参数
$options = array();
$options['max_face_num'] = 2;
$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;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中文源码网。

相关内容