Calling APIs or SDKs Locally
Image Recognition Software Development Kit (Image SDK) encapsulates the RESTful APIs provided by Image Recognition to simplify application development. You can add dependencies or download SDKs to call APIs to use Image Recognition.
This section uses Image Tagging as an example to describe how to use the Image Python SDK for local development. You can use the SDK by calling API functions.
- Install the Python environment and obtain the SDK.
- Download Python of a proper version from Python's official website and install it. Python 3.3 or later is recommended. This section uses Python 3.7 as an example.
- Download the latest version of PyCharm from PyCharm's official website.
- Start the PyCharm development tool and choose to configure the Python environment.
- Select the Python installation path. See Figure 1. After selecting the target Python, click Apply at the bottom of the page to complete the configuration.
- Create a project in PyCharm and click Terminal in the lower left corner. Run the following commands to install the SDK (the SDK supports Python 3 or later):
Install the SDK using pip commands: # Install the core library. pip install huaweicloudsdkcore # Install the image service library. pip install huaweicloudsdkimage To install the Python SDK using PyCharm, perform the following steps: 1. Start PyCharm and choose File > Settings... 2. Click Python Interpreter and then +. 3. Click +, search for huaweicloudsdkcore and huaweicloudsdkimage, and click Install Package in the lower left corner to install the packages.
On PyCharm, choose File > Settings > Project > Python Interpreter. Click + in the upper right corner, search for huaweicloudsdkcore and huaweicloudsdkimage respectively, and click Install Package in the lower left corner to install them.Figure 2 Installing the Python SDK for Image Recognition using PyCharm
- Copy the SDK sample code of Image Tagging to PyCharm.
# coding: utf-8 from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkimage.v2.region.image_region import ImageRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkimage.v2 import * 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("ap-southeast-1")) \ .build() try: request = RunImageTaggingRequest() request.body = ImageTaggingReq( url="https://support.huaweicloud.com/intl/zh-cn/productdesc-image/zh-cn_image_0241182299.png" ) response = client.run_image_tagging(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
- Obtain the AK and SK and replace <YOUR AK> and <YOUR SK> in the sample code with the AK and SK, respectively.
Log in to the Access Keys page and create an access key or use an existing access key. The AK and SK are contained in the credentials.csv file.
- Run the sample code to obtain the recognition result. You can interpret the review result based on the response parameter description. For details, see the image tag recognition result.
Figure 3 Example