<?php
header("Content-Type: text/html;charset=utf-8");
header("Content-type: application/json");
/*var_dump($_FILES['pic']);*/
/*
* 1.百度AI-人脸识别,access_token获取;
* 向API服务地址使用POST发送请求,必须在URL中带上参数access_token;
* 采用人脸识别接口V3版本;
*/
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);
$data = curl_exec($curl);//运行curl
curl_close($curl);
return $data;
}
$url = 'https://aip.baidubce.com/oauth/2.0/token';
$post_data['grant_type'] = 'client_credentials';
$post_data['client_id'] = '';
$post_data['client_secret'] = '';
$o = "";
foreach ($post_data as $k => $v) {
$o .= "$k=" . urlencode($v) . "&";
}
$post_data = substr($o, 0, -1);
//获取百度AI接口access_token;
$res_acc = json_decode(request_post($url, $post_data), true);
//var_dump($res_acc);
//上传图片处理,百度AI人脸识别图片格式支持PNG、JPG、JPEG、BMP;
if (isset($_FILES['pic']) && $_FILES['pic']['error'] == 0) {
$name = md5(time());
$ext = explode(".", $_FILES['pic']['name']);
$ext = end($ext);
$full = $name . "." . $ext;
$rs = move_uploaded_file($_FILES['pic']['tmp_name'], './upload/' . $full);
if (!$rs) {
die('上传图片出错');
}
//上传成功后,获取图片地址
$pic = "https://datav.qiaodu.net/face/upload/" . $full;
//转为Base64格式编码,不包含图片头的data:image/jpg;base64;
$base64_img = base64_encode(file_get_contents($pic));
/*
* 1.人脸检测
* 检测图片中的人脸并标记出位置信息;
* 采用人脸识别接口V3版本;
*/
//检测请求参数;
$img_info[] = '';
$img_info["image"] = $base64_img;
$img_info["image_type"] = "BASE64";
$img_info["face_field"] = "faceshape,facetype";
//人脸检测与分析;
$url_info = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=' . $res_acc['access_token'];
$bodys_info = json_encode($img_info, JSON_UNESCAPED_SLASHES);//防止/或"转义;
$res_info = json_decode(request_post($url_info, $bodys_info), true);
//如图片无人脸,退出;
if (count($res_info['result']['face_num']) == 0) {
die('没有检测出人脸');
}
var_dump($res_info);
/*
* 2.创建人脸库
* 用于向人脸库中新增用户,及组内用户的人脸图片;
* 采用人脸识别接口V3版本;
*/
//前端参数传递;
$user_id = "01025623";
$user_info = "Poleung";
//百度AI检测请求参数;
$img_addinfo["image"] = $base64_img;
$img_addinfo["image_type"] = "BASE64";
$img_addinfo["group_id"] = "train";//用户组id,百度人脸库管理默认设置组别;
$img_addinfo["user_id"] = $user_id;//用户id
$img_addinfo["user_info"] = $user_info;//用户资料
$img_addinfo["quality_control"] = "LOW";//图片质量控制
$img_addinfo["liveness_control"] = "NONE";//活体检测控制
$img_addinfo["action_type"] = "APPEND";//user_id重复注册时,新注册的图片操作方式;
$url_add = 'https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?access_token=' . $res_acc['access_token'];
$bodys_add = json_encode($img_addinfo, JSON_UNESCAPED_SLASHES);
$res_add = json_decode(request_post($url_add, $bodys_add), true);
echo json_encode($res_add["error_msg"]);
}
百度人脸识别过程记录(1)
最新推荐文章于 2021-09-10 10:44:23 发布