All Products
Search
Document Center

ImageAsyncScanRequest

Last Updated: Jun 06, 2019

Interface description

SDK interfaces

  1. ImageAsyncScanRequest

Function

Asynchronous image moderation: Identifies risky scenes in images, such as adult content.It must work with ImageAsyncScanResultsRequest to query results in a polling fashion.

region

  1. ap-southeast-1

Scene

Image moderation involves a variety of scenes, and each scene corresponds to a specific label. The relationship between scenes and labels is as follows:

Scene Label Remarks
porn normal Normal image without adult content
porn sexy Sexual image
porn porn Pornographic image

For example, if you want to identify pornographic images in your business, set the “scenes” parameter in the ImageAsyncScanRequest interface to porn. Similarly, you can set “scenes” to other values to suit different identification purposes.

Image requirements

  1. The image link protocol can be either HTTP or HTTPS.
  2. The image format can be PNG, JPG, JPEG, BMP, or GIF.
  3. The maximum image size for synchronous calls is 5 MB and that for asynchronous calls is 20 MB. The download time-out period is 3 seconds.
  4. The recommended image pixel is not smaller than 256 x 256. If the pixel is excessively small, the identification performance may be adversely affected.
  5. The response period of the Image Moderation API is determined by the image download period. Make sure that the storage service of the images is stable. It is recommended to store the images in OSS or cache them in CDN.

Call methods

Asynchronous image moderation – interface call example

  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Date;
  4. import java.util.LinkedHashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.UUID;
  8. import com.alibaba.fastjson.JSON;
  9. import com.alibaba.fastjson.JSONArray;
  10. import com.alibaba.fastjson.JSONObject;
  11. import com.aliyuncs.DefaultAcsClient;
  12. import com.aliyuncs.IAcsClient;
  13. import com.aliyuncs.exceptions.ClientException;
  14. import com.aliyuncs.exceptions.ServerException;
  15. import com.aliyuncs.green.model.v20170112.ImageAsyncScanRequest;
  16. import com.aliyuncs.http.FormatType;
  17. import com.aliyuncs.http.HttpResponse;
  18. import com.aliyuncs.profile.DefaultProfile;
  19. import com.aliyuncs.profile.IClientProfile;
  20. /**
  21. * Created by liuhai.lh on 2017/2/17.
  22. * Asynchronous image moderation interface
  23. * @author liuhai.lh
  24. * @date 2017/02/17
  25. */
  26. public class ImageAsyncScanRequestSample extends BaseSample{
  27. public static void main(String[] args) throws Exception {
  28. //Replace with your accessKeyId and accessKeySecret
  29. IClientProfile profile = DefaultProfile.getProfile("ap-southeast-1", "your accessKeyId", "your accessKeySecret");
  30. DefaultProfile.addEndpoint("ap-southeast-1", "ap-southeast-1", "Green", "green.ap-southeast-1.aliyuncs.com");
  31. IAcsClient client = new DefaultAcsClient(profile);
  32. ImageAsyncScanRequest imageAsyncScanRequest = new ImageAsyncScanRequest();
  33. imageAsyncScanRequest.setAcceptFormat(FormatType.JSON); // set return format
  34. imageAsyncScanRequest.setContentType(FormatType.JSON);
  35. imageAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // set method
  36. imageAsyncScanRequest.setEncoding("utf-8");
  37. imageAsyncScanRequest.setRegionId("ap-southeast-1");
  38. List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
  39. Map<String, Object> task1 = new LinkedHashMap<String, Object>();
  40. task1.put("dataId", UUID.randomUUID().toString());
  41. task1.put("url", "http://xxx.jpg");
  42. task1.put("time", new Date());
  43. tasks.add(task1);
  44. JSONObject data = new JSONObject();
  45. /**
  46. * porn
  47. */
  48. data.put("scenes", Arrays.asList("porn"));
  49. data.put("tasks", tasks);
  50. imageAsyncScanRequest.setContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
  51. /**
  52. * set timeout
  53. */
  54. imageAsyncScanRequest.setConnectTimeout(3000);
  55. imageAsyncScanRequest.setReadTimeout(6000);
  56. try {
  57. HttpResponse httpResponse = client.doAction(imageAsyncScanRequest);
  58. if(httpResponse.isSuccess()){
  59. JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getContent(), "UTF-8"));
  60. System.out.println(JSON.toJSONString(scrResponse, true));
  61. if (200 == scrResponse.getInteger("code")) {
  62. JSONArray taskResults = scrResponse.getJSONArray("data");
  63. for (Object taskResult : taskResults) {
  64. if(200 == ((JSONObject)taskResult).getInteger("code")){
  65. String taskId = ((JSONObject)taskResult).getString("taskId");
  66. // Save taskId for querying results later, refer to ImageAsyncScanResultsRequest
  67. System.out.println("args = [" + taskId + "]");
  68. }else{
  69. System.out.println("task process fail:" + ((JSONObject)taskResult).getInteger("code"));
  70. }
  71. }
  72. } else {
  73. System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
  74. }
  75. }else{
  76. System.out.println("response not success. status:" + httpResponse.getStatus());
  77. }
  78. } catch (ServerException e) {
  79. e.printStackTrace();
  80. } catch (ClientException e) {
  81. e.printStackTrace();
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. }