Updated on 2022-12-05 GMT+08:00

Demo Project of Image Tagging

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

  • Replace the AK/SK in the sample code with the actual AK/SK. You can use either the url or image parameter in the initialized ImageTaggingReq to configure image information. This section uses url as an example.
  • endpoint indicates the regions and endpoints for HUAWEI CLOUD services. For details, see Regions and Endpoints.
  1. The sample code for calling Image Tagging is as follows:
     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
    # coding: utf-8
    
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkcore.http.http_config import HttpConfig
    from huaweicloudsdkimage.v2 import ImageClient,RunImageTaggingRequest,ImageTaggingReq
    from huaweicloudsdkimage.v2.region.image_region import ImageRegion
    
    if __name__ == "__main__":
        //Enter your AK/SK.
        ak = "<YOUR AK>"
        sk = "<YOUR SK>"
    
        credentials = BasicCredentials(ak, sk) 
    
        client = ImageClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(ImageRegion.value_of("cn-north-1")) \
            .build()
    
        try:
            request = RunImageTaggingRequest()
            request.body = ImageTaggingReq(
                limit=50,
                threshold=95,
                language="zh",
                url="https://XXX.jpg"  //Replace it with an image URL that can be accessed from the Internet.
            )
            response = client.run_image_tagging(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. Execute the sample code. If 200 is displayed on the console, the code is successfully executed. For details about related parameters, see Image Tagging APIs.
    1
    2
    3
    200
    {"result": {"tags": [{"confidence": "98.00", "type": "Tree", "tag": "Tree", "i18n_tag": {"zh": "Chinese characters for tree", "en": "Tree"}, "i18n_type": {"zh": "Chinese characters for tree", "en": "Tree"}, "instances": []}]}}
    Process finished with exit code 0