最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信小程序?qū)崿F(xiàn)人臉檢測功能

 更新時間:2018年05月25日 16:43:04   作者:WhiteJ  
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)人臉檢測功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文為大家分享了微信小程序?qū)崿F(xiàn)人臉檢測的具體代碼,供大家參考,具體內(nèi)容如下

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

近期,人臉識別已經(jīng)升級到了V3,開啟了測試,所以也依照v3文檔進(jìn)行了更新;

1、人臉識別的每個接口,都需要用到百度云的access_token,首先獲取 access-token ,一個月之后access_token過期;可以將獲取的存入文件,再次引用時可以判斷其是否過期,然后引用或者重新獲?。?/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ù)可依照百度云人臉識別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)文章

最新評論

循化| 施秉县| 含山县| 和静县| 广宗县| 星座| 丰原市| 金沙县| 瑞金市| 台南县| 资中县| 衡阳县| 承德县| 甘南县| 北川| 原平市| 林甸县| 雷山县| 滦南县| 句容市| 云龙县| 南华县| 利辛县| 宿迁市| 永康市| 高青县| 永福县| 龙游县| 丹凤县| 华蓥市| 蒙山县| 新巴尔虎左旗| 玛纳斯县| 屏山县| 莱阳市| 南投县| 界首市| 天峻县| 许昌县| 兴海县| 二连浩特市|