Face++-识别人脸性别年龄小例子
这几天在研究人脸识别,在网上发现了这个第三方的框架,Face++,想必很多人都知道了,原谅我这个渣渣现在才知道,以前我是使用Opencv学习人脸识别的,不过既然有这么好的框架(而且是免费的),于是我就花时间来学习了,用Java写了一个小Demo。
详细配置请看点击Face++环境配置
首先准备一张图片:
private String path = "E:\\FaceTemp\\a1.jpg";
接下来填写你注册Face++的应用的API Key和API Secret(首先创建应用,会自动生成的)
HttpRequests requests = new HttpRequests(KEY, SCRET, true, true);
完整代码
package com.xieth.face.test;
import java.io.File;
import org.json.JSONObject;
import com.facepp.error.FaceppParseException;
import com.facepp.http.HttpRequests;
import com.facepp.http.PostParameters;
public class FacePP {
private final static String KEY = "ff607f454e694c74d71a4fef87263***";
private final static String SCRET = "Pne34RlltSytBchRkOhI74k49GLY****";
private String path = "E:\\FaceTemp\\a1.jpg";
private HttpRequests requests = null;
private JSONObject result = null;
public FacePP() {
requests = new HttpRequests(KEY, SCRET, true, true);
PostParameters params = new PostParameters();
params.setImg(new File(path));
try {
result = requests.detectionDetect(params);
System.out.println(result);
} catch (FaceppParseException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new FacePP();
}
}
返回结果:
{
"face":[
{
"position":{
"mouth_right":{
"y":26.141162,
"x":65.187797
},
"mouth_left":{
"y":26.564891,
"x":57.389831
},
"center":{
"y":22.276029,
"x":61.355932
},
"height":13.075061,
"width":18.305085,
"nose":{
"y":24.000412,
"x":61.514237
},
"eye_left":{
"y":19.823971,
"x":56.701017
},
"eye_right":{
"y":19.52586,
"x":65.671525
}
},
"tag":"",
"face_id":"71e2d01a2ffcfa1ae92ab5535abda93a",
"attribute":{
"age":{
"range":5,
"value":30
},
"gender":{
"value":"Male",
"confidence":99.9935
},
"race":{
"value":"Asian",
"confidence":92.5875
},
"smiling":{
"value":21.5289
}
}
}
],
"session_id":"3875fb2b56f040d490ed4be79b619324",
"img_id":"ab7986078a872b5524a724e1d238827f",
"response_code":200,
"img_height":413,
"img_width":295,
"url":null
}
详细说明见下图:
服务器是以JSON数据格式进行反馈给我们的,所以我们要进行解析数据(年龄,性别)。
try {
result = requests.detectionDetect(params);
JSONArray array1 = result.getJSONArray("face");
JSONObject obj = array1.getJSONObject(0).getJSONObject("attribute");
int age = obj.getJSONObject("age").getInt("value");
String gender = obj.getJSONObject("gender").getString("value");
if (gender.equals("Male")) gender = "男";
else gender = "女";
String race = obj.getJSONObject("race").getString("value");
switch (race) {
case "Asian":
race = "黄种人";
break;
case "White":
race = "白种人";
break;
case "Black":
race = "黑种人";
break;
default:
break;
}
System.out.println("年龄:" + age + "\n" + "性别:" + gender + "\n" + "肤色:" + race);
} catch (Exception e) {
e.printStackTrace();
}
输出结果:
年龄:30
性别:男
肤色:黄种人
完整代码:
package com.xieth.face.test;
import java.io.File;
import org.json.JSONArray;
import org.json.JSONObject;
import com.facepp.error.FaceppParseException;
import com.facepp.http.HttpRequests;
import com.facepp.http.PostParameters;
public class FacePP {
private final static String KEY = "ff607f454e694c74d71a4fef87263***";
private final static String SCRET = "Pne34RlltSytBchRkOhI74k49GLY****";
private String path = "E:\\FaceTemp\\a1.jpg";
private HttpRequests requests = null;
private JSONObject result = null;
public FacePP() {
requests = new HttpRequests(KEY, SCRET, true, true);
PostParameters params = new PostParameters();
params.setImg(new File(path));
try {
result = requests.detectionDetect(params);
JSONArray array1 = result.getJSONArray("face");
JSONObject obj = array1.getJSONObject(0).getJSONObject("attribute");
int age = obj.getJSONObject("age").getInt("value");
String gender = obj.getJSONObject("gender").getString("value");
if (gender.equals("Male"))
gender = "男";
else
gender = "女";
String race = obj.getJSONObject("race").getString("value");
switch (race) {
case "Asian":
race = "黄种人";
break;
case "White":
race = "白种人";
break;
case "Black":
race = "黑种人";
break;
default:
break;
}
System.out.println("年龄:" + age + "\n" + "性别:" + gender + "\n" + "肤色:" + race);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new FacePP();
}
}
换一张照片:
输出结果:
年龄:13
性别:女
肤色:黄种人
哈哈 少女时代的允儿太年轻啦 ~~
再来一张:
年龄:35
性别:男
肤色:黑种人
来一张白种人小女孩
年龄:8
性别:女
肤色:白种人
2016年3月24日16:38:35 记录一下,到时候移植到安卓上。。。 嘿嘿