Updated on 2023-07-17 GMT+08:00

Image Moderation (V2)

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

    Replace the AK/SK in the sample code with the actual AK/SK. You can modify either the image or url parameter in ImageDetectionReq to configure image information. This section uses url as an example.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    # coding: utf-8
    
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkmoderation.v2.region.moderation_region import ModerationRegion
    from huaweicloudsdkmoderation.v2 import ModerationClient,RunImageModerationRequest,ImageDetectionReq
    
    if __name__ == "__main__":
           //Enter your AK/SK.
        ak = "<YOUR AK>"
        sk = "<YOUR SK>"
    
        credentials = BasicCredentials(ak, sk)
    
        client = ModerationClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(ModerationRegion.value_of("xxx")) \   //Replace xxx with the region where the service is located.
            .build()
    
        try:
            request = RunImageModerationRequest()
            listImageDetectionReqCategoriesbody = [
                "porn",
            ]
            request.body = ImageDetectionReq(
                threshold=0,
                categories=listImageDetectionReqCategoriesbody,
                moderation_rule="default",
                url="https://XXX.jpg"  //Replace it with an image URL that can be accessed from the Internet.
            )
            response = client.run_image_moderation(request)
            print(response.status_code)
            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.
    200
    {"result": {"detail": { "porn": [{"confidence": 0.9883, "label": "normal"}, {"confidence": 0.0039, "label": "porn"}, {"confidence": 0.0078, "label": "sexy"}]}, "suggestion": "pass", "category_suggestions": {   "porn": "pass"}}}
    Process finished with exit code 0