Updated on 2024-12-27 GMT+08:00

Smart Document Analysis

Function

This API recognizes text, analyzes layout, extracts key-value pairs, identifies tables in various formatted documents such as certificates, receipts, and forms, and converts the results into a structured JSON format.

Notes and Constraints

  • English, Chinese, and some traditional Chinese characters are supported.
  • Only images in PNG, JPG, JPEG, BMP, GIF, TIFF, WebP, PCX, ICO or PSD format and PDF files can be recognized. PDF files can only be recognized one page at a time, but you can use the pdf_page_number parameter to specify which page you want to recognize.
  • No side of the image can be smaller than 15 or larger than 8,192 pixels.
  • The area to be recognized must occupy more than 80% of the image. When scanning a table, ensure that all text and its surrounding area are included in the image.
  • An image can be rotated to any angle.
  • For more accurate recognition results, the number of characters on a single page must be limited to 1,800 or less.
  • Text in images with complex backgrounds (such as outdoor scenery or anti-counterfeit watermarks) or distorted text cannot be analyzed.
  • OCR is a public cloud service whose resources are sharable to all online users. If you need to call multiple APIs concurrently, contact us.

Calling Method

For details, see Calling APIs.

Prerequisites

Before using this API, subscribe to the service and complete authentication. For details, see Subscribing to an OCR Service and Authentication.

Before using the service for the first time, you need to enable the service by clicking Subscribe. You only need to subscribe to the service once. If you have not subscribed to the service yet, error "ModelArts.4204" will be displayed when you call this API. Before you call the API, log in to the OCR console and subscribe to the corresponding service. Ensure that you make the subscription to the service in the same region where you want to call this API.

URI

POST /v2/{project_id}/ocr/smart-document-recognizer

Table 1 URI parameter

Parameter

Mandatory

Type

Description

project_id

Yes

String

Project ID, which can be obtained by referring to Obtaining a Project ID.

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

User token. During API authentication using a token, the token is added to requests to obtain permissions for calling the API. The value of X-Subject-Token in the response header is the obtained token.

Enterprise-Project-Id

No

String

Enterprise project ID. OCR allows you to use Enterprise Project Management Service (EPS) to split fees for resources used by different user groups and users. To obtain the enterprise project ID, go to the Enterprise Project Management console, click the enterprise project name, and obtain the enterprise project ID on the enterprise project details page. For details about how to create an enterprise project, see Optical Character Recognition User Guide.

NOTE:

After an enterprise project is created, parameter transfer involves the following scenarios:

  • If a correct enterprise project ID is carried and the OCR service can be used properly, the bills will be categorized under the corresponding enterprise project for that ID.
  • If an enterprise project ID that is in the correct format but does not actually exist is carried, and the OCR service can be used properly, the bills will display the corresponding non-existent enterprise project ID.
  • If no enterprise project ID or an enterprise project ID with incorrect format (such as special characters) is carried, and the OCR service can be used properly, the bills will be categorized under default.
Table 3 Request body parameters

Parameter

Mandatory

Type

Description

data

No

String

Set either this parameter or url. Base64 encoded string of the image or PDF. The file has a size limit of 10 MB. No side of the image can be smaller than 15 or larger than 8,192 pixels. Only images in JPG, PNG, BMP, or TIFF format can be recognized. PDFs are converted to images with a resolution of 144 dpi for document analysis, and they must meet the image size requirements mentioned above. If a PDF has multiple pages, only the first page will be recognized.

url

No

String

Set either this parameter or data. URL of an image or PDF. Currently, the following URLs are supported:

  • Public HTTP/HTTPS URL
  • URL provided by OBS. You need to be authorized to use OBS data, including service authorization, temporary authorization, and anonymous public authorization. For details, see Configuring Access Permissions of OBS.
NOTE:
  • The API response time depends on the image download time. If the image download takes a long time, the API call will fail.
  • Ensure that the storage service where the image to be detected resides is stable and reliable. OBS is recommended for storing image data.
  • The URL cannot contain Chinese characters. If Chinese characters exist, they must be encoded using UTF-8.

single_orientation_mode

No

Boolean

Whether to enable the single direction mode. The options are:

  • true: The single direction mode is enabled.
  • false: The single direction mode is disabled.

Enabling this function when text in the image is oriented uniformly improves recognition accuracy. Disabling it when text in the image varies in direction allows for multi-direction text recognition. If not specified, true is used by default. In this case, the fields in the image are recognized as in a single direction by default.

language

No

String

Language. If this parameter is not specified, Chinese and English will be used by default. For details, see Languages Supported by Huawei Cloud General Text OCR.

