This topic describes how to use Content Moderation SDK for Java to manage custom image libraries.

Description

You can customize image samples based on your management requirements for personalized content. Then, you can use custom image libraries to intelligently detect pornographic content, terrorist content, and ad violations in images and videos. For more information about parameters, see CreateImageLib.

For more information, 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 want to submit a local image or a binary image stream for image moderation.

Query custom image libraries

You can use the following code to query image libraries, including custom image libraries and feedback-based image libraries:
DescribeImageLibRequest describeImageLibRequest = new DescribeImageLibRequest();
describeImageLibRequest.setServiceModule("open_api");

try {
     // Query all image libraries, including custom image libraries and feedback-based image libraries. 
     DescribeImageLibResponse describeImageLibResponse = client.getAcsResponse(describeImageLibRequest);
     System.out.println(JSON.toJSONString(describeImageLibResponse));

     List<DescribeImageLibResponse.ImageLib> allLibs = describeImageLibResponse.getImageLibList();
     List<DescribeImageLibResponse.ImageLib> customImageLibs = new ArrayList<DescribeImageLibResponse.ImageLib>();
     for (DescribeImageLibResponse.ImageLib imageLib : allLibs) {
     String source =  imageLib.getSource();
     // List custom image libraries. 
     if("MANUAL".equals(source)) {
         customImageLibs.add(imageLib);
        }

      // List feedback-based image libraries. 
      if("FEEDBACK".equals(source)) {
          customImageLibs.add(imageLib);
        }
      }

     System.out.println(JSON.toJSONString(customImageLibs));
} catch (ClientException e) {
            e.printStackTrace();
}

Create a custom image library

You can use the following code to create a custom image library:
Note Set the parameters based on your business scenario.
CreateImageLibRequest  createImageLibRequest = new CreateImageLibRequest();
createImageLibRequest.setServiceModule("open_api");
createImageLibRequest.setName("Pornography detection blacklist");
createImageLibRequest.setScene("PORN");
createImageLibRequest.setCategory("BLACK");

try {
    CreateImageLibResponse createImageLibResponse = client.getAcsResponse(createImageLibRequest);
    // The ID of the request. If no exception occurs, the custom image library is created. 
    String requestId = createImageLibResponse.getRequestId();
    System.out.println(JSON.toJSONString(createImageLibResponse));
  } catch (ClientException e) {
      e.printStackTrace();
  }

Modify a custom image library

You can use the following code to change the name of a custom image library and the business scenario to which the custom image library applies:
UpdateImageLibRequest updateImageLibRequest = new UpdateImageLibRequest();
// The ID of the custom image library. 
updateImageLibRequest.setId(12345);
updateImageLibRequest.setName("New name of the custom image library");
updateImageLibRequest.setBizTypes(JSON.toJSONString(Arrays.asList("comment")));
updateImageLibRequest.setCategory("WHITE");
updateImageLibRequest.setScene("PORN");
try {
    UpdateImageLibResponse updateImageLibResponse = client.getAcsResponse(updateImageLibRequest);
    // The ID of the request. If no exception occurs, the custom image library is modified. 
    String requestId = updateImageLibResponse.getRequestId();
    System.out.println(JSON.toJSONString(updateImageLibResponse));
} catch (ClientException e) {
    e.printStackTrace();
}

Delete a custom image library

You can use the following code to delete a custom image library:
Note If you delete a custom image library, all images in the library are also deleted.
DeleteImageLibRequest  deleteImageLibRequest = new DeleteImageLibRequest();
// The ID of the custom image library. 
deleteImageLibRequest.setId(12345);
try {
     DeleteImageLibResponse deleteImageLibResponse = client.getAcsResponse(deleteImageLibRequest);
     // The ID of the request. If no exception occurs, the custom image library is deleted. 
     String requestId = deleteImageLibResponse.getRequestId();
     System.out.println(JSON.toJSONString(deleteImageLibResponse));
} catch (ClientException e) {
    e.printStackTrace();
}

