本文介绍了如何使用Java SDK新增个体的人脸图片。

功能描述

为个体新增一组人脸图片,一个个体最多允许包含20张人脸图片。关于参数的说明,请参见新增人脸API文档

您需要使用内容安全的API接入地址,调用本SDK接口。关于API接入地址的信息,请参见接入地址(Endpoint)

前提条件

  • 安装Java依赖。关于安装Java依赖的具体操作,请参见安装Java依赖
    说明 请一定按照安装Java依赖页面中的版本安装,否则会导致调用失败。
  • 如果使用本地文件或者二进制文件检测,请下载并在项目工程中引入Extension.Uploader工具类

新增人脸图片(URL)代码示例

import java.util.Arrays;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.AddFacesRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * 增加人脸信息,会返回增加成功的人脸ID。
 *
 * @author 
 * @date 2018/06/25
 */
public class FaceAddFaceRequestSample  {

    public static void main(String[] args) throws Exception {
        //请替换成您的AccessKey ID、AccessKey Secret。
        IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "您的AccessKey ID", "您的AccessKey Secret");
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        IAcsClient client = new DefaultAcsClient(profile);


        AddFacesRequest addFacesRequest = new AddFacesRequest();
        addFacesRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
        addFacesRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
        addFacesRequest.setEncoding("utf-8");

        JSONObject data = new JSONObject();
        /**
         * personId: 用户自定义个体ID,必填。
         * urls: 图片URL。
         */
        data.put("personId", "personId_test_3");
        data.put("urls", Arrays.asList("https://img.alicdn.com/tfs/xxx-550-407.jpg"));

        addFacesRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        /**
         * 请务必设置超时时间。
         */
        addFacesRequest.setConnectTimeout(3000);
        addFacesRequest.setReadTimeout(6000);

        try {
            HttpResponse httpResponse = client.doAction(addFacesRequest);

            if (httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                System.out.println(JSON.toJSONString(scrResponse, true));
                if (200 == scrResponse.getInteger("code")) {
                    JSONObject resultObject = scrResponse.getJSONObject("data");
                    if (200 == resultObject.getInteger("code")) {
                        System.out.println(resultObject.getString("personId"));
                    } else {
                        System.out.println("task process fail:" + resultObject.getInteger("code"));
                    }
                } else {
                    System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
                }
            } else {
                System.out.println("response not success. status:" + httpResponse.getStatus());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

新增人脸图片(本地文件)代码示例

import java.util.Arrays;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.green.extension.uploader.ClientUploader;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.AddFacesRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * 增加人脸信息,会返回增加成功的人脸ID。
 *
 * @author
 * @date 2018/06/25
 */
public class FaceAddFaceRequestSample  {

    public static void main(String[] args) throws Exception {
        // 请替换成您的AccessKey ID、AccessKey Secret。
        IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "您的AccessKey ID", "您的AccessKey Secret");
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        IAcsClient client = new DefaultAcsClient(profile);


        AddFacesRequest addFacesRequest = new AddFacesRequest();
        addFacesRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
        addFacesRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
        addFacesRequest.setEncoding("utf-8");

        JSONObject data = new JSONObject();
        String url = null;
        ClientUploader clientUploader = ClientUploader.getImageClientUploader(profile, false);
        try{
            url = clientUploader.uploadFile("d:/test.jpg");
        }catch (Exception e){
            e.printStackTrace();
        }
        /**
         * personId: 用户自定义个体ID,必填。
         * urls: 图片URL
         */
        data.put("personId", "personId_test_3");
        data.put("urls", Arrays.asList(url));

        addFacesRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        /**
         * 请务必设置超时时间。
         */
        addFacesRequest.setConnectTimeout(3000);
        addFacesRequest.setReadTimeout(6000);

        try {
            HttpResponse httpResponse = client.doAction(addFacesRequest);

            if (httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                System.out.println(JSON.toJSONString(scrResponse, true));
                if (200 == scrResponse.getInteger("code")) {
                    JSONObject resultObject = scrResponse.getJSONObject("data");
                    if (200 == resultObject.getInteger("code")) {
                        System.out.println(resultObject.getString("personId"));
                    } else {
                        System.out.println("task process fail:" + resultObject.getInteger("code"));
                    }
                } else {
                    System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
                }
            } else {
                System.out.println("response not success. status:" + httpResponse.getStatus());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}