怎么把百度云人脸识别添加到百度云人脸库中

1.首先注册一个百度云账号,然后在产品服务,人工智能里找到人脸识别,创建一个应用,在应用里创建一个名为人脸的群组


2.在index控制器中写两个方法

public function face_group(){
        $groupname='face';

        $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;
        }
   }
 public function facevalid(){
     $token=access_token();
     // echo $token;
    $file='./Uploads/2018-05-17/73.jpg';
    if (!file_exists($file)) {
      die('文件不存在');
    }
		$img = file_get_contents($file);
		$img = base64_encode($img);
		// echo $img;
		$options=array();
		$options['max_face_num']=2;
		$client=$this->init_face();
		$ret=$client->detect($img,'BASE64',$options);

        if ($ret['error_code']==0) {
        	$result=$ret['result'];
        	$face_num=$result['face_num'];

        	if ($face_num==1) {
        		$face_probability=$result['face_list'][0]['face_probability'];
        		if ($face_probability==1) {
        			$guid=myguid();
        			$group=$this->face_group();
        			$client->addUser($img,'BASE64',$group,$guid);
        			echo "人脸检测完成,并已入库";
        		}else{
        			die('可靠性为:'.$face_probability);
        		}
        	}else{
        		die('人脸数量大于1');
        	}
        }
   }
3.在这之前先初始化人脸识别
private function init_face(){
       $appid='你的appid';
       $apikey='你的apikey';
       $secretkey='你的secretkey ';

       $dir=APP_PATH.'/face/';
       require_once $dir .'AipFace.php';
       return new \AipFace($appid,$apikey,$secretkey);
	}
这样就可以添加到人脸库中啦

猜你喜欢

转载自blog.csdn.net/qq_41860519/article/details/80390889