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

Calling the Passport OCR SDK

OCR SDKs use OCR RESTful APIs to simplify development.

To call an OCR API using an SDK, do the following:

Step 1: Subscribing to a Service

Step 2 Installing the SDK

Step 3: Obtaining Sample Code

Step 4: Calling the Service

Preparations

You have registered an account with Huawei Cloud. Your account cannot be in arrears or frozen.

Step 1: Subscribing to a Service

  1. Log in to the HUAWEI CLOUD OCR console.

    The service is deployed in the region by default. Select a region based on service requirements. For details about the region where each service is deployed, see Regions and Endpoints.

  2. Select and subscribe to your desired API.

    For this example, subscribe to the Passport OCR API.

    If the service is not enabled, an error message with error code "ModelArts.4204" will be displayed when you call the service. Before calling the service, log in to the OCR console and enable the service. Ensure that the region where the service is enabled is the same as that where the service is called.

Step 2: Installing the SDK

  1. Download and install the OCR Python SDK.

    The following uses PyCharm as an example to describe how to install the SDK.

    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 huaweicloudsdkcore package.
    pip install huaweicloudsdkcore
    # Install the huaweicloudsdkocr package.
    pip install huaweicloudsdkocr

  2. Check whether the SDK has been installed properly.

    Choose File > Settings > Project > Python Interpreter in PyCharm and check whether the SDK has been installed properly.

Step 3: Obtaining Sample Code

Obtain the SDK sample code of Passport OCR.

  1. Log in to the API Explorer and click Python on the Sample Code tab page.
  2. Configure request body parameters. Set image or url as input data.

  3. Copy the sample code to PyCharm.

Step 4: Calling the Service

  1. 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.

  2. Run the sample code to obtain recognition results.

  • If an HTTPS request error warning containing "InsecureRequestWarning: Unverified HTTPS request is being made" is displayed, add the following code to clear the warning:
    import urllibs 
    Urllibs.disable_warnings()
  • If the SSL configuration is incorrect, the message "Error:sslHandShakeException occurred" is displayed when you call the service. You can skip the SSL certificate verification or configure the CA certificate on the server as required, for details, see "Python SDK" > "Configure client connection parameters" > "(Optional) Configuring an SSL" in Optical Character Recognition SDK Reference.