This topic describes the operations supported by the SDK for Java for the Content Moderation API to manage custom image libraries. You can perform these operations to customize image samples based on personalized content management requirements. Then, you can use custom image libraries to intelligently detect pornographic content, terrorist content, and ad violations in images and videos.

Before you begin

Before you call operations, make the following preparations:

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 {
            // List 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);
            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 and bizType of a custom image library:
UpdateImageLibRequest  updateImageLibRequest = new UpdateImageLibRequest();
        // Specify the ID of the 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);
            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 When you delete a custom image library, all images in the library are also deleted.
DeleteImageLibRequest  deleteImageLibRequest = new DeleteImageLibRequest();
        // Specify the ID of the image library.
        deleteImageLibRequest.setId(12345);
        try {
            DeleteImageLibResponse deleteImageLibResponse = client.getAcsResponse(deleteImageLibRequest);
            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);
            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 an 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);
                System.out.println(JSON.toJSONString(uploadImageToLibResponse));
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }

Delete images from a custom image library

You can use the following code to delete multiple images from a custom image library:
DeleteImageFromLibRequest deleteImageFromLibRequest = new DeleteImageFromLibRequest();
deleteImageFromLibRequest.setIds(JSON.toJSONString(Arrays.asList(669310)));
try {
    DeleteImageFromLibResponse deleteImageFromLibResponse = client.getAcsResponse(deleteImageFromLibRequest);
    System.out.println(JSON.toJSONString(deleteImageFromLibResponse));
} catch (ClientException e) {
    e.printStackTrace();
}