Remove similar image samples

更新时间:
复制 MD 格式

This topic describes how to use the Java SDK to remove similar image samples from an image library.

Feature description

The removal operation takes effect within 1 minute. You can remove up to 100 sample images at a time. For more information about the parameters, see the Delete sample images API documentation.

You must use the Content Moderation endpoint to call the service using the SDK. For more information about API 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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.green.model.v20180509.DeleteSimilarityImageRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        /**
         * An Alibaba Cloud account AccessKey has permissions to call all API operations. 
         * Use a Resource Access Management (RAM) user to call API operations or perform routine O&M. 
         * Common methods to obtain environment variables:
         * Method 1:
         *     Obtain the AccessKey ID of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey secret of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * Method 2:
         *     Obtain the AccessKey ID of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey secret of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                "Obtain the AccessKey ID of the RAM user from an environment variable.",
                "Obtain the AccessKey secret of the RAM user from an environment variable.");
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        IAcsClient client = new DefaultAcsClient(profile);

        DeleteSimilarityImageRequest deleteSimilarityImageRequest = new DeleteSimilarityImageRequest();
        // Specify the API response format and the request method.
        deleteSimilarityImageRequest.setAcceptFormat(FormatType.JSON);
        deleteSimilarityImageRequest.setMethod(MethodType.POST);
        deleteSimilarityImageRequest.setEncoding("utf-8");
        deleteSimilarityImageRequest.setProtocol(ProtocolType.HTTP);

        JSONObject httpBody = new JSONObject();

        // To delete images from an image library in batches, specify dataId. This operation is free of charge.
        httpBody.put("dataIds", Arrays.asList("x-3340-4b10-a622-7a5668f24b81"));

        deleteSimilarityImageRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()),
            "UTF-8", FormatType.JSON);

        /**
         * Set the timeout period. The server-side timeout period for a full-link request is 10 seconds. Configure the timeout period accordingly.
         * If the ReadTimeout value that you set is less than the server-side processing time, a ReadTimeout exception is returned.
         */
        deleteSimilarityImageRequest.setConnectTimeout(3000);
        deleteSimilarityImageRequest.setReadTimeout(10000);
        HttpResponse httpResponse = null;
        try {
            httpResponse = client.doAction(deleteSimilarityImageRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // The server receives the request, processes it, and returns a result.
        if (httpResponse != null && httpResponse.isSuccess()) {
            JSONObject scrResponse = JSON.parseObject(org.apache.commons.codec.binary.StringUtils.newStringUtf8(httpResponse.getHttpContent()));
            System.out.println(JSON.toJSONString(scrResponse, true));
            int requestCode = scrResponse.getIntValue("code");
            // The detection result of each image.
            JSONArray taskResults = scrResponse.getJSONArray("data");
            if (200 == requestCode) {
                for (Object taskResult : taskResults) {
                    // The processing result of a single image.
                    int taskCode = ((JSONObject) taskResult).getIntValue("code");
                    if (200 == taskCode) {
                        System.out.println("One image was successfully deleted.");
                    } else {
                        // The image failed to be processed. Analyze the cause based on the specific situation.
                        System.out.println("Task processing failed. Task response:" + JSON.toJSONString(taskResult));
                    }
                }
            } else {
                /**
                 * This indicates that the entire request failed to be processed. Analyze the cause based on the specific situation.
                 */
                System.out.println("The entire image scan request failed. Response:" + JSON.toJSONString(scrResponse));
            }
        }
    }

}