全部產品
Search
文件中心

Content Moderation:刪除個體

更新時間:Jul 06, 2024

本文介紹了如何使用Java SDK刪除個體資訊。

功能描述

刪除個體時,個體對應的圖片以及資訊均會被刪除。關於參數的詳細說明,請參見刪除個體API文檔

您需要使用Alibaba Content Security Service的API接入地址,調用本SDK介面。關於API接入地址的資訊,請參見接入地址(Endpoint)

前提條件

  • 安裝Java依賴。關於安裝Java依賴的具體操作,請參見安裝Java依賴

    說明

    請一定按照安裝Java依賴頁面中的版本安裝,否則會導致調用失敗。

  • 如果使用本地檔案或者二進位檔案檢測,請下載並在專案工程中引入Extension.Uploader工具類

刪除個體程式碼範例

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

/**
 * 刪除個體。
 *
 * @author 
 * @date 2018/06/25
 */
public class FaceDeletePersonRequestSample  {

    public static void main(String[] args) throws Exception {
        /**
         * 阿里雲帳號AccessKey擁有所有API的存取權限,建議您使用RAM使用者進行API訪問或日常營運。 
         * 常見擷取環境變數方式:
         * 方式一:
         *     擷取RAM使用者AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     擷取RAM使用者AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * 方式二:
         *     擷取RAM使用者AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     擷取RAM使用者AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                "建議從環境變數中擷取RAM使用者AccessKey ID",
                "建議從環境變數中擷取RAM使用者AccessKey Secret");
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        // 注意:此處執行個體化的client儘可能重複使用,提升檢測效能。避免重複建立串連。
        IAcsClient client = new DefaultAcsClient(profile);

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

        JSONObject data = new JSONObject();
        /**
         * personId: 使用者自訂個體ID,必填。
         */
        data.put("personId", "personId_test_1");

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

        /**
         * 請務必設定逾時時間。
         */
        request.setConnectTimeout(3000);
        request.setReadTimeout(6000);

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

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