Help Center> > SDK Reference >Using Python SDK

Using Python SDK

Users can use SDK in token- or AK/SK-based authentication mode. For details about how to obtain authentication messages, see Authentication. This section describes how to use SDK in token- and AK/SK-based modes.

Download OCR Python SDK from https://developer.huaweicloud.com/en-us/sdk?OCR, and decompress it.

A user needs to apply for enabling the service before using it for the first time. For details about how to enable the service, click Subscribe. You only need to subscribe to OCR once.

AK/SK-based Authentication

This section uses ID Card OCR as an example to describe how to use SDK in AK/SK-based authentication mode.

  1. Obtain AK/SK. For details, see Authentication > AK/SK-based Authentication.
  2. Configure AK/SK of the Python SDK.
    Change the values of AK and SK of the aksk_request function in the OCRDemo.py file of the demo project to the obtained AK/SK.
    Figure 1 Modifying parameters in the OCRDemo.py file
  3. Modify parameters.

    If you use a local image file for recognition, change the image path in OCRDemo.py to the path of the file. If you use the default image of the SDK, you do not need to modify the image path.

  4. Execute the OCRDemo.py file. If 200 is displayed on the console, the program is successfully executed. You can view the recognition result on the console.
    {
        "result": {
            "name": "name recognized from the image",
            "sex": "sex recognized from the image",
            "ethnicity": "ethnicity recognized from the image",
            "birth": "1990-01-24", 
            "address": "address recognized from the image",
            "number": "389201199001245580"
        }
    }

    To call other OCR services, change the value of parameter req_uri of the aksk_request function to the specific URI. For the URI list, see Mappings Between Services and APIs.

Token-based Authentication

This section uses ID Card OCR as an example to describe how to use SDK in token-based authentication mode.

  1. Open the OCRDemo.py file. Change the values of username, password, and domain_name in the token_request function to the actual username, password, and domain name registered with the system. If the user is not an IAM user, the domain name and username are the same.
    Figure 2 Changing the username, password, and domain name in the OCRDemo.py file
  2. Execute the code. The recognition result is displayed.