Pu Zhibing
2024-12-13 73b750200f25df08aa64124da49e7461f9de6653
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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...
    }
}