All Products
Search
Document Center

Content Moderation:Set person information

Last Updated:Aug 19, 2024

This topic describes how to use the Java SDK to set person information.

Usage notes

The purpose of setting person information is to add remarks to the corresponding person. Setting person information is an optional step. After you setting information about a person, the information is included in the returned results of custom retrieval. For more information about the parameters, see API operation for setting person information.

You need to use the endpoints of the Content Moderation API to call this SDK. For more information about the endpoints, see Endpoints.

Prerequisites

  • Java dependencies are installed. For more information, see Installation.

    Note

    You must use the Java version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

  • The Extension.Uploader utility class is downloaded and imported into your project if you submit a local image or a binary image stream for image moderation.

Sample code for setting person information

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.SetPersonRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * Set person information. 
 * @author 
 * @date 2018/06/25
 */
public class FaceSetPersonRequestSample {

    public static void main(String[] args) throws Exception {
        /**
         * The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M.  
         * Common ways to obtain environment variables:
         * Method 1:
         *     Obtain the AccessKey ID of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey secret of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * Method 2:
         *     Obtain the AccessKey ID of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey secret of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                "We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
                "We recommend that you obtain the AccessKey secret of your RAM user from environment variables");
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
        IAcsClient client = new DefaultAcsClient(profile);

        SetPersonRequest setPersonRequest = new SetPersonRequest();
        setPersonRequest.setAcceptFormat(FormatType.JSON); // Specify the API response format. 
        setPersonRequest.setMethod(com.aliyuncs.http.MethodType.POST); // Specify the request method. 
        setPersonRequest.setEncoding("utf-8");

        JSONObject data = new JSONObject();
        /**
         * personId: the ID of the custom person. This parameter is required. 
         * name: the name of the custom person. This parameter is optional. 
         * note: the remarks. This parameter is optional. 
         */
        data.put("personId", "personId_test_3");
        data.put("name", "Test");
        data.put("note", "Remarks");


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

        /**
         * Specify the connection timeout period and read timeout period. 
         */
        setPersonRequest.setConnectTimeout(3000);
        setPersonRequest.setReadTimeout(6000);

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

            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();
        }
    }
}