在线体验V3人脸检测地址
https://www.ydxiaoshuai.cn/xai/rest/face/index
先来看一个V1-V3版本的人脸检测比较
V1-3版本人脸检测数据对比
可以发现。同一张图片的你。V3版本更年轻了哦
------------------------------------------分割线代码开始------------------------------------------
JavaAPI示例代码
增加了image_type参数,face_field参数后面不再加s了
package com.xs.face;
import java.net.URLEncoder;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xs.pojo.face.FaceV3DetectBean;
import com.xs.util.baidu.Base64Util;
import com.xs.util.baidu.FileUtil;
import com.xs.util.baidu.HttpUtil;
public class FaceDetectV3Sample {
/**
* 人脸检测V3版本URL
*/
public static String FACE_DETECT_URL="https://aip.baidubce.com/rest/2.0/face/v3/detect";
public static void main(String[] args) throws Exception {
String filePath ="./face.jpg";
String result = faceDetect(filePath,"1","自己应用获取的AccessToken");
System.out.println("=====接口返回内容:"+result);
JSON json = JSON.parseObject(result);
FaceV3DetectBean bean = JSONObject.toJavaObject(json, FaceV3DetectBean.class);
System.out.println("人脸数量:"+bean.getResult().getFace_num());
System.out.println("美丑打分:"+bean.getResult().getFace_list().get(0).getBeauty());
System.out.println("年龄:"+bean.getResult().getFace_list().get(0).getAge());
}
/**
* 所有参数人脸检测
* @param filePath 图片路径数据,图片大小不超过2M。
* @param max_face_num 最多处理人脸数目,默认值1
* @param face_fields 包括age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities信息,逗号分隔,默认只返回人脸框、概率和旋转角度。
* @return
* @throws Exception
*/
public static String faceDetect(String filePath,String max_face_num,String accessToken) throws Exception {
byte[] imgData = FileUtil.readFileByBytes(filePath);
String imgStr = Base64Util.encode(imgData);
//image_type为FACE_TOKEN image参数为faceToken 每个图片在第一次使用URL|BASE64返回faceToken才能使用FACE_TOKEN 进行检测哦
String faceToken = "f7ec8ecd441886371b9749d1fc853f88";
//image_type为URL image参数为url
String url = "https://www.xxxx.com/face.jpg";
/**
* image_type 新加参数 与image结合使用 URL BASE64 FACE_TOKEN
*/
String params = "image=" + URLEncoder.encode(imgStr,"UTF-8")+"&image_type=BASE64&max_face_num="+max_face_num+"&face_field=age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities";
String params = "image=" + url+"&image_type=URL&max_face_num="+max_face_num+"&face_field=age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities";
String params = "image=" + faceToken+"&image_type=FACE_TOKEN&max_face_num="+max_face_num+"&face_field=age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities";
String result = HttpUtil.post(FACE_DETECT_URL, accessToken, params);
return result;
}
}
返回的结果(数据很长,具体根据自己需求选择返回的字段哦)
{"error_code":0,"error_msg":"SUCCESS","log_id":4278514612,"timestamp":1525658118,"cached":0,"result":{"face_num":1,"face_list":[{"face_token":"f7ec8ecd441886371b9749d1fc853f44","location":{"left":139.6802826,"top":143.2796936,"width":96,"height":96,"rotation":-2},"face_probability":1,"angle":{"yaw":3.577158689,"pitch":4.245626926,"roll":-2.851191044},"age":23,"beauty":44.79724503,"expression":{"type":"none","probability":0.9998862743},"face_shape":{"type":"heart","probability":0.4918475449},"gender":{"type":"male","probability":0.9999206066},"glasses":{"type":"common","probability":0.9997472167},"landmark":[{"x":160.8643188,"y":160.2290955},{"x":206.6844482,"y":157.5333099},{"x":184.0262146,"y":186.3094788},{"x":186.1922455,"y":207.6744843}],"landmark72":[{"x":140.3707123,"y":165.2830811},{"x":142.4906921,"y":179.2499084},{"x":146.4187927,"y":193.0246887},{"x":152.4317932,"y":206.5871277},{"x":162.0330505,"y":219.8708801},{"x":174.5300751,"y":232.4580994},{"x":187.8030701,"y":237.3661804},{"x":203.6371155,"y":233.1720123},{"x":218.1924744,"y":220.01474},{"x":228.0222473,"y":204.4451294},{"x":233.0258484,"y":189.1201172},{"x":235.7172241,"y":173.7797699},{"x":236.9867096,"y":158.4904785},{"x":151.0836029,"y":161.3935242},{"x":155.3612976,"y":158.2462311},{"x":160.1599731,"y":157.4094849},{"x":165.3396301,"y":158.1388092},{"x":170.085022,"y":161.4363403},{"x":165.4792633,"y":162.7316284},{"x":160.3265533,"y":163.4096069},{"x":155.3059082,"y":162.972229},{"x":160.8643188,"y":160.2290955},{"x":143.5744629,"y":150.026825},{"x":149.0124207,"y":143.9742126},{"x":156.2673035,"y":142.4179993},{"x":163.8598328,"y":143.3335419},{"x":171.1524658,"y":148.2867126},{"x":163.7554932,"y":148.786377},{"x":156.3734741,"y":148.7999878},{"x":149.5826874,"y":149.2512207},{"x":197.3506165,"y":160.0897522},{"x":201.7075806,"y":156.0402374},{"x":206.882019,"y":154.7565613},{"x":212.4109039,"y":155.4908905},{"x":217.4255066,"y":158.050705},{"x":212.8363953,"y":160.0161438},{"x":207.59375,"y":161.0450134},{"x":202.1746979,"y":160.9025879},{"x":206.6844482,"y":157.5333099},{"x":192.5471039,"y":146.4320526},{"x":199.972229,"y":140.9735718},{"x":208.2919312,"y":139.4329834},{"x":216.6661072,"y":140.0569458},{"x":224.2270508,"y":146.2198792},{"x":216.5629272,"y":145.7130585},{"x":208.586853,"y":145.9061127},{"x":200.8113098,"y":146.4360809},{"x":176.9011993,"y":161.6260986},{"x":175.7974243,"y":171.3020325},{"x":174.5703735,"y":181.0285187},{"x":171.8184509,"y":190.9635773},{"x":178.4221954,"y":191.6383514},{"x":191.3653564,"y":191.236557},{"x":197.2584534,"y":189.7222748},{"x":193.6408081,"y":180.2554321},{"x":191.5493011,"y":170.5995941},{"x":189.3864136,"y":161.21875},{"x":184.0262146,"y":186.3094788},{"x":170.5522614,"y":206.9529419},{"x":177.6235657,"y":202.6881104},{"x":185.7793732,"y":201.6790771},{"x":194.9309082,"y":201.787796},{"x":202.8224792,"y":206.0887756},{"x":196.1691895,"y":213.4197388},{"x":185.6742249,"y":216.7504425},{"x":176.4473877,"y":213.9425812},{"x":178.0608063,"y":205.8628998},{"x":185.9603424,"y":205.4321289},{"x":194.4753265,"y":204.8729248},{"x":194.4585571,"y":208.7193909},{"x":185.6791992,"y":210.0457001},{"x":178.0577393,"y":209.1998596}],"race":{"type":"yellow","probability":0.9999475479}}]}}
人脸数量:1
美丑打分:44.79724503
年龄:23
小帅丶已经封装好的JavaBean对象
/**
* Copyright 2018 xsshome.cn
*/
package com.xs.pojo.face;
import java.util.List;
/**
* Auto-generated: 2018-05-07 9:39:5
* V3版本人脸检测Javabean
* @author 小帅丶
*/
public class FaceV3DetectBean {
private int error_code;
private String error_msg;
private long log_id;
private long timestamp;
private int cached;
private Result result;
public void setError_code(int error_code) {
this.error_code = error_code;
}
public int getError_code() {
return error_code;
}
public void setError_msg(String error_msg) {
this.error_msg = error_msg;
}
public String getError_msg() {
return error_msg;
}
public void setLog_id(long log_id) {
this.log_id = log_id;
}
public long getLog_id() {
return log_id;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public long getTimestamp() {
return timestamp;
}
public void setCached(int cached) {
this.cached = cached;
}
public int getCached() {
return cached;
}
public void setResult(Result result) {
this.result = result;
}
public Result getResult() {
return result;
}
/**
* Result
* @author 小帅丶
*
*/
public static class Result {
private int face_num;
private List face_list;
public void setFace_num(int face_num) {
this.face_num = face_num;
}
public int getFace_num() {
return face_num;
}
public void setFace_list(List face_list) {
this.face_list = face_list;
}
public List getFace_list() {
return face_list;
}
}
/**
* Face_list
* @author 小帅丶
*
*/
public static class Face_list {
private String face_token;
private Location location;
private int face_probability;
private Angle angle;
private int age;
private double beauty;
private Expression expression;
private Face_shape face_shape;
private Gender gender;
private Glasses glasses;
private List landmark;
private List landmark72;
private Race race;
public void setFace_token(String face_token) {
this.face_token = face_token;
}
public String getFace_token() {
return face_token;
}
public void setLocation(Location location) {
this.location = location;
}
public Location getLocation() {
return location;
}
public void setFace_probability(int face_probability) {
this.face_probability = face_probability;
}
public int getFace_probability() {
return face_probability;
}
public void setAngle(Angle angle) {
this.angle = angle;
}
public Angle getAngle() {
return angle;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setBeauty(double beauty) {
this.beauty = beauty;
}
public double getBeauty() {
return beauty;
}
public void setExpression(Expression expression) {
this.expression = expression;
}
public Expression getExpression() {
return expression;
}
public void setFace_shape(Face_shape face_shape) {
this.face_shape = face_shape;
}
public Face_shape getFace_shape() {
return face_shape;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public Gender getGender() {
return gender;
}
public void setGlasses(Glasses glasses) {
this.glasses = glasses;
}
public Glasses getGlasses() {
return glasses;
}
public void setLandmark(List landmark) {
this.landmark = landmark;
}
public List getLandmark() {
return landmark;
}
public void setLandmark72(List landmark72) {
this.landmark72 = landmark72;
}
public List getLandmark72() {
return landmark72;
}
public void setRace(Race race) {
this.race = race;
}
public Race getRace() {
return race;
}
}
/**
* Race
* @author 小帅丶
*
*/
public static class Race {
private String type;
private double probability;
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setProbability(double probability) {
this.probability = probability;
}
public double getProbability() {
return probability;
}
}
/**
* Landmark72
* @author 小帅丶
*
*/
public static class Landmark72 {
private double x;
private double y;
public void setX(double x) {
this.x = x;
}
public double getX() {
return x;
}
public void setY(double y) {
this.y = y;
}
public double getY() {
return y;
}
}
/**
* Landmark
* @author 小帅丶
*
*/
public static class Landmark {
private double x;
private double y;
public void setX(double x) {
this.x = x;
}
public double getX() {
return x;
}
public void setY(double y) {
this.y = y;
}
public double getY() {
return y;
}
}
/**
* Glasses
* @author 小帅丶
*
*/
public static class Glasses {
private String type;
private double probability;
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setProbability(double probability) {
this.probability = probability;
}
public double getProbability() {
return probability;
}
}
/**
* Gender
* @author 小帅丶
*
*/
public static class Gender {
private String type;
private double probability;
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setProbability(double probability) {
this.probability = probability;
}
public double getProbability() {
return probability;
}
}
/**
* Face_shape
* @author 小帅丶
*
*/
public static class Face_shape {
private String type;
private double probability;
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setProbability(double probability) {
this.probability = probability;
}
public double getProbability() {
return probability;
}
}
/**
* Expression
* @author 小帅丶
*
*/
public static class Expression {
private String type;
private double probability;
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setProbability(double probability) {
this.probability = probability;
}
public double getProbability() {
return probability;
}
}
/**
* Angle
* @author 小帅丶
*
*/
public static class Angle {
private double yaw;
private double pitch;
private double roll;
public void setYaw(double yaw) {
this.yaw = yaw;
}
public double getYaw() {
return yaw;
}
public void setPitch(double pitch) {
this.pitch = pitch;
}
public double getPitch() {
return pitch;
}
public void setRoll(double roll) {
this.roll = roll;
}
public double getRoll() {
return roll;
}
}
/**
* Location
* @author 小帅丶
*
*/
public static class Location {
private double left;
private double top;
private int width;
private int height;
private int rotation;
public void setLeft(double left) {
this.left = left;
}
public double getLeft() {
return left;
}
public void setTop(double top) {
this.top = top;
}
public double getTop() {
return top;
}
public void setWidth(int width) {
this.width = width;
}
public int getWidth() {
return width;
}
public void setHeight(int height) {
this.height = height;
}
public int getHeight() {
return height;
}
public void setRotation(int rotation) {
this.rotation = rotation;
}
public int getRotation() {
return rotation;
}
}
}
------------------------------------------分割线代码结束------------------------------------------
有可能遇到的错误信息对应表