java 人脸比对_【人脸对比】V3版不会看这里(JavaAPI)

直接开始代码。仅供参考如有问题评论区说明,前提自己已经看过文档

------------------------------------------------------------------------------------

需要的jar

com.alibaba

fastjson

1.2.47

工具类下载

* 重要提示代码中所需工具类

* FileUtil,Base64Util,HttpUtil,GsonUtils请从

* https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72

* https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2

* https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3

* https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3

* 下载

------------------------------------------------------------------------------------

使用的示例图片 来源网络

3a6a6ce2519724ac34c9a375a21c3572.png

------------------------------------------分割线代码开始------------------------------------------

JavaAPI示例代码

比对两张图片中人脸的相似度,并返回相似度分值

import java.util.ArrayList;

import java.util.List;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

/**

* 人脸对比V3版本示例代码-JavaAPI

* @author 小帅、

*

*/

public class FaceV3MatchSample {

private static final String FACE_MATCH = "https://aip.baidubce.com/rest/2.0/face/v3/match";

public static void main(String[] args) {

System.out.println(FaceMatch("/Users/xiaoshuai/Downloads/face.jpeg", "/Users/xiaoshuai/Downloads/face.jpeg", "自己应用获取的AccessToken"));

FaceV3MatchBean faceV3MatchBean = JSONObject.toJavaObject(JSON.parseObject(result), FaceV3MatchBean.class);

System.out.println("对比分数值:"+faceV3MatchBean.getResult().getScore());

}

/**

* 人脸对比示例代码

* @param path1 图片本地路径1

* @param path2 图片本地路径1

* @param token AccessToken

* @return

*/

public static String FaceMatch(String path1,String path2,String token) {

try {

// 本地文件路径

String filePath1 = path1;

String filePath2 = path2;

byte[] imgData1 = FileUtil.readFileByBytes(filePath1);

byte[] imgData2 = FileUtil.readFileByBytes(filePath2);

String imgStr1 = Base64Util.encode(imgData1);

String imgStr2 = Base64Util.encode(imgData2);

List faceMatchs = new ArrayList();

FaceV3Bean faceMatch1 = new FaceV3Bean(imgStr1,"BASE64");

FaceV3Bean faceMatch2 = new FaceV3Bean(imgStr2,"BASE64");

faceMatchs.add(faceMatch1);

faceMatchs.add(faceMatch2);

String param = JSONObject.toJSONString(faceMatchs);

System.out.println("======"+param);

// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。

String accessToken = token;

String result = HttpUtil.post(FACE_MATCH, accessToken, param);

return result;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

}

接口返回数据

可以明显看出同一张图片分数为100

{

"error_code": 0,

"error_msg": "SUCCESS",

"log_id": 2272406243,

"timestamp": 1525788381,

"cached": 0,

"result": {

"score": 100,

"face_list": [

{

"face_token": "c739e29fc9b2bf58a18b63ae3868b237"

},

{

"face_token": "c739e29fc9b2bf58a18b63ae3868b237"

}

]

}

}

-------------------

对比分数值:100

所需Java对象(作者自己封装并非官方提供的哦)

import org.json.JSONObject;

public class FaceV3Bean {

private String image;

private String image_type;

private String face_type;

private String quality_control;

private String liveness_control;

public FaceV3Bean() {

}

public FaceV3Bean(String image, String image_type) {

super();

this.image = image;

this.image_type = image_type;

}

public FaceV3Bean(String image, String image_type, String face_type,

String quality_control, String liveness_control) {

super();

this.image = image;

this.image_type = image_type;

this.face_type = face_type;

this.quality_control = quality_control;

this.liveness_control = liveness_control;

}

public String getImage() {

return image;

}

public void setImage(String image) {

this.image = image;

}

public String getImage_type() {

return image_type;

}

public void setImage_type(String image_type) {

this.image_type = image_type;

}

public String getFace_type() {

return face_type;

}

public void setFace_type(String face_type) {

this.face_type = face_type;

}

public String getQuality_control() {

return quality_control;

}

public void setQuality_control(String quality_control) {

this.quality_control = quality_control;

}

public String getLiveness_control() {

return liveness_control;

}

public void setLiveness_control(String liveness_control) {

this.liveness_control = liveness_control;

}

}

对比返回的JSON转JavaBean对象

import java.util.List;

/**

* 人脸V3对比Bean

* @author 小帅丶

*/

public class FaceV3MatchBean {

private int error_code;

private String error_msg;

private long log_id;

private long timestamp;

private int cached;

private Result result;

public int getError_code() {

return error_code;

}

public void setError_code(int error_code) {

this.error_code = error_code;

}

public String getError_msg() {

return error_msg;

}

public void setError_msg(String error_msg) {

this.error_msg = error_msg;

}

public long getLog_id() {

return log_id;

}

public void setLog_id(long log_id) {

this.log_id = log_id;

}

public long getTimestamp() {

return timestamp;

}

public void setTimestamp(long timestamp) {

this.timestamp = timestamp;

}

public int getCached() {

return cached;

}

public void setCached(int cached) {

this.cached = cached;

}

public Result getResult() {

return result;

}

public void setResult(Result result) {

this.result = result;

}

public static class Result{

private double score;

private List face_list;

public double getScore() {

return score;

}

public void setScore(double score) {

this.score = score;

}

public List getFace_list() {

return face_list;

}

public void setFace_list(List face_list) {

this.face_list = face_list;

}

}

/**

* @author 小帅丶

*

*/

public static class Face_list{

private String face_token;

public String getFace_token() {

return face_token;

}

public void setFace_token(String face_token) {

this.face_token = face_token;

}

}

}

------------------------------------------分割线代码结束------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值