百度云 获取access_token ,人脸识别

本文介绍了一种基于百度AI平台的人脸识别实现方法,通过生成唯一字符串标识每张照片,并利用PHP代码请求access_token,进而完成人脸检测。文章详细展示了从获取认证令牌到发送请求进行人脸检测的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

人工智能人脸识别,每一个照片都有一个唯一的字符串。
//生成guid
     function guid(){
         if (function_exists('com_create_guid')) {
             return com_create_guid();
         }else{
            mt_srand((double)microtime()*1000);
            $charid = strtoupper(md5(uniqid(rand(),true)));
            $hyphen = chr(45);
            $uuid = chr(123)
                    .substr($charid, 0,8).$hyphen
                    .substr($charid, 8,4).$hyphen
                    .substr($charid, 12,4).$hyphen
                    .substr($charid, 16,4).$hyphen
                    .substr($charid, 20,12)
                    .chr(125);
            return $uuid;
         }

        

     }
     function myguid(){
        $guid = guid();
        $guid = trim($guid,'{}');
        $guid = str_replace('-', '_', $guid);
        return $guid;
     }

然后写请求,获取access_token,代码如下

function https_post($url = '', $param = '') {

    // return 123;
        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'];
                    }
                }
            }catch(Exception $e){

            }
        }
        $url = 'https://aip.baidubce.com/oauth/2.0/token';
        $post_data['grant_type']       = 'client_credentials';
        $post_data['client_id']      = 'xx';
        $post_data['client_secret'] = 'xx';
        $o = "";
        foreach ( $post_data as $k => $v ) 
        {
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);
        
        $res = https_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']-3600;
        file_put_contents($file,json_encode($data));
        return $arr['access_token'];
       }else{
        return false;
       }
    }

我把access_token放在了一个文件中,存储起来。

我在后台调用了这些方法,代码如下

//人脸检测
	public function facevalid(){
		$token = access_token();
		// echo $token;

		$file = './Uploads/1.jpg';
		if (!file_exists($file)) {
			die('文件不存在!');
		}




		 $url="https://aip.baidubce.com/rest/2.0/face/v2/detect?access_token=".$token;
		    $img=file_get_contents($file);
		    $img=base64_encode($img);
		    // echo $img;    
		    $bodys=array(
		       'max_face_num'=>5,
		       'face_fileds'=>"age,beauty,expression,faceshape,gender,galsses,landmark,race,qualities",
		       'image'=>$img
		      );
		    $res=request_post($url,$bodys);
		    echo $res;
		   }
}
这些代码就可以检测人脸是否存在


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值