kv

No

Boolean

Whether to extract key-value pairs. If you choose to extract key-value pairs, the results will be returned with the keyword kv_result.

table

No

Boolean

Whether to recognize tables. Here, tables refer to logical tables that typically have an M x N format and have a header in the first row or column. If you choose to recognize tables, the results will be returned with the keyword table_result.

layout

No

Boolean

Whether to analyze the layout. If you choose to analyze the layout, the results will be returned with the keyword layout_result.

return_excel

No

Boolean

This parameter is available only when table is set to True. Whether to return the Base64-encoded field for converting a table into a Microsoft Excel file.

form

No

Boolean

Whether to recognize wired forms. A wired form displays crucial information in wired cells, like household registers and motor vehicle sales invoices. If you choose to recognize wired forms, the results will be returned with the keyword form_result.

formula

No

Boolean

Whether to recognize formulas. The results are returned as a LaTeX sequence. If you choose to recognize formulas, the results will be returned with the keyword formula_result.

  • Enabling formula recognition may slow down the response speed.
  • Currently, formula recognition is supported for up to three lines only. Formulas exceeding three lines are not supported.

kv_map

No

String

JSON-serialized string of a dictionary that needs to be passed in, which is used to normalize and map specific key values in kv_result. For example, if kv_result contains the key-value pair {"Name": "Xiaoming"}, passing in the kv_map {"Name": "Full name"} would result in {"Full Name": "Xiaoming"}.

NOTE:

Example:

  • "kv_map":"{"Name":"Full name"}"

erase_seal

No

Boolean

Whether to erase the seal. Enabling it can enhance the character recognition accuracy in the area blocked by the seal.

pdf_page_number

No

Integer

Specify which page of the PDF file you want to recognize. If this parameter is specified, the content on the specified page is identified. If this parameter is not specified, the default is to recognize the first page.

Response Parameters

Status code: 200

Table 4 Response body parameter

Parameter

Type

Description

result

Array of SmartDocumentRecognizerResult objects

List of results returned in the order of the pages, with the first item in the list being the recognition result of the first page, and so on. This parameter is not included for a failed call.

Table 5 SmartDocumentRecognizerResult

Parameter

Type

Description

ocr_result

SmartDocumentRecognizerOcrResult object

Character recognition results

kv_result

SmartDocumentRecognizerKvResult object

Key-value pair extraction results. This parameter is returned only when kv is set to true.

table_result

SmartDocumentRecognizerTableResult object

Table recognition results. This parameter is returned only when table is set to true.

layout_result

SmartDocumentRecognizerLayoutResult object

Layout analysis results. This parameter is returned only when layout is set to true.

form_result

SmartDocumentRecognizerFormResult object

Wired form recognition results. This parameter is returned only when form is set to true.

formula_result

SmartDocumentRecognizerFormulaResult object

Formula recognition result

Table 6 SmartDocumentRecognizerOcrResult

Parameter

Type

Description

direction

Float

Image direction

words_block_count

Integer

Number of text blocks that have been recognized

words_block_list

Array of SmartDocumentRecognizerWordsBlockList objects

List of text blocks that have been recognized. The output sequence is from left to right and from top to bottom.

Table 7 SmartDocumentRecognizerWordsBlockList

Parameter

Type

Description

words

String

Text block recognition results

location

Array<Array<Integer>>

List of location information about a text block, including the 2D coordinates (x, y) of four vertexes in the text area, where the coordinate origin is the upper-left corner of the image, the X axis is horizontal, and the Y axis is vertical.

confidence

Float

Confidence of a recognized text block

Table 8 SmartDocumentRecognizerKvResult

Parameter

Type

Description

kv_block_count

Integer

Number of key-value pairs recognized by the model

kv_block_list

Array of SmartDocumentRecognizerKVBlock objects

List of key-value pair recognition results

Table 9 SmartDocumentRecognizerKVBlock

Parameter

Type

Description

key

String

Key in a key-value pair, for example, Name in Name: Xiaoming.

value

String

Value in a key-value pair, for example, Xiaoming in Name: Xiaoming.

words_block_count

Integer

Number of text boxes contained in the key-value pair

words_block_list

Array of SmartDocumentRecognizerKVWordsBlock objects

List of text box recognition results

Table 10 SmartDocumentRecognizerKVWordsBlock

Parameter

Type

Description

words

String

Text block recognition results

location

Array<Array<Integer>>

List of location information about a text block, including the 2D coordinates (x, y) of four vertexes in the text area, where the coordinate origin is the upper-left corner of the image, the X axis is horizontal, and the Y axis is vertical.

type

String

