Auto Classification OCR

Function

Auto Classification OCR detects and locates the documents (tickets, certificates, or other text carriers) specified on an image and returns the structured information in JSON format.

The API returns the position coordinates of the documents, recognized structured information, and the corresponding category in a list. For details about the ticket types supported by this API, see Table 4.For details about the constraints on using this API, see Constraints. For details about how to use this API, see Introduction to OCR.

Only the documents that are successfully recognized are charged. For example, if an image contains three documents, two are recognized successfully, and the remaining one fails to be recognized, the API is charged twice.

Prerequisites

Before using Auto Classification OCR, you need to apply for the service and complete authentication. For details, see Subscribing to OCR and Authentication.

URI

POST https://{endpoint}/v2/{project_id}/ocr/auto-classification

Table 1 Path parameters

Parameter

Mandatory

Description

endpoint

Yes

Domain name or IP address of the server bearing the REST service endpoint. The endpoint varies depending on services in different regions. For more details, see Endpoints.

For example, the endpoint of OCR in the CN North-Beijing4 region is ocr.cn-north-4.myhuaweicloud.com.

project_id

Yes

Project ID, which can be obtained from 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.

Content-Type

Yes

String

MIME type of the request body. The value is application/json.

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

image

No. Set either this parameter or url.

String

Base64 character string converted from the image. The size cannot exceed 10 MB.

The narrow edge contains at least 15 pixels and the wide edge contains at most 8,000 pixels. The JPEG, JPG, PNG, BMP, and TIFF formats are supported.

url

No. Set either this parameter or image.

String

Image URL. Currently, the following URLs are supported:

  • Public network: 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 images to be detected reside is stable and reliable. OBS is recommended for storing image data.

type_list

No

List

Documents to be identified. The documents that are not in the list cannot be identified. If this parameter is not specified, the identification information about all types of supported documents is returned by default.

Table 4 describes the document types supported by the current version.

Table 4 Supported document types

Type

Value

Front side of an ID card (with the photo)

id_card_portrait_side

Back side of an ID card (with the national emblem)

id_card_emblem_side

Primary page of a vehicle license

vehicle_license_front

Secondary page of a vehicle license

vehicle_license_back

Primary page of a driving license

driver_license_front

Secondary page of a driving license

driver_license_back

Passport

passport

Bank card

bankcard

Road transport license

transportation_license

VAT invoice

vat_invoice

General VAT invoice (roll invoice)

vat_roll_invoice

Motor vehicle sales invoice

mvs_invoice

Taxi Invoice

taxi_invoice

Train ticket

train_ticket

Quota invoice

quota_invoice

Toll invoice

toll_invoice

Flight itinerary

flight_itinerary

Response Parameters

Response parameters and status codes vary in different recognition results. They are described as below.

Status code: 200

Table 5 Response body parameter

Parameter

Type

Description

result

AutoClassificationResult object

Calling result of a successful API call

This parameter is not included when the API fails to be called.

Table 6 AutoClassificationResult

Parameter

Type

Description

status

Object

Identification status of documents

content

Object

Structured identification result of documents

type

String

Documents type

location

Array of integers

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

confidence

Object

Confidence information of a related field. The value ranges from 0 to 1.

A higher confidence level indicates a higher reliability and accuracy of the corresponding field identified.

The confidence is not equal to the accuracy, and is calculated through related algorithms.

Status code: 400

Table 7 Response body parameters

Parameter

Type

Description

error_code

String

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

If error code ModelArts.4204 is displayed, refer to Why Is a Message Stating "ModelArts.4204" Displayed When the OCR API Is Called?

This parameter is not included when the API is successfully called.

error_msg

String

Error message returned when the API fails to be called

This parameter is not included when the API is successfully called.

Request Example

  • The endpoint is the request URL for calling an API. Endpoints vary depending on services and regions. For details, see Endpoints.

    For example, Auto Classification OCR is deployed in the CN North-Beijing4 region. The endpoint is ocr.cn-north-4.myhuaweicloud.com. The request URL is https://ocr.cn-north-4.myhuaweicloud.com/v2/{project_id}/ocr/auto-classification. project_id is the project ID. For details about how to obtain the project ID, see Obtaining a Project ID.

  • For details about how to obtain a token, see Making an API Request.
  • Request example (Method 1: Use the image Base64 string.)
    POST https://{endpoint}/v1.0/ocr/auto-classification
    Request Header:
    Content-Type: application/json   
    X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...
    Request Body: 
    {
       "image":"/9j/4AAQSkZJRgABAgEASABIAAD/4RFZRXhpZgAATU0AKgAAAA..."
    }
  • Request example (Method 2: Use the image URL.)
    POST https://{endpoint}/v2/{project_id}/ocr/auto-classification
    Request Header:
    Content-Type: application/json   
    X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...
    Request Body: 
    {
       "url":"https://BucketName.obs.xxxx.com/ObjectName"
    }

Example Response

Status code: 200

Successful response example

{
    "result": [
        {
            "status": {
                "error_code": "AIS.0000", 
                "error_msg": "Succeeded."
            }, 
            "type": "quota_invoice", 
            "location": [
                [65, 1006], 
                [1388, 1006], 
                [1388, 1863], 
                [65, 1863]
            ],
            "content": {               
                "code": "13XXXXXX1301",
                "number": "40XXXX88",
                "amount": "Amount recognized from the image",
                "location": "Location recognized from the image",
                "confidence": {                    
                    "code": 0.9253, 
                    "number": 0.9665,
                    "amount": 0.9848,
                    "location": 0.9847 
                }                
            }
        },
        {
            "status": {
                "error_code": "AIS.0000", 
                "error_msg": "Succeeded."
            }, 
            "type": "quota_invoice", 
            "location": [
                 [621, 605], 
                 [1328, 605], 
                 [1328, 1094], 
                 [621, 1094]
            ],     
            "content": {               
                "code": "13XXXXXX1301",
                "number": "40XXXX00",
                "amount": "Amount recognized from the image",
                "location": "Location recognized from the image",
                "confidence": {                    
                    "code": 0.9556, 
                    "number": 0.9455,
                    "amount": 0.9917,
                    "location": 0.9347 
                }                
            }
        },
        {
            "status": {
                "error_code": "AIS.0104", 
                "error_msg": "The image is not supported or the image quality is poor."
            }, 
            "type": "quota_invoice", 
            "location": [
                [816, 103], 
                [2125, 103], 
                [2125, 978], 
                [816, 978]
            ],
            "content": {}
        }
    ]
}

Status code: 400

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

Status Codes

Status Code

Description

200

Success response

400

Failure response

For details about status codes, see Status Codes.

Error Codes

For details about error codes, see Error Codes.