麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 課堂 > 小程序 > 正文

微信小程序開發實現人臉檢測功能

2020-03-21 16:17:28
字體:
來源:轉載
供稿:網友

本文為大家分享了微信小程序實現人臉檢測的具體代碼,供大家參考,具體內容如下

因為本文章的人臉檢測技術運用的是百度云人工智能,首先要有百度云的賬號。

近期,人臉識別已經升級到了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;     } } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久成人黄色 | 在线播放免费播放av片 | 毛片免费观看视频 | 日本精品免费观看 | 在线免费91 | 国产papa| 免费观看高清视频网站 | 久久性生活免费视频 | 久久久久久免费免费 | chinese 军人 gay xx 呻吟 | www中文在线 | 久久精热 | 黄色大片免费看 | 黄色av网站在线观看 | 日韩午夜片 | 精品欧美一区二区精品久久 | 成人不卡| 欧美精品一区自拍a毛片在线视频 | 国产成人在线综合 | 亚洲男人的天堂在线视频 | 欧美18—19sex性hd | 欧美激情性色生活片在线观看 | 毛片在线视频观看 | 国产免费久久久久 | 可以免费看的av | 国产自在线 | 羞羞视频2023| 国产精品91在线 | 电影91| 久久区二区 | 精品av在线播放 | 99国产精品自拍 | 中文区中文字幕免费看 | 91网站链接 | 欧美中文字幕一区二区 | 操操操日日日干干干 | 最新中文字幕第一页视频 | 免费一级a毛片免费观看 | 美女视频黄视大全视频免费网址 | 91专区在线观看 | 久久精品视频在线免费观看 |