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

Background information

You can 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. For more information about parameters, see Create an image library.

Prerequisites

  • Python dependencies are installed. For more information, see Installation.
    Note You must use the required Python 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:
# coding=utf-8

from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DescribeImageLibRequest

# Enter your AccessKey pair. 
clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret", 'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# The request object cannot be reused. You must create a request object for each request. 
request = DescribeImageLibRequest.DescribeImageLibRequest()
request.set_ServiceModule("open_api")
response = clt.do_action_with_exception(request)
print(response)

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.
# coding=utf-8
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import CreateImageLibRequest

# Enter your AccessKey pair. 
clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret", 'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# The request object cannot be reused. You must create a request object for each request. 
request = CreateImageLibRequest.CreateImageLibRequest()
request.set_ServiceModule("open_api")
request.set_Name("Pornography detection blacklist")
request.set_Scene("PORN")
request.set_Category("BLACK")
response = clt.do_action_with_exception(request)
print(response)

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:

# coding=utf-8

from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import UpdateImageLibRequest

# Enter your AccessKey pair. 
clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret", 'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# The request object cannot be reused. You must create a request object for each request. 
request = UpdateImageLibRequest.UpdateImageLibRequest()
request.set_Id(12345)
request.set_Name("New name of the image library")
request.set_Scene("PORN")
request.set_Category("WHITE")
response = clt.do_action_with_exception(request)
print(response)

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.
# coding=utf-8
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DeleteImageLibRequest

# Enter your AccessKey pair. 
clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret", 'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# The request object cannot be reused. You must create a request object for each request. 
request = DeleteImageLibRequest.DeleteImageLibRequest()
request.set_Id(12345)
response = clt.do_action_with_exception(request)
print(response)

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:
# coding=utf-8

from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DescribeImageFromLibRequest

# Enter your AccessKey pair. 
clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret", 'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# The request object cannot be reused. You must create a request object for each request. 
request = DescribeImageFromLibRequest.DescribeImageFromLibRequest()
request.set_ImageLibId(12345)
request.set_PageSize(10)
request.set_CurrentPage(1)
response = clt.do_action_with_exception(request)
print(response)

Delete images from a custom image library

You can use the following code to delete multiple images from a custom image library:
# coding=utf-8

from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DeleteImageFromLibRequest

# Enter your AccessKey pair. 
clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret", 'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# The request object cannot be reused. You must create a request object for each request. 
request = DeleteImageFromLibRequest.DeleteImageFromLibRequest()
request.set_Ids("['669310']")
response = clt.do_action_with_exception(request)
print(response)