package com.stylefeng.guns.modular.system.util;/*
|
引入依赖包
|
最低SDK版本要求:facebody20191230的SDK版本需大于等于3.0.7。
|
可以在此仓库地址中引用最新版本SDK:https://mvnrepository.com/artifact/com.aliyun/facebody20191230
|
<!-- https://mvnrepository.com/artifact/com.aliyun/facebody20191230 -->
|
<dependency>
|
<groupId>com.aliyun</groupId>
|
<artifactId>facebody20191230</artifactId>
|
<version>${aliyun.facebody.version}</version>
|
</dependency>
|
*/
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.annotation.JSONField;
|
import com.aliyun.facebody20191230.models.CompareFaceResponse;
|
import com.aliyun.tea.TeaException;
|
import com.aliyun.tea.TeaModel;
|
import com.aliyun.teautil.Common;
|
import lombok.Data;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.InputStream;
|
import java.net.URL;
|
import java.util.List;
|
|
public class CompareFace {
|
public static com.aliyun.facebody20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
|
/*
|
初始化配置对象com.aliyun.teaopenapi.models.Config
|
Config对象存放 AccessKeyId、AccessKeySecret、endpoint等配置
|
*/
|
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
.setAccessKeyId(accessKeyId)
|
.setAccessKeySecret(accessKeySecret);
|
// 访问的域名
|
config.endpoint = "facebody.cn-shanghai.aliyuncs.com";
|
return new com.aliyun.facebody20191230.Client(config);
|
}
|
|
|
|
|
public static boolean faceCompare(String pic1,String pic2) throws Exception {
|
// 创建AccessKey ID和AccessKey Secret,请参考https://help.aliyun.com/document_detail/175144.html
|
// 如果您使用的是RAM用户的AccessKey,还需要为子账号授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html
|
// 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
|
String accessKeyId = "LTAI5tR4whv88Y5CUucCJEu6";
|
String accessKeySecret = "2fObO6LE6U2OzrUfXw9YBlQWHohFvg";
|
com.aliyun.facebody20191230.Client client = CompareFace.createClient(accessKeyId, accessKeySecret);
|
// 场景一,使用本地文件
|
URL url1 = new URL(pic1);
|
InputStream inputStreamA = url1.openConnection().getInputStream();
|
// 场景二,使用任意可访问的url
|
URL url = new URL(pic2);
|
InputStream inputStreamB = url.openConnection().getInputStream();
|
com.aliyun.facebody20191230.models.CompareFaceAdvanceRequest compareFaceAdvanceRequest = new com.aliyun.facebody20191230.models.CompareFaceAdvanceRequest()
|
.setImageURLAObject(inputStreamA)
|
.setImageURLBObject(inputStreamB);
|
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
try {
|
// 复制代码运行请自行打印 API 的返回值
|
CompareFaceResponse compareFaceResponse = client.compareFaceAdvance(compareFaceAdvanceRequest, runtime);
|
|
// 获取整体结果
|
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(compareFaceResponse)));
|
|
String jsonString = Common.toJSONString(TeaModel.buildMap(compareFaceResponse));
|
// 解析整个JSON
|
JSONObject jsonObject = JSON.parseObject(jsonString);
|
|
// 提取Body部分
|
JSONObject bodyJson = jsonObject.getJSONObject("body");
|
|
// 提取Data部分
|
Data data = bodyJson.getObject("Data", Data.class);
|
double confidence = data.getConfidence();
|
if (confidence>61){
|
return true;
|
}else {
|
return false;
|
}
|
} catch (TeaException teaException) {
|
// 获取整体报错信息
|
System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
|
// 获取单个字段
|
System.out.println(teaException.getCode());
|
}
|
return false;
|
}
|
|
// 定义对应的Java Bean类
|
public static class Headers {
|
// 在这里定义Headers的属性和getter/setter方法
|
}
|
|
public static class Body {
|
@JSONField(name = "RequestId")
|
private String requestId;
|
private Data data;
|
// getters and setters...
|
}
|
@lombok.Data
|
public static class Data {
|
private double QualityScoreA;
|
private double QualityScoreB;
|
private int IsMaskB;
|
private int IsMaskA;
|
@JSONField(name = "Thresholds")
|
private double[] thresholds;
|
private double Confidence;
|
private List<Integer> LandmarksAList;
|
private List<Integer> LandmarksBList;
|
private List<Integer> RectAList;
|
private List<Integer> RectBList;
|
// getters and setters...
|
}
|
}
|