Java版本阿里云【人脸人体】之【人脸对比】接口对接

**

阿里云【人脸人体】之【人脸对比】接口对接

**

第一步:导入相关依赖包


    com.aliyun
     aliyun-java-sdk-facebody
     1.0.8
 

第二步:工具包

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.aliyuncs.facebody.model.v20191230.*;

/**
 * 阿里云人体人脸中的<人脸识别>
 * PS:只有上海区域的OSS才能使用
 */
public class CompareFace {

    private static final String ak_id = "********替换成自己的AccessKeyID*********";
    private static final String ak_secret = "*******替换成自己的AccessKeySecret**********";

    /**
     * 图片要求:
     * 请求格式:JPEG、JPG、PNG、BMP。
     * 图像大小:图像大小不超过3M。
     * 图像分辨率:图片大小要求5x5像素以上,人脸的尺寸建议大于64x64像素。
     * @param imageURLA  图片A
     * @param imageURLB  图片B
     * @return
     */
    public static boolean faceContrast(String imageURLA,String imageURLB){
        boolean flag = false;
        
		//目前区域只开通了华东2(上海)
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", ak_id, ak_secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CompareFaceRequest request = new CompareFaceRequest();
        request.setRegionId("cn-shanghai");
        request.setImageURLA(imageURLA);
        request.setImageURLB(imageURLB);
        try {
            CompareFaceResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            Float confidence = response.getData().getConfidence();
            if (confidence > 61){
                flag = true;
            }
        } 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 flag;
    }

    public static void main(String[] args) {
        String imageURLA = "https://chaojiguoke.oss-cn-shanghai.aliyuncs.com/img/894883263a674eceabe1f147610579fb.jpg";
        String imageURLB = "https://chaojiguoke.oss-cn-shanghai.aliyuncs.com/img/17d824985ca048a5835b94b306ffb8a0.jpg";
        boolean b = faceContrast(imageURLA, imageURLB);
        if (b){
            System.out.println("通过:超过61%");
        }else{
            System.out.println("失败:低于61%");
        }
    }
}

第三步:接口返回数据格式

1.xml格式

3A4092F5-C708-4258-953B-4B3FCEF3F8FB

    61
    69
    75
    26.42851448059082
    106
    61
    86
    112
    771
    118
    222
    303

2.JSON格式

{
  "RequestId": "3A4092F5-C708-4258-953B-4B3FCEF3F8FB",
  "Data": {
    "Thresholds": [
      61.0,
      69.0,
      75.0
    ],
    "Confidence": 26.42851448059082,
    "RectAList": [
      106,
      61,
      86,
      112
    ],
    "RectBList": [
      771,
      118,
      222,
      303
    ]
  }
}

详细资料:
接口文档: https://help.aliyun.com/document_detail/151891.html
常见错误码: https://help.aliyun.com/document_detail/146756.html?spm=a2c4g.11186623.0.0.66304ac8n0p2fi

你可能感兴趣的:(Java)