Manual image review
This topic describes how to use the Java software development kit (SDK) to call the manual image review API.
Description
If the result of machine-assisted moderation for an image does not meet your expectations, you can use the manual image review API. For more information about the parameters, see Manual image review.
You can use the API endpoint for Content Moderation to call this SDK operation. For more information about API endpoints, see Endpoints.
Prerequisites
Java dependencies are installed. For more information, see Installation.
NoteYou 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 image review task
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.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.ImageAsyncManualScanRequest;
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;
import java.util.UUID;
public class ImageAsyncManualScanSample {
public static void main(String[] args) throws Exception {
/**
* An Alibaba Cloud account AccessKey has full access to all APIs. For security, use a RAM user to make API calls or perform 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");
// Note: To improve moderation performance, reuse the instantiated client. This avoids establishing repeated connections.
IAcsClient client = new DefaultAcsClient(profile);
ImageAsyncManualScanRequest imageAsyncManualScanRequest = new ImageAsyncManualScanRequest();
// Specify the API response format.
imageAsyncManualScanRequest.setAcceptFormat(FormatType.JSON);
// Specify the request method.
imageAsyncManualScanRequest.setMethod(MethodType.POST);
imageAsyncManualScanRequest.setEncoding("utf-8");
// HTTP and HTTPS are supported.
imageAsyncManualScanRequest.setProtocol(ProtocolType.HTTP);
JSONObject task = new JSONObject();
task.put("dataId", UUID.randomUUID().toString());
// Set the image URL.
task.put("url", "The URL of the image to be moderated.");
JSONObject httpBody = new JSONObject();
httpBody.put("tasks", Arrays.asList(task));
// The callback and seed parameters are optional and used for webhook notifications.
httpBody.put("callback", "The webhook address.");
httpBody.put("seed", "A random string.");
imageAsyncManualScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()),
"UTF-8", FormatType.JSON);
/**
* Set the timeout period. The server-side timeout period is 10 seconds. Set the timeout period accordingly.
* If the ReadTimeout value is less than the server-side timeout period, a ReadTimeout exception is returned.
*/
imageAsyncManualScanRequest.setConnectTimeout(3000);
imageAsyncManualScanRequest.setReadTimeout(10000);
try {
HttpResponse httpResponse = client.doAction(imageAsyncManualScanRequest);
// 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 for 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 image review task
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.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.ImageAsyncManualScanResultsRequest;
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 ImageAsyncManualScanResults {
public static void main(String[] args) throws Exception {
/**
* An Alibaba Cloud account AccessKey has full access to all APIs. For security, use a RAM user to make API calls or perform 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");
// Note: To improve moderation performance, reuse the instantiated client. This avoids establishing repeated connections.
IAcsClient client = new DefaultAcsClient(profile);
ImageAsyncManualScanResultsRequest imageAsyncManualScanResultsRequest =
new ImageAsyncManualScanResultsRequest();
// Specify the API response format.
imageAsyncManualScanResultsRequest.setAcceptFormat(FormatType.JSON);
// Specify the request method.
imageAsyncManualScanResultsRequest.setMethod(MethodType.POST);
imageAsyncManualScanResultsRequest.setEncoding("utf-8");
// HTTP and HTTPS are supported.
imageAsyncManualScanResultsRequest.setProtocol(ProtocolType.HTTP);
List<String> taskIds = new ArrayList<String>();
// Add the task ID that you want to query. You must save the task ID when you submit the task.
taskIds.add("The ID of the manual image review task.");
imageAsyncManualScanResultsRequest.setHttpContent(JSON.toJSONString(taskIds).getBytes("UTF-8"), "UTF-8", FormatType.JSON);
/**
* You must set a timeout period.
*/
imageAsyncManualScanResultsRequest.setConnectTimeout(3000);
imageAsyncManualScanResultsRequest.setReadTimeout(6000);
try {
HttpResponse httpResponse = client.doAction(imageAsyncManualScanResultsRequest);
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();
}
}
}