Manual review for videos

Updated at:

This topic describes how to use the Java SDK to manually review videos.

Feature description

If the results of machine-assisted moderation for a video do not meet your expectations, you can use the video manual review API. For more information about the parameters, see the Video Manual Review API documentation.

You can call this SDK operation using the Content Moderation API endpoint. 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.

Submit a manual review task for a video

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.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.VideoAsyncManualScanRequest;
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.Arrays;

public class VideoAsyncManualScanSample {

    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 ways 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");
        // Note: To improve moderation performance, reuse the instantiated client as much as possible and avoid creating repeated connections.
        IAcsClient client = new DefaultAcsClient(profile);

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

        JSONObject httpBody = new JSONObject();
        JSONObject task = new JSONObject();
        task.put("dataId", "Data ID");
        // Set the video URL.
        task.put("url", "URL of the video to be moderated");
        httpBody.put("tasks", Arrays.asList(task));
        // The callback and seed parameters are optional and used for callback notifications.
        httpBody.put("callback", "Webhook address");
        httpBody.put("seed", "Random string");

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

        /**
         * Set the timeout period. The server-side timeout period for the entire link is 10 seconds. Set the timeout period accordingly.
         * If the ReadTimeout value that you set is smaller than the server-side processing time, a ReadTimeout exception is returned.
         */
        videoAsyncManualScanRequest.setConnectTimeout(3000);
        videoAsyncManualScanRequest.setReadTimeout(10000);

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

            // The result returned by the server after the server receives and processes the request.
            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 moderation result of each task.
                JSONArray taskResults = scrResponse.getJSONArray("data");
                if (200 == requestCode) {
                    for (Object taskResult : taskResults) {
                        // The processing result of a single task.
                        int taskCode = ((JSONObject) taskResult).getIntValue("code");
                        if (200 == taskCode) {
                            // Save the taskId to poll the result.
                            System.out.println(((JSONObject)taskResult).getString("taskId"));
                        } else {
                            // The processing of a single task failed. Analyze the cause based on the specific situation.
                            System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
                        }
                    }
                } else {
                    /**
                     * This indicates that the entire scan request failed. Analyze the cause based on the specific situation.
                     */
                    System.out.println("the whole scan request failed. response:" + JSON.toJSONString(scrResponse));
                }
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

Query the result of a manual review task for a video

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.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.VideoAsyncManualScanResultsRequest;
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.ArrayList;
import java.util.List;

public class VideoAsyncManualScanResults {

    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 ways 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");
        // Note: To improve moderation performance, reuse the instantiated client as much as possible and avoid creating repeated connections.
        IAcsClient client = new DefaultAcsClient(profile);

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


        List<String> taskIds = new ArrayList<String>();
        // Add the taskId that you want to query. You must save the taskId when you submit the task.
        taskIds.add("ID of the manual video review task");
        videoAsyncManualScanResultsRequest.setHttpContent(JSON.toJSONString(taskIds).getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        /**
         * You must set a timeout period.
         */
        videoAsyncManualScanResultsRequest.setConnectTimeout(3000);
        videoAsyncManualScanResultsRequest.setReadTimeout(6000);

        try {
            HttpResponse httpResponse = client.doAction(videoAsyncManualScanResultsRequest);
            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")) {
                    JSONArray taskResults = scrResponse.getJSONArray("data");
                    for (Object taskResult : taskResults) {
                        if (200 == ((JSONObject) taskResult).getInteger("code")) {
                            String url = ((JSONObject) taskResult).getString("url");
                            String taskId = ((JSONObject) taskResult).getString("taskId");
                            String suggestion = ((JSONObject) taskResult).getString("suggestion");
                            // Process the business logic based on the suggestion. For example, delete non-compliant data.
                        } else {
                            System.out.println("task process fail:" + ((JSONObject) taskResult).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();
        }
    }
}