当前位置:   article > 正文

Java spring boot 阿里云调用人脸识别接口,本地sdk上传到阿里云调用api_java 调用阿里巴巴initfaceverify接口调用示例

java 调用阿里巴巴initfaceverify接口调用示例

Java spring boot 阿里云调用人脸识别接口

没有写测试类,工具类如下,有access_key_id和access_key_secret传参调用就可使用

代码如下:
pom.xml依赖

<!--人脸识别阿里云-->
		<dependency>
			<groupId>com.aliyun</groupId>
			<artifactId>aliyun-java-sdk-facebody</artifactId>
			<version>1.2.2</version>
		</dependency>
		<dependency>
			<groupId>com.aliyun</groupId>
			<artifactId>viapi-utils</artifactId>
			<version>1.0.0</version>
		</dependency>
		<dependency>
			<groupId>com.aliyun</groupId>
			<artifactId>aliyun-java-sdk-viapiutils</artifactId>
			<version>1.0.0</version>
		</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

FaceRecognitionUtil类


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.facebody.model.v20191230.*;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.io.File;
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;
import java.text.SimpleDateFormat;

import sun.misc.BASE64Decoder;  
import sun.misc.BASE64Encoder; 


public class FaceRecognitionUtil {

	
    //DefaultProfile.getProfile的参数分别是地域,access_key_id, access_key_secret
    public static DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "xxxxxxxxxxxxxxxxxxx", "ooooooooooooooooooooooooooooo");
    public static  IAcsClient client = new DefaultAcsClient(profile);

    // 人脸对比
    /**
     * VerifyFace API 人脸比对
     *
     * @param imageUrl_login 对比人脸图片1
     * @param imageUrl_register 对比人脸图片2
     */
    public static String VerifyFace(String imageUrl_login, String imageUrl_register) {

        CompareFaceRequest request = new CompareFaceRequest();
        request.setRegionId("cn-shanghai");
        request.setImageURLA(imageUrl_login);
        request.setImageURLB(imageUrl_register);

        try {
            CompareFaceResponse response = client.getAcsResponse(request);
            System.out.println("人脸对比:"+new Gson().toJson(response));
            
            JSONObject jsonObject = JSONObject.parseObject(new Gson().toJson(response));
            jsonObject=JSONObject.parseObject((jsonObject.get("data").toString()));
            String confidence = jsonObject.get("confidence").toString();
            Float confidenceFloat = Float.parseFloat(confidence);
            
			if (confidenceFloat >= 0.61) {
				return "Yes";
			} else {
				return "No";
			}
            
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }


        return null;
    }
    /**
     * RecognizeFace API 人脸检测
     *
     * @param imageUrl 人脸图片
     */
    public static String RecognizeFace(String imageUrl) {

    	RecognizeFaceRequest request = new RecognizeFaceRequest();
        request.setRegionId("cn-shanghai");
        request.setImageURL(imageUrl);

        try {
            RecognizeFaceResponse response = client.getAcsResponse(request);
            System.out.println("人脸检测:"+new Gson().toJson(response));
            
            JSONObject jsonObject = JSONObject.parseObject(new Gson().toJson(response));
            jsonObject=JSONObject.parseObject((jsonObject.get("data").toString()));
            String faceCounts = jsonObject.get("faceCount").toString();
            if (Integer.parseInt(faceCounts)==0) {
            	return "No";
			}
            String likeDoubles = jsonObject.get("faceProbabilityList").toString();
            likeDoubles = likeDoubles.substring(1,likeDoubles.length() - 1);
            if ("".equals(likeDoubles)) {
            	return "No";
			}
            String[] likeList = likeDoubles.split(",");
            if (likeList.length>0) {
            	String num_str=likeList[0].toString();
            	Double num_double=Double.parseDouble(num_str);
				if (num_double>0.6) {
					return "Yes";
				}else {
					return "No";
				}
			}
            return "No";
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

        return null;
    }
    /**
     * DetectLivingFace API 人脸活体识别
     *
     * @param imageUrl_login  人脸活体识别
     */
    public static String DetectLivingFace(String imageUrl_login) {

        DetectLivingFaceRequest request = new DetectLivingFaceRequest();
        request.setRegionId("cn-shanghai");

        List<DetectLivingFaceRequest.Tasks> tasksList = new ArrayList<DetectLivingFaceRequest.Tasks>();

        DetectLivingFaceRequest.Tasks tasks1 = new DetectLivingFaceRequest.Tasks();
        tasks1.setImageURL(imageUrl_login);
        tasksList.add(tasks1);
        request.setTaskss(tasksList);

        try {
            DetectLivingFaceResponse response = client.getAcsResponse(request);
            System.out.println("人脸活体识别:"+new Gson().toJson(response));
            String str = new Gson().toJson(response).toString();
            if (str!=null){
                // 此处引入的是 com.alibaba.fastjson.JSONObject; 对象
                JSONObject jsonObject = JSONObject.parseObject(str);
                jsonObject=JSONObject.parseObject((jsonObject.get("data").toString()));
                //result_pass为-1时,人脸活体识别不通过pass
                int result_pass = jsonObject.toString().indexOf("pass");
                int result_block = jsonObject.toString().indexOf("block");
                //判断是否通过活体
                if (result_pass != -1 && result_block == -1){
                    return "Yes";
                }
                return  "No";
            }
            

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return  null;
    }
    

	// 本地上传sdk无需网络url
	public static void testUploadFile() throws ClientException, IOException {
		String accessKey = "xxxxxxxxxxxxxxxxx";
		String accessKeySecret = "ooooooooooooooo";
		String file1 = "D:\\2020-11-06\\timg.jpg"; // 或者本地上传
		String file2 = "D:\\2020-11-06\\0.jpg"; // 或者本地上传
		// String file =
		// "https://fuss10.elemecdn.com/5/32/c17416d77817f2507d7fbdf15ef22jpeg.jpeg";
		FileUtils fileUtils = FileUtils.getInstance(accessKey, accessKeySecret);
		String ossurl1 = fileUtils.upload(file1);
		String ossurl2 = fileUtils.upload(file2);
		System.out.println(ossurl2);
		//人脸属性检测
		RecognizeFace(ossurl2);
		//GetFaceAttribute(ossurl2);
		//人脸活体检测
		DetectLivingFace(ossurl2);
		//人脸对比
		VerifyFace(ossurl1, ossurl2);
	}
}

  • 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
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191

人脸识别核心代码如上。

人脸识别项目运行结果如下

执行结果:
在这里插入图片描述

控制台打印
上边代码是我根据阿里云返回的数据自己写的判断,各位同学可以根据自己需求写判断。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/938201
推荐阅读
相关标签
  

闽ICP备14008679号