face++人脸识别

1
QQ图片20140812183958
20140812_124221
20140812_143408

上星期去参加TC 北京(Tech China Beijing)峰会,当然是免费学生票啦…..2800的门票我就呵呵了,不过学生票除了参加论坛之外权限慢慢,很满足啦展会上体验了些类似iwatch,自平衡脚踏车,虚拟3D等设备,整体来说还不错,其实我就是奔着两样东西去的:午餐+Google glass。哈哈哈,不多说了,跑远了。Google glass一直就想玩玩,毕竟一万多买不起,这次算是实现了。只不过比较坑的是只搭载了出门问问一个app,只能问问路。我最想体验的是人脸识别功能,可惜了

刚好最近在做微信,鉴于此,于是想到了加入人脸识别功能。

算是试玩吧,我就选择了face++,看上去还不错的样子。

首先当然是注册,创建应用,获取API Key和API Secret了,这里不多说。

Face++提供人脸识别接口,检测给定图片(Image)中的所有人脸(Face)的位置和相应的面部属性接口地址如下:

http://apicn.faceplusplus.com/v2/detection/detect?api_key=YOUR_API_KEY&

api_secret=YOUR_API_SECRET&url=http%3A%2F%2Ffaceplusplus.com%2Fstatic%2Fimg%2Fdemo%2F1.jpg

&attribute=glass,pose,gender,age,race,smiling

**参数** **是否必须** **含义**
api_key 必须 App的Face++ API Key
api_secret 必须 APP的Face++ API Secret
url 或 img[POST] 必须 待检测图片的URL 或者 通过POST方法上传的二进制数据,原始图片大小需要小于3M
mode 可选 检测模式可以是normal(默认) 或者 oneface 。在oneface模式中,检测器仅找出图片中最大的一张脸。
attribute 可选 可以是none或者由逗号分割的属性列表。默认为gender, age, race, smiling。目前支持的属性包括:gender, age, race, smiling, glass, pose
tag 可选 可以为图片中检测出的每一张Face指定一个不包含^@,&=*'”等非法字符且不超过255字节的字符串作为tag,tag信息可以通过 /info/get_face 查询
async 可选 如果置为true,该API将会以异步方式被调用;也就是立即返回一个session id,稍后可通过/info/get_session查询结果。默认值为false。
返回结果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{
"face": [{
"attribute": {
"age": {
"range": 5,
"value": 24
},
"gender": {
"confidence": 99.9999,
"value": "Female"
},
"glass": {
"confidence": 99.4157,
"value": "None"
},
"pose": {
"pitch_angle": {
"value": 1e-06
},
"roll_angle": {
"value": 0.650337
},
"yaw_angle": {
"value": -0.42409
}
},
"race": {
"confidence": 98.058,
"value": "Asian"
},
"smiling": {
"value": 3.78394
}
},
"face_id": "9b4e21eba34985325293d5add1c7e255",
"position": {
"center": {
"x": 50.121951,
"y": 35.97561
},
"eye_left": {
"x": 43.465122,
"y": 30.670488
},
"eye_right": {
"x": 56.80878,
"y": 30.821951
},
"height": 27.560976,
"mouth_left": {
"x": 45.649512,
"y": 45.041707
},
"mouth_right": {
"x": 55.134878,
"y": 44.858049
},
"nose": {
"x": 50.183415,
"y": 38.410732
},
"width": 27.560976
},
"tag": ""
}],
"img_height": 410,
"img_id": "438d1eea1ba05fb112c8f3430f0b219b",
"img_width": 410,
"session_id": "d48f8e1acb2d421c8ae2cb146298a7f4",
"url": "http://faceplusplus.com/static/img/demo/1.jpg"
}

取结果参数中的gender,age,race,smiling等参数 ,如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"attribute": {
"age": {
"range": 5,
"value": 17
},
"gender": {
"confidence": 99.9781,
"value": "Female"
},
"glass": {
"confidence": 99.9815,
"value": "None"
},
"pose": {
"pitch_angle": {
"value": 0.000019753399999999996
},
"roll_angle": {
"value": 1.75177
},
"yaw_angle": {
"value": 4
}
},
"race": {
"confidence": 99.471,
"value": "Asian"
},
"smiling": {
"value": 87.1365
}
}

上述结果表示:

年龄age为17,误差在5岁 性别为female,表示女,置信度为99% 种族为亚洲Asian, 微笑度 87%

和图片比较一下,还是比较一致的

040959401895877

