Web Image OCR

Function

Web Image OCR recognizes characters in a web image and returns the structured result in JSON format. For details about the constraints on using this API, see Constraints. For details about how to use this API, see Introduction to OCR.

Figure 1 Web Image OCR example

Prerequisites

Before using Web Image 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/web-image

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 a maximum of 8,192 pixels. The JPEG, JPG, PNG, BMP, TIFF, GIF, and WebP 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.

detect_direction

No

Boolean

Whether to enable the function of aligning tilted images. The options are as follows:

  • true: The tilted image is aligned.
  • false: The tilted image is not aligned.

An image tilted at any angle can be aligned. If this parameter is not specified, the default value false is used.

extract_type

No

Array of strings

Structured data extraction parameter list. Currently, only the contact information and image width and height are supported. The input parameter value of the contact information is contact_info. The input parameter value of the image width and height is image_size.

If this parameter is not set or is deleted, this parameter will not be used.

Response Parameters

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

Status code: 200

Table 4 Response body parameter

Parameter

Type

Description

result

WebImageResult object

Calling result of a successful API call

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

Table 5 WebImageResult

Parameter

Type

Description

words_block_count

Integer

Number of detected text blocks

words_block_list

Array of WebImageWordsBlockList objects

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

Table 6 WebImageWordsBlockList

Parameter

Type

Description

words

String

Recognition result of a text block

confidence

Number

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.

location

Array of integers

List of recognized location information about a text block, including the 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.

extracted_data

Object

Structured data extraction parameter list. Currently, only the contact information and image width and height are supported. The input parameter value of the contact information is contact_info. The input parameter value of the image width and height is image_size.

If extract_type is left blank or missing, no information is extracted.

contact_info

Object

Extracted contact information, including the name, phone number, province, city, and detailed address.

If extract_type does not contain this parameter, this parameter does not exist in the response.

image_size

Object

Width and height of an image

If extract_type does not contain this parameter, this parameter does not exist in the response.

height

Integer

Image height, which is returned when image_size is passed.

width

Integer

Image width, which is returned when image_size is passed.

name

String

Name, which is returned when contact_info is passed.

phone

String

Contact phone number, which is returned when contact_info is passed.

province

String

Province, which is returned when contact_info is passed.

city

String

City, which is returned when contact_info is passed.

district

String

County or district, which is returned when contact_info is passed.

detail_address

String

Detailed address (excluding the province, city, and county or district), which is returned when contact_info is passed.

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

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, Web Image OCR is deployed in the CN South-Guangzhou region. The endpoint is ocr.cn-south-1.myhuaweicloud.com. The request URL is https://ocr.cn-south-1.myhuaweicloud.com/v2/{project_id}/ocr/web-image. 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}/v2/{project_id}/ocr/web-image
    Request Header:
    Content-Type: application/json
    X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...
    
    Request Body:  
    {  
        "image":"/9j/4AAQSkZJRgABAgEASABIAAD/..." 
    }
  • Request example (Method 2: Use the image URL.)
    POST https://{endpoint}/v2/{project_id}/ocr/web-image
    Request Header:   
    Content-Type: application/json   
    X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...       
    Request Body:
    {
         "url":"https://BucketName.obs.xxxx.com/ObjectName"
    }
  • Sample code for a Python 3 request (For codes in other languages, refer to the following sample or use OCR SDK.)
    # encoding:utf-8
    
    import requests
    import base64
    
    url = "https://{endpoint}/v2/{project_id}/ocr/web-image"
    token = "Actual token value obtained by the user"
    headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}
    
    imagepath = r'./data/web-image-demo.png'
    with open(imagepath, "rb") as bin_data:
        image_data = bin_data.read()
    image_base64 = base64.b64encode(image_data).decode("utf-8")  # Base64 encoding of images.
    payload = {"image": image_base64} # url or image.
    response = requests.post(url, headers=headers, json=payload)
    print(response.text)

Example Response

Status code: 200

Successful response example

{ 
  "result": { 
      "words_block_count": 2, 
      "words_block_list": [ 
          { 
              "words": "Words recognized from the image",
              "confidence": 0.9950,
              "location": [ 
                  [13, 476], 
                  [91, 332], 
                  [125, 351], 
                  [48, 494] 
              ] 
          }, 
          { 
              "words": "Words recognized from the image",
              "confidence": 0.9910,
              "location": [ 
                  [13, 476], 
                  [91, 332], 
                  [125, 351], 
                  [48, 494] 
              ] 
          } 
      ],
      "extracted_data": {}
  } 
}

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.