Type

Table 11 SmartDocumentRecognizerTableResult

Parameter

Type

Description

table_count

Integer

Number of tables recognized by the model

table_list

Array of SmartDocumentRecognizerTableBlock objects

List of table recognition results

Table 12 SmartDocumentRecognizerLayoutResult

Parameter

Type

Description

layout_block_count

Integer

Number of document layout areas recognized by the model

layout_block_list

Array of SmartDocumentRecognizerLayoutBlock objects

List of document layout area recognition results

Table 13 SmartDocumentRecognizerLayoutBlock

Parameter

Type

Description

location

Array<Array<Integer>>

List of location information about a text block, including the 2D coordinates (x, y) of four vertexes in the text area, where the coordinate origin is the upper-left corner of the image, the X axis is horizontal, and the Y axis is vertical.

type

String

Document area type, including text, title, sub_title, image, image_caption, form, table, table_caption, header, footer, page_number, reference, formula, stamp, and directory.

text

String

Text in the document area. For tables and images, the text content is not returned.

words_ids

Array of integers

Index list of character recognition results, indicating which text blocks in words_block_list of ocr_result are located within the document area.

table_id

Integer

This parameter is returned only when type is table and the input parameter table is True, indicating which recognition result corresponds to the current logical table area in table_result.

form_id

Integer

This parameter is returned only when type is form and the input parameter table is True, indicating which recognition result corresponds to the current wired form area in form_result.

Table 14 SmartDocumentRecognizerFormResult

Parameter

Type

Description

form_count

Integer

Number of wired forms recognized by the model

form_list

Array of SmartDocumentRecognizerTableBlock objects

List of wired form recognition results

Table 15 SmartDocumentRecognizerTableBlock

Parameter

Type

Description

location

Array<Array<Integer>>

Location information of the current table, in list format, indicating the X and Y coordinates of the four vertices in a text block. The coordinate origin is the upper left corner of the image, the X axis is horizontal, and the Y axis is vertical.

words_block_count

Integer

Number of cells in a table

words_block_list

Array of SmartDocumentRecognizerTableWordsBlock objects

List of cell recognition results

excel

String

Base64 encoded string of the table recognition results. This parameter is returned only when return_excel is set to True. Decode the returned code using base64.b64decode and save it as an .xlsx file.

Table 16 SmartDocumentRecognizerTableWordsBlock

Parameter

Type

Description

words

String

Character recognition results in a cell

rows

Array of integers

Rows occupied by text. The values start from 0 and are displayed in a list. The data type is Integer.

columns

Array of integers

Columns occupied by text. The values start from 0 and are displayed in a list. The data type is Integer.

Table 17 SmartDocumentRecognizerFormulaResult

Parameter

Type

Description

formula_count

Integer

Number of mathematical formulas

formula_list

Array of SmartDocumentRecognizerFormulaBlock objects

List of mathematical formula recognition results

Table 18 SmartDocumentRecognizerFormulaBlock

Parameter

Type

Description

formula

String

Mathematical formula recognition results, which are represented as LaTeX strings

location

Array<Array<Integer>>

Mathematical formula location information, in list format, indicating the X and Y coordinates of the four vertices. The coordinate origin is the upper left corner of the image and has a horizontal X axis and vertical Y axis.

Status code: 400

Table 19 Response body parameters

Parameter

Type

Description

error_code

String

Error code of a failed API call. For details, see Error Codes.

This parameter is not returned for a successful call.

error_msg

String

Error message when the API call fails. This parameter is not included when the API is successfully called.

Example Request

  • Transfer the Base64 encoded string of the document image for recognition.
    POST https://{endpoint}/v2/{project_id}/ocr/smart-document-recognizer 
      
     { 
       "data" : "/9j/4AAQSkZJRgABAgEASABIAAD/4RFZRXhpZgAATU0AKgAAAA..." 
     }
  • Transfer the URL of the document image for recognition.
    POST https://{endpoint}/v2/{project_id}/ocr/smart-document-recognizer 
      
     { 
       "url" : "https://BucketName.obs.myhuaweicloud.com/ObjectName" 
     }

Example Response

Status code: 200

Example response for a successful request

