Updated on 2025-07-02 GMT+08:00

Image Moderation (V3)

  1. This section describes how to use the Image Moderation SDK by AK/SK authentication.

    Change the value of url or image in ImageDetectionReq in the sample code to configure the image information. Then run the code.

    # coding: utf-8
     
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkmoderation.v3.region.moderation_region import ModerationRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkmoderation.v3 import *
    import os
     
    if __name__ == "__main__":
        # Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
        # In this example, the AK and SK are stored in environment variables for identity authentication. Before running this example, configure environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK.
        ak = os.getenv("HUAWEICLOUD_SDK_AK")
        sk = os.getenv("HUAWEICLOUD_SDK_SK")
        projectId = "project_id"
     
        credentials = BasicCredentials(ak, sk, projectId) \
     
        client = ModerationClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(ModerationRegion.value_of("xxx")) \   
            .build()
        # Replace xxx with the service region, for example, ap-southeast-3.
        try:
            request = CheckImageModerationRequest()
            listCategoriesbody = [
                "porn"
            ]
            request.body = ImageDetectionReq(
                url="http://www.xxxx.xxxx",
                categories=listCategoriesbody,
                event_type="head_image"
            )
            response = client.check_image_moderation(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
  2. If 200 is displayed on the console, the code is successfully executed. The image moderation result is displayed on the console.
    {"request_id": "69b424920668cb15e6d25eef879a023b", "result": {"suggestion": "block", "category": "image_text", "details": [{"suggestion": "block", "category": "image_text", "confidence": 1.0, "qr_location": {"top_left_x": 74, "top_left_y": 66, "bottom_right_x": 151, "bottom_right_y": 142}, "qr_content": "http://xxxxxx/xxxxx", "label": "qr_code"}, {"suggestion": "block", "category": "image_text", "confidence": 0.98779297, "label": "ad"}, {"suggestion": "block", "category": "image_text", "confidence": 1.0, "label": "qr_code"}], "ocr_text": "xxxxxxxxx"}}