face++的SDK如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!–?PHP  
/** * Face++ PHP SDK * author: Tianye * since: 2013-12-11 **/
class Facepp{
######################################################
### If you choose Amazon(US) server,please use the ###
### http://apius.faceplusplus.com/v2 ### ### or ###
### https://apius.faceplusplus.com/v2 ###
######################################################
var $server = ‘http://apicn.faceplusplus.com/v2’;
#var $server = ‘https://apicn.faceplusplus.com/v2’;
#var $server = ‘http://apius.faceplusplus.com/v2’;
#var $server = ‘https://apius.faceplusplus.com/v2’;
#############################################
### set your api key and api secret here. ###
#############################################
var $api_key = ‘你的api_key ‘;
var $api_secret = ‘你的api_secret ‘;
public function __construct($api_key=NULL, $api_secret=NULL, $server=NULL)
{
if($api_key){ $this—>api_key = $api_key;
}
if($api_secret){
$this->api_secret = $api_secret;
}
if($server){
$this->server = $server;
}
}

/**
* @param $method : The Face++ API
* @param $params : Request Parameters
* @return : Array {‘http_code’:’Http Status Code’, ‘request_url’:’Http Request URL’,’body’:’ JSON Response’}
**/
public function execute($method,$params){
if(empty($params)){
$params=array();
}
$params[‘api_key’] = $this->api_key;
$params[‘api_secret’] = $this->api_secret;

return $this->request("{$this->server}{$method}",$params);
}

private function request($request_url , $request_body){
$useragent = ‘Faceplusplus PHP SDK/1.0’;
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $request_url);
curl_setopt($curl_handle, CURLOPT_FILETIME, TRUE);
curl_setopt($curl_handle, CURLOPT_FRESH_CONNECT, FALSE);
curl_setopt($curl_handle, CURLOPT_CLOSEPOLICY, CURLCLOSEPOLICY_LEAST_RECENTLY_USED);
curl_setopt($curl_handle, CURLOPT_MAXREDIRS, 5);
curl_setopt($curl_handle, CURLOPT_HEADER, FALSE);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 5184000);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($curl_handle, CURLOPT_NOSIGNAL, TRUE);
curl_setopt($curl_handle, CURLOPT_REFERER, $request_url);
curl_setopt($curl_handle, CURLOPT_USERAGENT, $useragent);
if (extension_loaded(‘zlib’)){
curl_setopt($curl_handle, CURLOPT_ENCODING, ”);
}
curl_setopt($curl_handle, CURLOPT_POST, TRUE);
if(array_key_exists(‘img’,$request_body)){
$request_body[‘img’] = ‘@’.$request_body[‘img’];
}else{
$request_body=http_build_query($request_body);
}
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $request_body);
$response_text = curl_exec($curl_handle);
$reponse_header = curl_getinfo($curl_handle);
curl_close($curl_handle);
return array(‘http_code’=>$reponse_header[‘http_code’],’request_url’=>$request_url,’body’=>$response_text);
}
}

调用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!–?PHP require_once ‘facepp_sdk.php’; ######################## ### example ### ######################## $facepp = new Facepp(); #detect local image $params=array(‘img’=–>'{image file path}’);  
$params[‘attribute’] = ‘gender,age,race,smiling,glass,pose’;
$response = $facepp->execute(‘/detection/detect’,$params);
print_r($response);

#detect image by url
$params=array(‘url’=>’http://www.faceplusplus.com.cn/wp-content/themes/faceplusplus/assets/img/demo/1.jpg’);
$response = $facepp->execute(‘/detection/detect’,$params);
print_r($response);

if($response[‘http_code’]==200){
#json decode
$data = json_decode($response[‘body’],1);
#get face landmark
foreach ($data[‘face’] as $face) {
$response = $facepp->execute(‘/detection/landmark’,array(‘face_id’=>$face[‘face_id’]));
print_r($response);
}
#create person
$response = $facepp->execute(‘/person/create’,array(‘person_name’=>’unique_person_name’));
print_r($response);

#delete person
$response = $facepp->execute(‘/person/delete’,array(‘person_name’=>’unique_person_name’));
print_r($response);

}

在微信中再将结果整理成文本消息就可以了。下面是实际使用结果:
getimgdata

感兴趣的可以关注服务号bistu体验。

当然了,这只是最简单的face++和微信结合使用的案例,face++还有许多更为强大的功能等待着大家的实际应用。

悲剧的是本人识别了几张。。奈何长相太老成,识别最低年龄也要28…..俺才大二啊。。说多了都是泪~

Author

Ludis

Posted on

2014-08-20

Updated on

2014-08-20

Licensed under

Comments