{
  "result" : [ {
    "formula_result" : {
      "formula_count" : 1,
      "formula_list" : [ {
        "formula" : "\\\\int _ { L } \\\\left ( 2 x y ^ { 3 } - y ^ { 2 } \\\\cos x \\\\right ) \\\\mathrm { d } x + \\\\left ( 1 - 2 y \\\\sin x + 3 x ^ { 2 } y ^ { 2 } \\\\right ) \\\\mathrm { d } y",
        "location" : [ [ 171, 919 ], [ 950, 919 ], [ 950, 967 ], [ 171, 967 ] ]
      } ]
    }
  }, {
    "layout_result" : {
      "layout_block_count" : 19,
      "layout_block_list" : [ {
        "location" : [ [ 1165, 368 ], [ 2031, 368 ], [ 2031, 465 ], [ 1165, 465 ] ],
        "type" : "title",
        "text" : "Heilongjiang VAT Special Invoice",
        "words_ids" : [ 0 ]
      }, {
        "location" : [ [ 15, 19 ], [ 1078, 19 ], [ 1078, 637 ], [ 15, 637 ] ],
        "type" : "form",
        "text" : "xxxx",
        "words_ids" : [ 2, 3, 4 ],
        "form_id" : 0
      }, {
        "location" : [ [ 18, 180 ], [ 1077, 180 ], [ 1077, 636 ], [ 18, 636 ] ],
        "type" : "table",
        "text" : "xxxx",
        "words_ids" : [ 0, 1, 2 ],
        "table_id" : 0
      } ]
    }
  }, {
    "form_result" : {
      "form_count" : 1,
      "form_list" : [ {
        "location" : [ [ 15, 19 ], [ 1074, 19 ], [ 1074, 636 ], [ 15, 636 ] ],
        "words_block_count" : 24,
        "words_block_list" : [ {
          "words" : "xxx",
          "rows" : [ 0 ],
          "columns" : [ 0, 1, 2 ]
        }, {
          "words" : "xxxx",
          "rows" : [ 1 ],
          "columns" : [ 0, 1, 2 ]
        } ],
        "excel" : "UEsDBBQAAAAIAAAAIQBhXUk6TwEAAI8EAAATAAAAW0NvbnRlbnRfVHlwZX..."
      } ]
    }
  }, {
    "table_result" : {
      "table_count" : 1,
      "table_list" : [ {
        "words_block_count" : 24,
        "words_block_list" : [ {
          "words" : "Name of goods or taxable labor services",
          "rows" : [ 0 ],
          "columns" : [ 0 ]
        }, {
          "words" : "Specifications and model",
          "rows" : [ 0 ],
          "columns" : [ 1 ]
        } ],
        "excel" : "xxxx",
        "location" : [ [ 275, 967 ], [ 2919, 967 ], [ 2919, 1177 ], [ 275, 1177 ] ]
      } ]
    }
  }, {
    "kv_result" : {
      "kv_block_count" : 25,
      "kv_block_list" : [ {
        "key" : "Invoice issuance date",
        "value" : "August 31, 2017",
        "words_block_count" : 2,
        "words_block_list" : [ {
          "words" : "Invoice issuance date",
          "location" : [ [ 2241, 589 ], [ 2480, 592 ], [ 2480, 646 ], [ 2241, 643 ] ],
          "type" : "key"
        }, {
          "words" : "August 31, 2017",
          "location" : [ [ 2479, 591 ], [ 2850, 595 ], [ 2850, 649 ], [ 2479, 645 ] ],
          "type" : "value"
        } ]
      } ]
    }
  }, {
    "ocr_result" : {
      "direction" : 0.4767,
      "words_block_count" : 67,
      "words_block_list" : [ {
        "words" : "Heilongjiang VAT Special Invoice",
        "location" : [ [ 430, 100 ], [ 874, 99 ], [ 874, 139 ], [ 430, 141 ] ],
        "confidence" : 0.9552
      } ]
    }
  } ]
}

Status code: 400

Example response for a failed request

{
  "error_code" : "AIS.0103",
  "error_msg" : "The image size does not meet the requirements."
}

Example SDK Code

The example SDK code is as follows:

