Updated on 2025-08-15 GMT+08:00

Connecting to OCR to Recognize Text in Images

Optical Character Recognition (OCR) provides services through open Application Programming Interfaces (APIs). You can use programming languages such as Python and Java to call OCR APIs to extract text from images. OCR allows you to automate the collection of key data. It helps you build an intelligent service system to improve efficiency.

To recognize characters in images or scanned paper documents, you can connect your applications to OCR. For instance, this can be used to extract text from ID card images uploaded by users to Huawei Cloud OBS.

Prerequisites

Billing

After connecting an application to the OCR connector, you must pay for the OCR service separately. For details, see Huawei Cloud Pricing. After an application is disabled, OCR is still billed based on the preset billing rules.

Creating an OCR Connector

  1. Log in to the application designer by referring to Logging In to the Application Designer.
  2. In the navigation pane, choose Integrations.
  3. Click Connector Instance under Connector.
  4. In the navigation pane on the left, choose AI > OCR.
  5. In the right pane, click + and set the parameters.

    Figure 1 Creating OCR
    Table 1 Parameters for creating an OCR connector

    Parameter

    Description

    Name

    Name of the connector to be created. The naming requirements are as follows:

    • The value cannot exceed 64 characters, including the prefix namespace.

      To prevent duplicate data names among different tenants, each tenant must define a unique namespace when first creating an application. A tenant can create only one namespace. After being created, the namespace cannot be modified.

    • Start with a letter and can contain only letters, digits, and underscores (_). Do not end with an underscore (_).

    Region

    Region where OCR is deployed.

    Access Key ID

    Access key ID is the unique identifier associated with a secret access key. The access key ID and secret access key are used together to obtain an encrypted signature for a request. Set this parameter to the value of AK in Prerequisites.

    Secret Access Key

    Secret access key is used together with the access key ID to encrypt the request, identify the sender, and prevent the request from being tempered. Set this parameter to the value of SK in Prerequisites.

    Package Current Configuration

    If this parameter is selected, the connector will be packed together with applications. For example, if an application package is released to the runtime environment, the current configuration is synchronized to the runtime environment by default. If this option is not selected, the connector will not be packaged and released to the runtime environment. In this case, you need to create the corresponding connector by referring to Managing Connectors in Applications.

    This parameter is selected by default. You are advised not to select this option in information-sensitive scenarios.

  6. Click Save.
  7. (Optional) Test whether ID card photos can be sent.

    1. Click Test on the connector details page.
    2. Set Choose Type to Image URL or Image File, enter an image URL or select an ID card image, and click Test.
      Figure 2 Configuring test ID card information

      The following information indicates successful recognition of the ID card image text.

      Figure 3 Successful test

Calling a Connector in a Script

Call the connector in the script to connect to OCR.

  1. Create an empty script by referring to Creating a Blank Script.
  2. In the script editor, enter the following code.

    Use the following code in your script to call the OCR API for ID card image recognition via URL and output basic information.
    //Import the standard library file on which the event depends. ocr is the standard library preset in the system.
    import * as ocr from 'ocr';
     
    let cli = ocr.newClient("OCR_Test");
    try {
        let url = "https://***"
        let resp = cli.idCardWithURL(url, "front");
        console.log(resp);
    } catch (e) {
        console.log("error: ", e)
    }

    In ocr.newClient ("OCR_Test"), OCR_Test indicates the connector name, url indicates the URL of the image, and front indicates that the front of the ID card is recognized.

  3. Click in the upper part of the script editor to save the script.
  4. Click to execute the script.
  5. Click in the upper right corner of the test window without setting input parameters.
  6. On the Output tab page, check whether the text on the ID card is recognized.

    If the text on the ID card is recognized, the connector is successfully called.

  7. Click in the upper part of the script editor to activate the script.

Calling a Connector in a Flow

Call the connector in a flow to connect to OCR.

  1. Create a blank flow by referring to Creating a Blank Flow.
  2. On the flow design page, choose Connectors > AI, and drag the OCR diagram element to the canvas.

    The OCR diagram element is preset in the system for connecting to OCR.

    Figure 4 Dragging the OCR diagram element to the canvas

  3. Click the widget, click , and set the OCR connection information.

    Figure 5 Setting basic information
    Table 2 Basic information parameters of the OCR diagram element

    Parameter

    Description

    Label

    Label of a diagram element displayed on the page.

    Name

    Unique ID of the diagram element in the system. The ID cannot be the same as that of other diagram elements. The naming requirements are as follows:

    • Enter up to 80 characters.
    • Start with a letter and can contain only letters, digits, and underscores (_). It cannot end with an underscore (_).

    Description

    Enter the description of the connector.

    Connector

    Select the target connector from the drop-down list, that is, the connector created in Creating an OCR Connector.

  4. Click and set action parameters.

    Figure 6 Configuring action parameters
    Table 3 Action parameters of the OCR diagram element

    Parameter

    Description

    Action

    Options:
    • id_card_with_url: Recognizes ID cards based on image URLs. Currently supports URLs that are temporarily authenticated or anonymously and publicly authenticated on OBS.
    • Id_card_with_image: Recognizes ID cards based on Base64-encoded image data. The encoded image size cannot exceed 10 MB. Image dimensions must be between 15 and 8,000 pixels per side. Supported formats include JPG, PNG, BMP, and TIFF.

    Input Parameters

    • When the Action is id_card_with_url, the input parameters are as follows:
      • If Target is set to url, set Source to the URL of the image. Currently, URLs that are temporarily authenticated or anonymously and publicly authenticated on OBS are supported.
      • If Target is set to side, set Source to front (front side of the ID card) or back (back side of the ID card).
    • When the Action is id_card_with_image, the input parameters are as follows:
      • If Target is set to image, set Source to the Base64-encoded image data. The encoded image size cannot exceed 10 MB. Image dimensions must be between 15 and 8,000 pixels per side. Supported formats include JPG, PNG, BMP, and TIFF.
      • If Target is set to side, set Source to front (front side of the ID card) or back (back side of the ID card).

    Output Parameters

    • If Source is set to result, Target is the result of a successful call. You must define a structure variable as described in Table 4, which outlines the member variables of the result structure.

      For details about how to create a variable in a flow, see Creating a Blank Flow.

    • If Source is set to error, Target is the error code for a failed call.
    Table 4 Member variables of the result structure

    Variable

    Type

    Description

    name

    String

    Name.

    gender

    String

    Gender.

    birth

    String

    Date of birth.

    ethnicity

    String

    Ethnicity.

    address

    String

    Address.

    number

    String

    ID card number.

    issue

    String

    Issuing authority.

    valid_from

    String

    Date of issue.

    valid_to

    String

    Date of expiry.

  5. Connect the start diagram element to the OCR diagram element.

    Figure 7 Connecting diagram elements

  6. Click to save the flow.
  7. Click to activate the flow.

    If the ID card recognition information is displayed, the connector is successfully called.

  8. Click to activate the flow.

Related Documents

After connecting to the OCR service through connectors, the OCR service can be used to identify all information in the ID card image uploaded by a user to Huawei OBS. For details, see Uploading and Recognizing ID Card Images with a Connector.