本文为大家分享了微信小程序实现人脸检测的具体代码,供大家参考,具体内容如下
因为本文章的人脸检测技术运用的是百度云人工智能,首先要有百度云的账号。
近期,人脸识别已经升级到了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 = '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,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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
微信小程序,人脸检测
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。