You are advised to update the SDKs to the latest versions before use to prevent the local outdated SDKs from being unable to use the latest OCR functions.

  • Transfer the Base64 encoded string of the document image for recognition.
     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
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.ocr.v1.region.OcrRegion;
    import com.huaweicloud.sdk.ocr.v1.*;
    import com.huaweicloud.sdk.ocr.v1.model.*;
    
    
    public class RecognizeSmartDocumentRecognizerSolution {
    
        public static void main(String[] args) {
            // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
            // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
            String ak = System.getenv("CLOUD_SDK_AK");
            String sk = System.getenv("CLOUD_SDK_SK");
    
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
    
            OcrClient client = OcrClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(OcrRegion.valueOf("<YOUR REGION>"))
                    .build();
            RecognizeSmartDocumentRecognizerRequest request = new RecognizeSmartDocumentRecognizerRequest();
            SmartDocumentRecognizerRequestBody body = new SmartDocumentRecognizerRequestBody();
            body.withData("/9j/4AAQSkZJRgABAgEASABIAAD/4RFZRXhpZgAATU0AKgAAAA...");
            request.withBody(body);
            try {
                RecognizeSmartDocumentRecognizerResponse response = client.recognizeSmartDocumentRecognizer(request);
                System.out.println(response.toString());
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                e.printStackTrace();
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getRequestId());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }
    
  • Transfer the URL of the document image for recognition.
     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
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.ocr.v1.region.OcrRegion;
    import com.huaweicloud.sdk.ocr.v1.*;
    import com.huaweicloud.sdk.ocr.v1.model.*;
    
    
    public class RecognizeSmartDocumentRecognizerSolution {
    
        public static void main(String[] args) {
            // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
            // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
            String ak = System.getenv("CLOUD_SDK_AK");
            String sk = System.getenv("CLOUD_SDK_SK");
    
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
    
            OcrClient client = OcrClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(OcrRegion.valueOf("<YOUR REGION>"))
                    .build();
            RecognizeSmartDocumentRecognizerRequest request = new RecognizeSmartDocumentRecognizerRequest();
            SmartDocumentRecognizerRequestBody body = new SmartDocumentRecognizerRequestBody();
            body.withUrl("https://BucketName.obs.myhuaweicloud.com/ObjectName");
            request.withBody(body);
            try {
                RecognizeSmartDocumentRecognizerResponse response = client.recognizeSmartDocumentRecognizer(request);
                System.out.println(response.toString());
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                e.printStackTrace();
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getRequestId());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }
    
  • Transfer the Base64 encoded string of the document image for recognition.
     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
    # coding: utf-8
    
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkocr.v1.region.ocr_region import OcrRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkocr.v1 import *
    
    if __name__ == "__main__":
        # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak = os.getenv("CLOUD_SDK_AK")
        sk = os.getenv("CLOUD_SDK_SK")
    
        credentials = BasicCredentials(ak, sk) \
    
        client = OcrClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(OcrRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = RecognizeSmartDocumentRecognizerRequest()
            request.body = SmartDocumentRecognizerRequestBody(
                data="/9j/4AAQSkZJRgABAgEASABIAAD/4RFZRXhpZgAATU0AKgAAAA..."
            )
            response = client.recognize_smart_document_recognizer(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • Transfer the URL of the document image for recognition.
     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
    # coding: utf-8
    
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkocr.v1.region.ocr_region import OcrRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkocr.v1 import *
    
    if __name__ == "__main__":
        # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak = os.getenv("CLOUD_SDK_AK")
        sk = os.getenv("CLOUD_SDK_SK")
    
        credentials = BasicCredentials(ak, sk) \
    
        client = OcrClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(OcrRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = RecognizeSmartDocumentRecognizerRequest()
            request.body = SmartDocumentRecognizerRequestBody(
                url="https://BucketName.obs.myhuaweicloud.com/ObjectName"
            )
            response = client.recognize_smart_document_recognizer(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • Transfer the Base64 encoded string of the document image for recognition.
     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
    37
    38
    39
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        ocr "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1/region"
    )
    
    func main() {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := ocr.NewOcrClient(
            ocr.OcrClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.RecognizeSmartDocumentRecognizerRequest{}
    	dataSmartDocumentRecognizerRequestBody:= "/9j/4AAQSkZJRgABAgEASABIAAD/4RFZRXhpZgAATU0AKgAAAA..."
    	request.Body = &model.SmartDocumentRecognizerRequestBody{
    		Data: &dataSmartDocumentRecognizerRequestBody,
    	}
    	response, err := client.RecognizeSmartDocumentRecognizer(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • Transfer the URL of the document image for recognition.
     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
    37
    38
    39
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        ocr "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1/region"
    )
    
    func main() {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := ocr.NewOcrClient(
            ocr.OcrClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.RecognizeSmartDocumentRecognizerRequest{}
    	urlSmartDocumentRecognizerRequestBody:= "https://BucketName.obs.myhuaweicloud.com/ObjectName"
    	request.Body = &model.SmartDocumentRecognizerRequestBody{
    		Url: &urlSmartDocumentRecognizerRequestBody,
    	}
    	response, err := client.RecognizeSmartDocumentRecognizer(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    

For more SDK code examples in various programming languages, see the Sample Code tab on the right of the API Explorer page, which can automatically generate corresponding SDK code examples.

Status Codes

Status Code

Description

200

Example response for a successful request

400

Example response for a failed request

See Status Codes.

Error Codes

See Error Codes.