经过百度API的人脸检测,检测代码如下所示:
package com.xunpu;
import com.baidu.aip.face.AipFace;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
public class Sample {
//设置APPID/AK/SK
public static final String APP_ID = "16879113";
public static final String API_KEY = "twBwSMN5IkTKyM9vpVa0VS4P";
public static final String SECRET_KEY = "NwqlbFKFyxIUMA5yziXLzl4EHFknYZlj";
public static void main(String[] args) {
HashMap<String, String> options = new HashMap<>();
options.put("face_field", "age,beauty,expression,faceshape,gender,glasses,landmark,race,quality,facetype");
options.put("max_face_num", "1");//将脸部字段的属性和人脸数量放入map中。
// 初始化一个AipFace
AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
try {
byte[] image1= IOUtils.toByteArray(new FileInputStream("C:\\Users\\Lenovo\\Desktop\\1.jpeg"));
// 调用接口
String image = Base64.getEncoder().encodeToString(image1);
String imageType = "BASE64";
// 人脸检测
JSONObject res = client.detect(image, imageType,options);
System.out.println(res.toString(2));
} catch (IOException e) {
e.printStackTrace();
}
}
}
得到结果:
"C:\Program Files\Java\jdk1.8.0_131\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1\lib\idea_rt.jar=7827:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;D:\Java项目\faceIdentification\target\test-classes;D:\Java项目\faceIdentification\target\classes;D:\Java项目\faceIdentification;E:\Maven\repository\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;E:\Maven\repository\com\baidu\aip\java-sdk\4.11.3\java-sdk-4.11.3.jar;E:\Maven\repository\org\json\json\20160810\json-20160810.jar;E:\Maven\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;E:\Maven\repository\org\slf4j\slf4j-simple\1.7.25\slf4j-simple-1.7.25.jar;E:\Maven\repository\commons-io\commons-io\2.6\commons-io-2.6.jar;E:\Maven\repository\commons-codec\commons-codec\1.12\commons-codec-1.12.jar;E:\Maven\repository\com\google\code\gson\gson\2.8.5\gson-2.8.5.jar;E:\Maven\repository\junit\junit\4.13-beta-3\junit-4.13-beta-3.jar;E:\Maven\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;E:\Maven\repository\org\junit\jupiter\junit-jupiter-api\5.5.1\junit-jupiter-api-5.5.1.jar;E:\Maven\repository\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;E:\Maven\repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;E:\Maven\repository\org\junit\platform\junit-platform-commons\1.5.1\junit-platform-commons-1.5.1.jar" com.xunpu.Sample
[main] INFO com.baidu.aip.client.BaseClient - get access_token success. current state: STATE_AIP_AUTH_OK
{
"result": {
"face_num": 1,
"face_list": [{
"expression": {
"probability": 1,
"type": "none"
},
"face_shape": {
"probability": 0.3,
"type": "heart"
},
"beauty": 82.11,
"gender": {
"probability": 1,
"type": "female"
},
"race": {
"probability": 1,
"type": "yellow"
},
"face_probability": 1,
"quality": {
"illumination": 212,
"occlusion": {
"right_eye": 0.02,
"nose": 0,
"mouth": 0,
"left_eye": 0,
"left_cheek": 0.03,
"chin_contour": 0,
"right_cheek": 0.03
},
"blur": 0,
"completeness": 1
},
"glasses": {
"probability": 1,
"type": "none"
},
"landmark72": [
{
"x": 247.35,
"y": 184.19
},
{
"x": 252.69,
"y": 209.74
},
{
"x": 259.9,
"y": 235.25
},
{
"x": 272.43,
"y": 260.09
},
{
"x": 296.91,
"y": 280.98
},
{
"x": 323.77,
"y": 291.59
},
{
"x": 349.93,
"y": 293.79
},
{
"x": 369.06,
"y": 282.94
},
{
"x": 384.32,
"y": 263.59
},
{
"x": 392.5,
"y": 242.55
},
{
"x": 397.38,
"y": 221.83
},
{
"x": 400.03,
"y": 201.29
},
{
"x": 400.38,
"y": 180.99
},
{
"x": 292.34,
"y": 175.74
},
{
"x": 300.37,
"y": 169.65
},
{
"x": 309.09,
"y": 167.44
},
{
"x": 317.34,
"y": 169.47
},
{
"x": 324.37,
"y": 176.7
},
{
"x": 317.23,
"y": 178.87
},
{
"x": 308.67,
"y": 180.25
},
{
"x": 299.87,
"y": 178.88
},
{
"x": 309.15,
"y": 173.79
},
{
"x": 279.08,
"y": 158.07
},
{
"x": 290.93,
"y": 145.36
},
{
"x": 305.42,
"y": 141.89
},
{
"x": 319.39,
"y": 142.98
},
{
"x": 331.51,
"y": 151.53
},
{
"x": 318.82,
"y": 151.36
},
{
"x": 305.54,
"y": 150.46
},
{
"x": 292.43,
"y": 152.25
},
{
"x": 361.08,
"y": 177.05
},
{
"x": 367.64,
"y": 169.46
},
{
"x": 375.97,
"y": 167.23
},
{
"x": 383.4,
"y": 169.38
},
{
"x": 389.19,
"y": 175.84
},
{
"x": 384.04,
"y": 178.96
},
{
"x": 376.47,
"y": 180.04
},
{
"x": 368.28,
"y": 179.05
},
{
"x": 373.05,
"y": 173.64
},
{
"x": 358.88,
"y": 152.45
},
{
"x": 368.56,
"y": 143.9
},
{
"x": 379.5,
"y": 142.63
},
{
"x": 390.55,
"y": 145.58
},
{
"x": 397.58,
"y": 156.75
},
{
"x": 389.29,
"y": 151.97
},
{
"x": 379.61,
"y": 150.91
},
{
"x": 369.13,
"y": 151.78
},
{
"x": 335.96,
"y": 176.92
},
{
"x": 335.01,
"y": 189.3
},
{
"x": 333.82,
"y": 201.77
},
{
"x": 329.44,
"y": 216.69
},
{
"x": 341.68,
"y": 215.2
},
{
"x": 357.48,
"y": 215.58
},
{
"x": 365,
"y": 216.12
},
{
"x": 361.33,
"y": 200.98
},
{
"x": 357.77,
"y": 188.73
},
{
"x": 354.16,
"y": 176.56
},
{
"x": 353.18,
"y": 204.55
},
{
"x": 322.1,
"y": 248.3
},
{
"x": 334.9,
"y": 237.3
},
{
"x": 349.74,
"y": 234.1
},
{
"x": 361.54,
"y": 236.42
},
{
"x": 366.88,
"y": 247.52
},
{
"x": 360.99,
"y": 256.28
},
{
"x": 349.7,
"y": 260.13
},
{
"x": 334.54,
"y": 257.11
},
{
"x": 336.07,
"y": 243.25
},
{
"x": 349.29,
"y": 241.7
},
{
"x": 359.32,
"y": 242.82
},
{
"x": 358.63,
"y": 248.86
},
{
"x": 349.1,
"y": 249.5
},
{
"x": 336.58,
"y": 249.57
}
],
"face_type": {
"probability": 0.97,
"type": "human"
},
"angle": {
"roll": -2.48,
"pitch": -0.54,
"yaw": -11.53
},
"face_token": "69079c75751088681baee67638093169",
"location": {
"top": 142.26,
"left": 247.4,
"rotation": 0,
"width": 153,
"height": 151
},
"landmark": [
{
"x": 309.15,
"y": 173.79
},
{
"x": 373.05,
"y": 173.64
},
{
"x": 353.18,
"y": 204.55
},
{
"x": 347.81,
"y": 246.63
}
],
"age": 23
}]
},
"log_id": 304592857487057141,
"error_msg": "SUCCESS",
"cached": 0,
"error_code": 0,
"timestamp": 1565748705
}
Process finished with exit code 0
可以发现,有一大堆是我们不需要的结果,如72个标记点(landmark)。现在我们只需要提取我们需要的内容(即性别、年龄、颜值、脸型、表情、是否带眼镜、种族)即可。
//如果解析成功,就将JSONObject对象进行解析。设置默认空字符串的原因是:避免空指针异常。
if (detect.optString("error_msg", "").equals("SUCCESS")) {
JSONObject resObject = detect.optJSONObject("result");
if (resObject.optInt("face_num") == 1) {
JSONObject faceObj = (JSONObject) resObject.optJSONArray("face_list").get(0);
FaceInfo faceInfo = parseAttribute(faceObj);
return faceInfo;
}
}
//将得到的json串进行解析(对每部分解析)
private static FaceInfo parseAttribute (JSONObject faceobj){
FaceInfo res = new FaceInfo();
//表情
String expression=faceobj.optJSONObject("expression").getString("type");
switch (expression){
case "smile":
res.setExpression("微笑");
break;
case "laugh":
res.setExpression("大笑");
break;
case "none":
default:
res.setExpression("无");
break;
}
//face_shape脸形
String faceShape=faceobj.optJSONObject("face_shape").getString("type");
switch (faceShape){
case "squre":
res.setFaceShape("国字脸");
break;
case "triangle":
res.setFaceShape("瓜子脸");
break;
case "oval":
res.setFaceShape("鹅蛋脸");
break;
case "heart":
res.setFaceShape("心形脸");
break;
case "round":
res.setFaceShape("圆形脸");
break;
default:
res.setFaceShape("未知");
break;
}
//beauty颜值
double beauty=faceobj.optDouble("beauty");
res.setBeauty(beauty);
//gender 性别
String gender=faceobj.optJSONObject("gender").getString("type");
switch (gender){
case "female":
res.setGender("女");
break;
case "male":
res.setGender("男");
break;
default:
res.setGender("未知");
break;
}
//race人种
String race=faceobj.optJSONObject("race").getString("type");
switch(race){
case "yellow":
res.setRace("亚美人种");
break;
case "white":
res.setRace("欧亚人种");
break;
case "black":
res.setRace("赤道人种");
break;
case "arabs":
res.setRace("大洋洲人种");
break;
default:
res.setRace("未知");
break;
}
//glasses 是否带眼镜
String glasses=faceobj.optJSONObject("glasses").getString("type");
switch(glasses){
case "common":
res.setGlasses("普通眼镜");
break;
case "sun":
res.setGlasses("墨镜");
break;
case "none":
res.setGlasses("无眼镜");
break;
default:
res.setGlasses("未知");
break;
}
//年龄
int age=faceobj.optInt("age");
res.setAge(age);
return res;
}
最后得到的结果为: