Manage custom image libraries

更新时间:
复制 MD 格式

Use the AI Guardrails SDK for Python to create and manage custom image libraries, then reference those libraries in image and video moderation tasks to detect pornographic content, terrorist content, and ad violations.

All examples share the same client initialization pattern. Reuse the client across requests to improve performance and avoid repeated connection overhead.

Prerequisites

Before you begin, ensure that you have:

Important

Use the Python version specified in the Installation guide. An incompatible version causes operation calls to fail.

List image libraries

DescribeImageLibRequest returns all image libraries in your account, including custom image libraries and feedback-based image libraries.

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

# Reuse this client across requests to improve performance.
clt = client.AcsClient(
    os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),      # AccessKey ID
    os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),  # AccessKey secret
    "cn-shanghai"
)
region_provider.modify_point("Green", "cn-shanghai", "green.cn-shanghai.aliyuncs.com")

# Create a new request object for each request. Request objects cannot be reused.
request = DescribeImageLibRequest.DescribeImageLibRequest()
request.set_ServiceModule("open_api")

response = clt.do_action_with_exception(request)
print(response)

Create a custom image library

CreateImageLibRequest creates a new custom image library. Set Scene and Category based on your moderation policy.

ParameterTypeDescription
ServiceModuleStringSet to open_api
NameStringDisplay name for the library
SceneStringModeration scenario: PORN, TERRORISM, or AD
CategoryStringLibrary type: BLACK (blocklist) or WHITE (allowlist). Blocklist images trigger violations; allowlist images bypass moderation.
# coding=utf-8
import os
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import CreateImageLibRequest

clt = client.AcsClient(
    os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
    os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
    "cn-shanghai"
)
region_provider.modify_point("Green", "cn-shanghai", "green.cn-shanghai.aliyuncs.com")

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)

For a full list of parameters, see Create an image library.

Update a custom image library

UpdateImageLibRequest changes the name or scene of an existing custom image library. Identify the library by its Id.

ParameterTypeDescription
IdIntegerID of the library to update
NameStringNew display name
SceneStringModeration scenario: PORN, TERRORISM, or AD
CategoryStringLibrary type: BLACK (blocklist) or WHITE (allowlist)
# coding=utf-8
import os
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import UpdateImageLibRequest

clt = client.AcsClient(
    os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
    os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
    "cn-shanghai"
)
region_provider.modify_point("Green", "cn-shanghai", "green.cn-shanghai.aliyuncs.com")

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

Warning

Deleting a custom image library also deletes all images in the library.

DeleteImageLibRequest deletes the library identified by Id.

ParameterTypeDescription
IdIntegerID of the library to delete
# coding=utf-8
import os
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DeleteImageLibRequest

clt = client.AcsClient(
    os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
    os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
    "cn-shanghai"
)
region_provider.modify_point("Green", "cn-shanghai", "green.cn-shanghai.aliyuncs.com")

request = DeleteImageLibRequest.DeleteImageLibRequest()
request.set_Id(12345)

response = clt.do_action_with_exception(request)
print(response)

List images in a custom image library

DescribeImageFromLibRequest returns a paginated list of images in a specific library.

ParameterTypeDescription
ImageLibIdIntegerID of the library to query
PageSizeIntegerNumber of images per page
CurrentPageIntegerPage number to retrieve, starting from 1
# coding=utf-8
import os
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DescribeImageFromLibRequest

clt = client.AcsClient(
    os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
    os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
    "cn-shanghai"
)
region_provider.modify_point("Green", "cn-shanghai", "green.cn-shanghai.aliyuncs.com")

request = DescribeImageFromLibRequest.DescribeImageFromLibRequest()
request.set_ImageLibId(12345)
request.set_PageSize(10)
request.set_CurrentPage(1)

response = clt.do_action_with_exception(request)
print(response)

Remove images from a custom image library

DeleteImageFromLibRequest removes one or more images from a library. Pass a list of image IDs to Ids.

ParameterTypeDescription
IdsStringList of image IDs to remove, for example "['669310']"
# coding=utf-8
import os
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DeleteImageFromLibRequest

clt = client.AcsClient(
    os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
    os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
    "cn-shanghai"
)
region_provider.modify_point("Green", "cn-shanghai", "green.cn-shanghai.aliyuncs.com")

request = DeleteImageFromLibRequest.DeleteImageFromLibRequest()
request.set_Ids("['669310']")

response = clt.do_action_with_exception(request)
print(response)

What's next