Query images in a custom image library

You can use the following code to query all images that have been added to a custom image library:
DescribeImageFromLibRequest describeImageFromLibRequest = new DescribeImageFromLibRequest();
describeImageFromLibRequest.setImageLibId(1519);
describeImageFromLibRequest.setPageSize(20);
describeImageFromLibRequest.setCurrentPage(1);
try {
    DescribeImageFromLibResponse describeImageFromLibResponse = client.getAcsResponse(describeImageFromLibRequest);
    // The number of images to return.
    describeImageFromLibResponse.getTotalCount();
    // The number of the page to return.
    describeImageFromLibResponse.getCurrentPage();
    // The number of images to return on each page.
    describeImageFromLibResponse.getPageSize();
    // The images.
    for (DescribeImageFromLibResponse.ImageFromLib imageFromLib : describeImageFromLibResponse.getImageFromLibList()) {
         // The primary key ID of the image.
         imageFromLib.getId();
         // The URL of the image.
         imageFromLib.getImage();
         // The URL of the image thumbnail.
         imageFromLib.getThumbnail();
     }
     System.out.println(JSON.toJSONString(describeImageFromLibResponse));
} catch (ClientException e) {
    e.printStackTrace();
}

Add an image to a custom image library

You can use the following code to add an image to a custom image library. To add an image, perform the following steps:
  1. Obtain the image upload credential.
  2. Upload the image to the custom image library.
  3. Submit the information about the library to which the image is uploaded and the image path to the server.
 // Obtain the image upload credential. 
DescribeUploadInfoRequest describeUploadInfoRequest = new DescribeUploadInfoRequest();
describeUploadInfoRequest.setBiz("customImageLib");
DescribeUploadInfoResponse describeUploadInfoResponse = null;
try {
     describeUploadInfoResponse = client.getAcsResponse(describeUploadInfoRequest);
     System.out.println(JSON.toJSONString(describeUploadInfoResponse));
} catch (ClientException e) {
            e.printStackTrace();
}

// Upload an image. 
CustomLibUploader customLibUploader = new CustomLibUploader();
String object = null;
try {
    object = customLibUploader.uploadFile(describeUploadInfoResponse.getHost(), describeUploadInfoResponse.getFolder(), describeUploadInfoResponse.getAccessid(),
             describeUploadInfoResponse.getPolicy(), describeUploadInfoResponse.getSignature(),
             "/Users/liuhai.lh/Desktop/a.jpg");
} catch (Exception e) {
    e.printStackTrace();
}

if(org.apache.commons.lang.StringUtils.isNotBlank(object)){
    UploadImageToLibRequest imageToLibRequest = new UploadImageToLibRequest();
    imageToLibRequest.setImageLibId(1519);
    imageToLibRequest.setImages(JSON.toJSONString(Arrays.asList(object)));

    try {
        UploadImageToLibResponse uploadImageToLibResponse = client.getAcsResponse(imageToLibRequest);
        // The ID of the request. If no exception occurs, the image is added to the custom image library. 
        String requestId = uploadImageToLibResponse.getRequestId();
        System.out.println(JSON.toJSONString(uploadImageToLibResponse));
    } catch (ClientException e) {
        e.printStackTrace();
    }
}

Remove images from a custom image library

You can use the following code to remove multiple images from a custom image library:
DeleteImageFromLibRequest deleteImageFromLibRequest = new DeleteImageFromLibRequest();
deleteImageFromLibRequest.setIds(JSON.toJSONString(Arrays.asList(669310)));
try {
    DeleteImageFromLibResponse deleteImageFromLibResponse = client.getAcsResponse(deleteImageFromLibRequest);
    // The ID of the request. If no exception occurs, the images are removed from the custom image library. 
    String requestId = uploadImageToLibResponse.getRequestId();
    System.out.println(JSON.toJSONString(deleteImageFromLibResponse));
} catch (ClientException e) {
    e.printStackTrace();
}