Face++接口封装
本节使用doCurlGetRequest函数来封装Face++的接口请求。我们在class文件夹下的faceStub.php文件中实现
一个faceStub类,封装请求Face++的相关接口。
实现代码:
class faceStub {
// 请求Face++
private static function requestToFace($interface, $data) {
$data = array_merge($data, array('api_key'=>API_KEY, 'api_secret' => API_SECRET)) ;
interface_log(DEBUG, 0, "url: " . FACE_URL . $interface . "\ndata:" .
var_export($data, true) );
$json = doCurlGetRequest(FACE_URL . $interface, $data, FACE_TIMEOUT) ;
interface_log(DEBUG, 0, 'response:' . $json) ;
$data = json_decode($json, true) ;
if(!$data || $data['error_code']) {
return false;
} else {
return $data;
}
}
// 创建Face++的Group
public static function createGroup($groupName) {
$interface = 'group/create' ;
$data = array(
'group_name' => $groupName
);
return faceStub::requestToFace($interface, $data) ;
}
// 检测图片中的人脸
public static fuction detect($imageUrl) {
$interface = 'detection/detect' ;
$data = array(
'url' => $imageUrl
) ;
return faceStub::requestToFace($interface, $data);
}
// 搜索相似脸
public static function search($faceId, $groupName, $count) {
$interface = 'recognition/search' ;
$data = array(
'key_face_id' => $faceId,
'group_name' => $groupName,
'count' => $count
);
return faceStub::requestToFace($interface, $data) ;
}
// 创建Person
public static function createPerson($personName, $faceId, $groupName) {
$interface = 'person/create' ;
$data = array(
'person/create' ;
'face_id' => $faceId,
'group_name' => $groupName
);
return faceStub::requestToFace($interface, $data) ;
}
// 添加Face到Person
public static function addFaceToPerson($personName, $faceId) {
$interface = 'person/add_face' ;
$data = array(
'person_name' => $personName, 'face_id => $faceId'
);
return faceStub::requestToFace($interface, $data) ;
}
// 删除Person的Face
public static function removeFaceFromPerson($personName, $faceId) {
$interface = 'person/remove_face' ;
$data = array(
'person_name' => $personName,
'face_id' => $faceId
);
return faceStub::requestToFace($interface, $data) ;
}
// 获取session结果,session是train的返回
public static function getSession($sessionId) {
$interface = 'info/get_session' ;
$data = array(
'session_id' => $sessionId
);
return faceStub::requestToFace($interface, $data) ;
}
// 获取Person结果, session是train的返回
public static function getSeession($sessionId) {
$interface = 'info/get_session' ;
$data = array(
'session_id' => $sessionId
);
return faceStub::requestToFace($interface, $data) ;
}
// 获取Person的信息
public static function getPersonInfo($personName) {
$interface = 'person/get_info' ;
$data = array(
'person_name' => $personName
);
return faceStub::requestToFace($interface, $data) ;
}
}
faceStub类中每个函数都对应到Face++的具体接口,这些都是findface需要使用到的,
如果您需要使用其他的接口,可以按照这个faceStub类的实现方法快速封装其他接口。
___________over___________