PHP接入百度人臉識別,供大家參考,具體內(nèi)容如下
1.注冊百度開發(fā)者賬號。
https://login.bce.baidu.com/?account= 注冊登錄成功后,進(jìn)入控制臺,在左邊的導(dǎo)航欄找到產(chǎn)品-》人工智能-》人臉識別:
創(chuàng)建應(yīng)用。創(chuàng)建應(yīng)用成功后拿到應(yīng)用的API Key和Secret Key
2.接口調(diào)用-獲取access token
調(diào)用人臉識別接口前需要獲取access token,示例代碼:
function execCurl($url, $param) {
if (empty($url) || empty($param)) {
return array('result' => false, 'err_msg' => 'url or param is null');
}
$curl = curl_init();//初始化curl
curl_setopt($curl, CURLOPT_URL, $url);//抓取指定網(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, $param);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($curl);//運行curl
$curl_err_msg = curl_error($curl);
curl_close($curl);
if($data === false) {
return array('result' => false, 'err_msg' => $curl_err_msg);
}else {
return array('result' => true, 'data' => $data);
}
}
$url = 'https://aip.baidubce.com/oauth/2.0/token';
$param = array(
'grant_type' => 'client_credentials',
'client_id' => '你的API Key',
'client_secret' => '你的Secret Key'
);
$result = execCurl($url, $param);
3.調(diào)用人臉檢查接口
如果需要判斷一張圖片中的人臉,是否符合后續(xù)識別或者對比的條件,在請求時在face_field參數(shù)中請求quality。
下面示例是請求圖文為base64轉(zhuǎn)碼字符串(轉(zhuǎn)碼不能包含頭信息),也可以傳圖片網(wǎng)絡(luò)地址,官方文檔
示例代碼:
function checkPictureToCheckFace($image_path, $access_token) {
$url = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=' . $access_token;
$param = array(
'image' => base64EncodeImage($image_path),
'image_type' => 'BASE64',
'face_field' => 'quality'
);
$param = json_encode($param);
$result = execCurl($url, $param);
if($result['result'] == false) return $result;
$data = json_decode($result['data'], true);
if($data['error_code'] != 0) return array('result' => false, 'err_msg' => '上傳的人臉不符合要求,請重新上傳五官清晰的真實頭像');
$err_result = array('result' => false, 'err_msg' => '上傳的人臉不符合要求,請重新上傳五官清晰的真實頭像!');
$quality = $data['result']['face_list'][0]['quality'];
if($quality['occlusion']['left_eye'] > 0.6) return $err_result; // 左眼被遮擋的閾值
if($quality['occlusion']['right_eye'] > 0.6) return $err_result; // 右眼被遮擋的閾值
if($quality['occlusion']['nose'] > 0.7) return $err_result; // 鼻子被遮擋的閾值
if($quality['occlusion']['mouth'] > 0.7) return $err_result; // 嘴巴被遮擋的閾值
if($quality['occlusion']['left_cheek'] > 0.8) return $err_result; // 左臉頰被遮擋的閾值
if($quality['occlusion']['right_cheek'] > 0.8) return $err_result; // 右臉頰被遮擋的閾值
if($quality['occlusion']['chin_contour'] > 0.6) return $err_result; // 下巴被遮擋閾值
if($quality['blur'] != 0 $quality['blur'] > 0.7) return $err_result; // 模糊度范圍 0是最清晰,1是最模糊
if($quality['illumination'] 40) return $err_result; // 光照范圍
if($quality['completeness'] == 0) return $err_result; // 人臉完整度 0為人臉溢出圖像邊界,1為人臉都在圖像邊界內(nèi)
return array('result' => true);
}
function execCurl($url, $param) {
if (empty($url) || empty($param)) {
return array('result' => false, 'err_msg' => 'url or param is null');
}
$curl = curl_init();//初始化curl
curl_setopt($curl, CURLOPT_URL, $url);//抓取指定網(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, $param);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($curl);//運行curl
$curl_err_msg = curl_error($curl);
curl_close($curl);
if($data === false) {
return array('result' => false, 'err_msg' => $curl_err_msg);
}else {
return array('result' => true, 'data' => $data);
}
}
function base64EncodeImage($image_path) {
$base64_image = '';
$image_info = getimagesize($image_path);
$image_data = fread(fopen($image_path, 'r'), filesize($image_path));
$base64_image = chunk_split(base64_encode($image_data));
return $base64_image;
}
$image_path = 'image/1.jpg';
$access_token = '上一步的獲取access token';
$result = checkPictureToCheckFace($image_path, $access_token);
判斷是否符合人臉識別檢查的字段說明:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- PHP實現(xiàn)微信小程序人臉識別刷臉登錄功能
- PHP使用Face++接口開發(fā)微信公眾平臺人臉識別系統(tǒng)的方法
- 基于OpenCV的PHP圖像人臉識別技術(shù)