Image Moderation is for scanning images to detect whether there is inappropriate content.

  • You can submit image with url, local file path or file binary stream.

Preparation

Please complete the following preparations before starting:
  1. Create AccessKeyId and AccessKeySecret. Please refer to Create AccessKey
  2. For dependencies installation, please refer to Install Dependencies

Image Moderation

Interface Description
Interface Features Region Description
ImageSyncScanRequest Image Scan (Synchronized) cn-shanghai cn-beijing cn-shenzhen ap-southeast-1 us-west-1 Mutliple risks scenes can be scan in one rrequest, such as porn, terrorism, ad, qrcode, logo. Fee will be charged per image per scens
Sample Code
  • Scan Images by submitting urls
    1. import com.alibaba.fastjson.JSON;
    2. import com.alibaba.fastjson.JSONArray;
    3. import com.alibaba.fastjson.JSONObject;
    4. import com.aliyuncs.DefaultAcsClient;
    5. import com.aliyuncs.IAcsClient;
    6. import com.aliyuncs.green.model.v20180509.ImageSyncScanRequest;
    7. import com.aliyuncs.http.FormatType;
    8. import com.aliyuncs.http.HttpResponse;
    9. import com.aliyuncs.http.MethodType;
    10. import com.aliyuncs.http.ProtocolType;
    11. import com.aliyuncs.profile.DefaultProfile;
    12. import com.aliyuncs.profile.IClientProfile;
    13. import java.util.*;
    14. public class Main {
    15. public static void main(String[] args) throws Exception {
    16. IClientProfile profile = DefaultProfile
    17. .getProfile("cn-shanghai", "Your accessKeyId", "Your accessKeySecret");
    18. DefaultProfile
    19. .addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    20. IAcsClient client = new DefaultAcsClient(profile);
    21. ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
    22. imageSyncScanRequest.setAcceptFormat(FormatType.JSON);
    23. imageSyncScanRequest.setMethod(MethodType.POST);
    24. imageSyncScanRequest.setEncoding("utf-8");
    25. //both http and https supported
    26. imageSyncScanRequest.setProtocol(ProtocolType.HTTP);
    27. JSONObject httpBody = new JSONObject();
    28. /**
    29. * Set the target risk scenes for scanning. Each scene will be charged separately.
    30. * In one request, you can scan multiple image for multiple scenes. Fee will be charged per image per scene.
    31. * porn: for adult content filtering, terrorism: for terrorism and violence filtering, ocr: to detect text from images( only simple Chinese, traditional Chinese and English supported )
    32. */
    33. httpBody.put("scenes", Arrays.asList("porn"));
    34. /**
    35. * input imges, one task contains one image
    36. * When batch scanning, processinng time will be defined by the image last finished.
    37. * Here is sample code for single image per request
    38. */
    39. JSONObject task = new JSONObject();
    40. task.put("dataId", UUID.randomUUID().toString());
    41. //url of image
    42. task.put("url", "http://xxx.test.jpg");
    43. task.put("time", new Date());
    44. httpBody.put("tasks", Arrays.asList(task));
    45. imageSyncScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()),
    46. "UTF-8", FormatType.JSON);
    47. /**
    48. * Timeout setting
    49. */
    50. imageSyncScanRequest.setConnectTimeout(3000);
    51. imageSyncScanRequest.setReadTimeout(10000);
    52. HttpResponse httpResponse = null;
    53. try {
    54. httpResponse = client.doAction(imageSyncScanRequest);
    55. } catch (Exception e) {
    56. e.printStackTrace();
    57. }
    58. //servcer accept the request and response when complete
    59. if (httpResponse != null && httpResponse.isSuccess()) {
    60. JSONObject scrResponse = JSON.parseObject(org.apache.commons.codec.binary.StringUtils.newStringUtf8(httpResponse.getHttpContent()));
    61. System.out.println(JSON.toJSONString(scrResponse, true));
    62. int requestCode = scrResponse.getIntValue("code");
    63. //scan results of each image
    64. JSONArray taskResults = scrResponse.getJSONArray("data");
    65. if (200 == requestCode) {
    66. for (Object taskResult : taskResults) {
    67. //scanning result code for current image
    68. int taskCode = ((JSONObject) taskResult).getIntValue("code");
    69. //scan results per scenes for current image
    70. JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");
    71. if (200 == taskCode) {
    72. for (Object sceneResult : sceneResults) {
    73. String scene = ((JSONObject) sceneResult).getString("scene");
    74. String suggestion = ((JSONObject) sceneResult).getString("suggestion");
    75. //do something according to scene and suggestion
    76. System.out.println("scene = [" + scene + "]");
    77. System.out.println("suggestion = [" + suggestion + "]");
    78. }
    79. } else {
    80. //error happen during single image processing
    81. System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
    82. }
    83. }
    84. } else {
    85. /**
    86. * the whole requuest failed
    87. */
    88. System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
    89. }
    90. }
    91. }
    92. }
  • Submit local image to scan
    1. import com.alibaba.fastjson.JSON;
    2. import com.alibaba.fastjson.JSONArray;
    3. import com.alibaba.fastjson.JSONObject;
    4. import com.aliyuncs.DefaultAcsClient;
    5. import com.aliyuncs.IAcsClient;
    6. import com.aliyuncs.green.extension.uploader.ClientUploader;
    7. import com.aliyuncs.green.model.v20180509.ImageSyncScanRequest;
    8. import com.aliyuncs.http.FormatType;
    9. import com.aliyuncs.http.HttpResponse;
    10. import com.aliyuncs.http.MethodType;
    11. import com.aliyuncs.http.ProtocolType;
    12. import com.aliyuncs.profile.DefaultProfile;
    13. import com.aliyuncs.profile.IClientProfile;
    14. import java.util.*;
    15. public class Main {
    16. public static void main(String[] args) throws Exception {
    17. IClientProfile profile = DefaultProfile
    18. .getProfile("cn-shanghai", "Your accessKeyId", "Your accessKeySecret");
    19. DefaultProfile
    20. .addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    21. IAcsClient client = new DefaultAcsClient(profile);
    22. ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
    23. imageSyncScanRequest.setAcceptFormat(FormatType.JSON);
    24. imageSyncScanRequest.setMethod(MethodType.POST);
    25. imageSyncScanRequest.setEncoding("utf-8");
    26. //both http and https supported
    27. imageSyncScanRequest.setProtocol(ProtocolType.HTTP);
    28. JSONObject httpBody = new JSONObject();
    29. /**
    30. * Set the target risk scenes for scanning. Each scene will be charged separately.
    31. * In one request, you can scan multiple image for multiple scenes. Fee will be charged per image per scene.
    32. * porn: for adult content filtering, terrorism: for terrorism and violence filtering, ocr: to detect text from images( only simple Chinese, traditional Chinese and English supported )
    33. */
    34. httpBody.put("scenes", Arrays.asList("porn"));
    35. /**
    36. * Upload your local image file to generate url for scanning
    37. */
    38. String url = null;
    39. ClientUploader uploader = ClientUploader.getImageClientUploader(client);
    40. try{
    41. url = uploader.uploadFile("d:/test.jpg");
    42. }catch (Exception e){
    43. e.printStackTrace();
    44. }
    45. /**
    46. * input imges, one task contains one image
    47. * When batch scanning, processinng time will be defined by the image last finished.
    48. * Here is sample code for single image per request
    49. */
    50. JSONObject task = new JSONObject();
    51. task.put("dataId", UUID.randomUUID().toString());
    52. //url of image after uploading
    53. task.put("url", url);
    54. task.put("time", new Date());
    55. httpBody.put("tasks", Arrays.asList(task));
    56. imageSyncScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()),
    57. "UTF-8", FormatType.JSON);
    58. /**
    59. * timeout setting is required
    60. */
    61. imageSyncScanRequest.setConnectTimeout(3000);
    62. imageSyncScanRequest.setReadTimeout(10000);
    63. HttpResponse httpResponse = null;
    64. try {
    65. httpResponse = client.doAction(imageSyncScanRequest);
    66. } catch (Exception e) {
    67. e.printStackTrace();
    68. }
    69. //Server accept request and response after complete
    70. if (httpResponse != null && httpResponse.isSuccess()) {
    71. JSONObject scrResponse = JSON.parseObject(org.apache.commons.codec.binary.StringUtils.newStringUtf8(httpResponse.getHttpContent()));
    72. System.out.println(JSON.toJSONString(scrResponse, true));
    73. int requestCode = scrResponse.getIntValue("code");
    74. //handling result of all images
    75. JSONArray taskResults = scrResponse.getJSONArray("data");
    76. if (200 == requestCode) {
    77. for (Object taskResult : taskResults) {
    78. //result code for each image
    79. int taskCode = ((JSONObject) taskResult).getIntValue("code");
    80. //all results of each scanning scene will be returned
    81. JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");
    82. if (200 == taskCode) {
    83. for (Object sceneResult : sceneResults) {
    84. String scene = ((JSONObject) sceneResult).getString("scene");
    85. String suggestion = ((JSONObject) sceneResult).getString("suggestion");
    86. //do something according to scene and suggestionn
    87. System.out.println("scene = [" + scene + "]");
    88. System.out.println("suggestion = [" + suggestion + "]");
    89. }
    90. } else {
    91. //error when scanning the image
    92. System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
    93. }
    94. }
    95. } else {
    96. /**
    97. * the whole request handle failed
    98. */
    99. System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
    100. }
    101. }
    102. }
    103. }
  • Scan by submitting file stream
    1. import com.alibaba.fastjson.JSON;
    2. import com.alibaba.fastjson.JSONArray;
    3. import com.alibaba.fastjson.JSONObject;
    4. import com.aliyuncs.DefaultAcsClient;
    5. import com.aliyuncs.IAcsClient;
    6. import com.aliyuncs.green.extension.uploader.ClientUploader;
    7. import com.aliyuncs.green.model.v20180509.ImageSyncScanRequest;
    8. import com.aliyuncs.http.FormatType;
    9. import com.aliyuncs.http.HttpResponse;
    10. import com.aliyuncs.http.MethodType;
    11. import com.aliyuncs.http.ProtocolType;
    12. import com.aliyuncs.profile.DefaultProfile;
    13. import com.aliyuncs.profile.IClientProfile;
    14. import org.apache.commons.io.FileUtils;
    15. import java.io.File;
    16. import java.util.*;
    17. public class Main {
    18. public static void main(String[] args) throws Exception {
    19. IClientProfile profile = DefaultProfile
    20. .getProfile("cn-shanghai", "Your accessKeyId", "Your accessKeySecret");
    21. DefaultProfile
    22. .addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    23. IAcsClient client = new DefaultAcsClient(profile);
    24. ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
    25. // format of return result format
    26. imageSyncScanRequest.setAcceptFormat(FormatType.JSON);
    27. // request method
    28. imageSyncScanRequest.setMethod(MethodType.POST);
    29. imageSyncScanRequest.setEncoding("utf-8");
    30. //both http and https supported
    31. imageSyncScanRequest.setProtocol(ProtocolType.HTTP);
    32. JSONObject httpBody = new JSONObject();
    33. /**
    34. * Set the target risk scenes for scanning. Each scene will be charged separately.
    35. * In one request, you can scan multiple image for multiple scenes. Fee will be charged per image per scene.
    36. * porn: for adult content filtering, terrorism: for terrorism and violence filtering, ocr: to detect text from images( only simple Chinese, traditional Chinese and English supported )
    37. */
    38. httpBody.put("scenes", Arrays.asList("porn"));
    39. /**
    40. * Upload your local image file to generate url for scanning
    41. */
    42. ClientUploader uploader = ClientUploader.getImageClientUploader(client);
    43. byte[] imageBytes = null;
    44. String url = null;
    45. try{
    46. //input file stream here
    47. imageBytes = FileUtils.readFileToByteArray(new File("/Users/01fb4ab6420b5f34623e13b82b51ef87.jpg"));
    48. //uploading
    49. url = uploader.uploadBytes(imageBytes);
    50. }catch (Exception e){
    51. e.printStackTrace();
    52. }
    53. /**
    54. * input imges, one task contains one image
    55. * When batch scanning, processinng time will be defined by the image last finished.
    56. * Here is sample code for single image per request
    57. */
    58. JSONObject task = new JSONObject();
    59. task.put("dataId", UUID.randomUUID().toString());
    60. //url after uploading
    61. task.put("url", url);
    62. task.put("time", new Date());
    63. httpBody.put("tasks", Arrays.asList(task));
    64. imageSyncScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()),
    65. "UTF-8", FormatType.JSON);
    66. /**
    67. * timeout setting
    68. */
    69. imageSyncScanRequest.setConnectTimeout(3000);
    70. imageSyncScanRequest.setReadTimeout(10000);
    71. HttpResponse httpResponse = null;
    72. try {
    73. httpResponse = client.doAction(imageSyncScanRequest);
    74. } catch (Exception e) {
    75. e.printStackTrace();
    76. }
    77. //Server accept request and response after complete
    78. if (httpResponse != null && httpResponse.isSuccess()) {
    79. JSONObject scrResponse = JSON.parseObject(org.apache.commons.codec.binary.StringUtils.newStringUtf8(httpResponse.getHttpContent()));
    80. System.out.println(JSON.toJSONString(scrResponse, true));
    81. int requestCode = scrResponse.getIntValue("code");
    82. //results of scan tasks
    83. JSONArray taskResults = scrResponse.getJSONArray("data");
    84. if (200 == requestCode) {
    85. for (Object taskResult : taskResults) {
    86. //return code per image
    87. int taskCode = ((JSONObject) taskResult).getIntValue("code");
    88. //results for each scens of each image
    89. JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");
    90. if (200 == taskCode) {
    91. for (Object sceneResult : sceneResults) {
    92. String scene = ((JSONObject) sceneResult).getString("scene");
    93. String suggestion = ((JSONObject) sceneResult).getString("suggestion");
    94. //do something according to scene and suggestion
    95. System.out.println("scene = [" + scene + "]");
    96. System.out.println("suggestion = [" + suggestion + "]");
    97. }
    98. } else {
    99. //single task failed
    100. System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
    101. }
    102. }
    103. } else {
    104. /**
    105. * the whole request failed
    106. */
    107. System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
    108. }
    109. }
    110. }
    111. }