Custom image libraries

Updated at:

This topic describes how to use the Python SDK to manage custom image libraries.

Feature description

You can customize image samples for intelligent pornography detection, terrorist and political content detection, and image or video ad detection to meet personalized content moderation requirements. For more information about parameters, see Create image library API documentation.

You must use the AI Guardrails endpoint to call the service using the SDK. For more information about API endpoints, see Endpoints.

Prerequisites

Get custom image library list

You can use the following code to get the list of image libraries associated with your account, 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

# Note: Reuse the instantiated client as much as possible to improve moderation performance and avoid repeated connections.
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# Create a new request object for each request. Do not reuse request objects.
request = DescribeImageLibRequest.DescribeImageLibRequest()
request.set_ServiceModule("open_api")
response = clt.do_action_with_exception(request)
print(response)

Create custom image library

You can use the following code to create a custom image library:

Note

Set different parameters based on your business scenarios.

# coding=utf-8
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import CreateImageLibRequest

# Note: Reuse the instantiated client as much as possible to improve moderation performance and avoid repeated connections.
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# Create a new request object for each request. Do not reuse request objects.
request = CreateImageLibRequest.CreateImageLibRequest()
request.set_ServiceModule("open_api")
request.set_Name("Pornography detection blocklist")
request.set_Scene("PORN")
request.set_Category("BLACK")
response = clt.do_action_with_exception(request)
print(response)

Modify custom image library

You can use the following code to modify the name and applicable business scenario (BizType) of a custom image library:

# coding=utf-8

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

# Note: Reuse the instantiated client as much as possible to improve moderation performance and avoid repeated connections.
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# Create a new request object for each request. Do not reuse request objects.
request = UpdateImageLibRequest.UpdateImageLibRequest()
request.set_Id(12345)
request.set_Name("Renamed pornography detection blocklist")
request.set_Scene("PORN")
request.set_Category("WHITE")
response = clt.do_action_with_exception(request)
print(response)

Delete 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.

# coding=utf-8
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DeleteImageLibRequest

# Note: Reuse the instantiated client as much as possible to improve moderation performance and avoid repeated connections.
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# Create a new request object for each request. Do not reuse request objects.
request = DeleteImageLibRequest.DeleteImageLibRequest()
request.set_Id(12345)
response = clt.do_action_with_exception(request)
print(response)

Get custom image library image list

You can use the following code to get the list of all added images in a custom image library:

# coding=utf-8

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

# Note: Reuse the instantiated client as much as possible to improve moderation performance and avoid repeated connections.
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# Create a new request object for each request. Do not reuse request objects.
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 custom images

You can use the following code to delete multiple custom 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

# Note: Reuse the instantiated client as much as possible to improve moderation performance and avoid repeated connections.
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# Create a new request object for each request. Do not reuse request objects.
request = DeleteImageFromLibRequest.DeleteImageFromLibRequest()
request.set_Ids("['669310']")
response = clt.do_action_with_exception(request)
print(response)