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

Image Moderation (Batch) (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 set Urls in ImageBatchModerationReq to configure the URLs of the images to be reviewed, and then execute the code.

     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
    39
    40
    # 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,RunImageBatchModerationRequest,ImageBatchModerationReq
    
    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 = RunImageBatchModerationRequest()
            listImageBatchModerationReqCategoriesbody = [
                "porn",
            ]
            listImageBatchModerationReqUrlsbody = [
                "https://XXX.jpg"   //Replace it with an image URL that can be accessed from the Internet.
            ]
            request.body = ImageBatchModerationReq(
                threshold=0,
                categories=listImageBatchModerationReqCategoriesbody,
                urls=listImageBatchModerationReqUrlsbody
            )
            response = client.run_image_batch_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 batch Image Moderation result is displayed on the console.
    200
    {'result': [{'category_suggestions': {
                                          
                                          'porn': 'pass',
                                          },
                 'detail': {
                            'porn': [{'confidence': 0.9883, 'label': 'normal'},
                                     {'confidence': 0.0039, 'label': 'porn'},
                                     {'confidence': 0.0078, 'label': 'sexy'}],
                 'suggestion': 'pass',
                 'url': 'https://sdk-obs-source-save.obs.cn-north-4.myhuaweicloud.com/terrorism.jpg'}]}
    
    Process finished with exit code 0