背景:
市面上很多公司都有人脸识别技术的解决方案,比如腾讯、百度、旷世、科大讯飞。我调用的旷世Face++的人脸识别API。
原理:
Face++API-通过url设置参数请求,Face++返回参数,然后自己对参数解析,最后呈现出来,没有什么难度,主要是学会对API的使用。
申请账号:
- 旷世Face++(https://www.faceplusplus.com.cn/)注册账号
- 进入应用管理,申请API Key API Secret
API的使用:
URL:https://api-cn.faceplusplus.com/facepp/v3/detect
private static String httpRequest(String apiUrl, String picUrl) {
StringBuffer strBuffer = new StringBuffer();
try {
URL url = new URL(apiUrl);
HttpURLConnection httpURLConn = (HttpURLConnection) url.openConnection();
httpURLConn.setDoOutput(true);
httpURLConn.setDoInput(true);
httpURLConn.setRequestMethod("POST");
httpURLConn.connect();
//
DataOutputStream out = new DataOutputStream(
httpURLConn.getOutputStream());
String data = "image_url=" + java.net.URLEncoder.encode(picUrl,"UTF-8") +
"&api_key=你的api_key"&api_secret=你的&api_secret";
out.writeBytes(data);
out.flush();
out.close();
//将返回的输入流转化成字符串
InputStream inputStream = httpURLConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//
String str = null;
while((str = bufferedReader.readLine()) != null) {
strBuffer.append(str);
}
//读取完毕,释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
httpURLConn.disconnect();
} catch (Exception e) {
// TODO: handle exception
}
return strBuffer.toString();
}
//api请求网址
String apiUrl = "https://api-cn.faceplusplus.com/facepp/v3/detect?return_attributes=gender,age,eyestatus,emotion,ethnicity,skinstatus";
//调用Http请求
String json = httpRequest(apiUrl, picUrl);
请求方法:POST
必填参数:api_key,api_secret,image_url / image_file / mage_base64 (三选一)
可选参数:return_landmark,return_attributes
返回实例:
{
"image_id": "Dd2xUw9S/7yjr0oDHHSL/Q==",
"request_id": "1470472868,dacf2ff1-ea45-4842-9c07-6e8418cea78b",
"time_used": 752,
"faces": [{
"landmark": {
"mouth_upper_lip_left_contour2": {
"y": 185,
"x": 146
},
"contour_chin": {
"y": 231,
"x": 137
},
.............省略关键点信息
"right_eye_pupil": {
"y": 146,
"x": 205
},
"mouth_upper_lip_bottom": {
"y": 195,
"x": 159
}
},
"attributes": {
"gender": {
"value": "Female"
},
"age": {
"value": 21
},
"glass": {
"value": "None"
},
"headpose": {
"yaw_angle": -26.625063,
"pitch_angle": 12.921974,
"roll_angle": 22.814377
},
"smile": {
"threshold": 30.1,
"value": 2.566890001296997
}
},
"face_rectangle": {
"width": 140,
"top": 89,
"left": 104,
"height": 141
},
"face_token": "ed319e807e039ae669a4d1af0922a0c8"
}]
}