微信小程序?qū)崿F(xiàn)人臉檢測功能
本文為大家分享了微信小程序?qū)崿F(xiàn)人臉檢測的具體代碼,供大家參考,具體內(nèi)容如下
因?yàn)楸疚恼碌娜四槞z測技術(shù)運(yùn)用的是百度云人工智能,首先要有百度云的賬號(hào)。
近期,人臉識(shí)別已經(jīng)升級(jí)到了V3,開啟了測試,所以也依照v3文檔進(jìn)行了更新;
1、人臉識(shí)別的每個(gè)接口,都需要用到百度云的access_token,首先獲取 access-token ,一個(gè)月之后access_token過期;可以將獲取的存入文件,再次引用時(shí)可以判斷其是否過期,然后引用或者重新獲?。?/p>
//獲取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);//抓取指定網(wǎng)頁 curl_setopt($curl, CURLOPT_HEADER, 0);//設(shè)置header curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求結(jié)果為字符串且輸出到屏幕上 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);//運(yùn)行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、創(chuàng)建初始化方法,需要用到 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框架)
將所需檢測圖片放入文件;具體參數(shù)可依照百度云人臉識(shí)別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'; //如果有可選參數(shù) $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) { //人臉數(shù)量為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('人臉數(shù)大于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; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS和css實(shí)現(xiàn)檢測移動(dòng)設(shè)備方向的變化并判斷橫豎屏幕
這篇文章主要介紹了JS和css實(shí)現(xiàn)檢測移動(dòng)設(shè)備方向的變化并判斷橫豎屏幕,本文分別給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05使用BroadcastChannel進(jìn)行跨窗口通信的實(shí)例詳解
BroadcastChannel 提供了一種簡單而有效的方式來實(shí)現(xiàn)同一瀏覽器環(huán)境下不同頁面或腳本之間的通信,對于需要跨窗口、標(biāo)簽頁或 iframe 同步數(shù)據(jù)的應(yīng)用場景,它是一種非常便捷的解決方案,本文介紹了如何使用 BroadcastChannel 進(jìn)行跨窗口通信,需要的朋友可以參考下2024-08-08Javascript 對象(object)合并操作實(shí)例分析
這篇文章主要介紹了Javascript 對象(object)合并操作,結(jié)合實(shí)例形式分析了javascript基于jQuery的extend方法、對象屬性、遍歷賦值等操作實(shí)現(xiàn)對象合并相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2019-07-07JavaScript 節(jié)點(diǎn)操作 以及DOMDocument屬性和方法
最近發(fā)現(xiàn)DOMDocument對象很重要,還有XMLHTTP也很重要 注意大小寫一定不能弄錯(cuò).2007-12-12