Help Center > > API Reference> APIs> Thailand ID Card OCR

Thailand ID Card OCR

Updated at: Sep 30, 2021 GMT+08:00

Function

Thailand ID Card OCR recognizes characters on a Thailand-issued national registration card 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 Thailand ID Card OCR example

Prerequisites

Before using Thailand ID Card OCR, you need to apply for the service and complete authentication. For details, see Subscribing to OCR 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 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.

URI

POST https://{endpoint}/v2/{project_id}/ocr/thailand-id-card

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 details, see Endpoints.

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-encoded image file. The image 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 JPEG, JPG, PNG, BMP, or TIFF format can be recognized.

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.

side

No

String

  • front: front of an ID card
  • back: back of an ID card

If this parameter is left empty or not included, the system automatically recognizes whether the image is the front or back of an ID card. To ensure accuracy, set this parameter.

return_portrait_image

No

Boolean

Whether to return the portrait. Value options are as follows:

  • true: The Base64 code of the portrait on the ID card will be returned.
  • false: The Base64 code of the portrait on the ID card will not be returned.

If this parameter is not specified, false is used by default. In this case, the Base64 code of the portrait on the ID card will not be returned.

return_portrait_location

No

Boolean

Whether to return the location of the portrait on the ID card. Value options are as follows:

  • true: The location of the portrait on the ID card will be returned.
  • false: The location of the portrait on the ID card will not be returned.

return_idcard_type

No

Boolean

Whether to return the ID card type. Value options are as follows:

  • true: The ID card type will be returned, indicating that the ID card is the original ID card or copy of the original ID card.
  • false: The ID card type will not be returned.

Response Parameters

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

Status code: 200

Table 4 Response parameters

Parameter

Type

Description

result

Object

Calling result of a successful API call

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

Table 5 Response parameters

Parameter

Type

Description

side

String

Front or back of an ID card. Value options are front and back.

id_number

String

ID number

name_th

String

Thai name

first_name_en

String

English given name

last_name_en

String

English surname

date_of_birth_th

String

Date of birth, in Thai

date_of_birth_en

String

Date of birth, in English

religion_th

String

Religion

address_th

String

Address on the ID card

date_of_issue_th

String

Date of issue, in Thai

date_of_issue_en

String

Date of issue, in English

date_of_expiry_th

String

Date of expiry, in Thai

date_of_expiry_en

String

Date of expiry, in English

serial_number

String

Serial number

card_number

String

ID number on the back of the ID card

laser_number

String

Laser code

confidence

Object

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

A higher confidence indicates a higher accuracy of the field identified.

The confidence is calculated using algorithms and is not equal to the accuracy.

portrait_image

String

Base64 code of the portrait

This parameter is available only when return_portrait_image is set to true.

portrait_location

Array of objects

Location of the portrait on the original image

This parameter is available only when return_portrait_location is set to true. The image is displayed in a list. The list contains the two-dimensional coordinates (x,y) of the four vertices in the portrait area. The origin of the coordinates is the upper left corner of the image. The x axis is horizontal, and the y axis is vertical.

idcard_type

String

ID card type. Value options are as follows:

  • normal: original ID card
  • copy: copy of the ID card

This parameter is available only when return_idcard_type is set to true.

Status code: 200

Table 6 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, Thailand ID Card OCR is deployed in the AP-Bangkok region. The endpoint is ocr.ap-southeast-2.myhuaweicloud.com. The request URL is https://ocr.ap-southeast-2.myhuaweicloud.com/v2/{project_id}/thailand-id-card. 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/thailand-id-card
    
    Request Header:
    Content-Type: application/json
    X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...
    Request Body: 
    {
        "image":"/9j/4AAQSkZJRgABAgEASABIAAD/4RFZRXhpZgAATU0AKgAAAAgABwESAAMAA...",
        "side":"front",
        "return_portrait_image":true,
        "return_portrait_location ":true,
        "return_idcard_type":true
    }
  • Request example (Method 2: Use the image URL.)
    POST  https://{endpoint}/v2/{project_id}/ocr/thailand-id-card
    
    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/thailand-id-card"
    token = "Actual token value obtained by the user"
    headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}
    
    imagepath = r'./data/thailand-id-card-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 (the front)

{
    "result": {
        "side": "front",
        "id_number": "X XXXX XXXXX XX X", 
        "name_th": "XXX", 
        "first_name_en": "XX", 
        "last_name_en": "XX", 
        "date_of_birth_th": "5 ก.พ. 2493", 
        "date_of_birth_en": "5 Feb. 1950", 
        "religion_th": "XX", 
        "address_th": "XXXXX", 
        "date_of_issue_th": "XX", 
        "date_of_issue_en": "4 Mar. 2011", 
        "date_of_expiry_th": "22 ก.พ. 2561", 
        "date_of_expiry_en": "22 Feb. 2018", 
        "serial_number": "XXXX-XX-XXXXX",
        "confidence": {
            "id_number": 0.9999, 
            "name_th": 0.9994, 
            "first_name_en": 0.998, 
            "last_name_en": 0.9997, 
            "date_of_birth_th": 0.9996, 
            "date_of_birth_en": 0.9997, 
            "religion_th": 0.686, 
            "address_th": 0.624, 
            "date_of_issue_th": 0.9567, 
            "date_of_issue_en": 0.9399, 
            "date_of_expiry_th": 0.9969, 
            "date_of_expiry_en": 0.61, 
            "serial_number": 0.9887
        },
        "portrait_image": "/9j/4AA... ",
        "portrait_location": [
            [576, 237],
            [741, 237],
            [739, 430],
            [574, 431]
        ],
        "idcard_type": "normal"
    }
}

Successful response example (the back)

{
    "result": {
        "side": "back",
        "card_number": "XXXX-XXX-XX", 
        "laser_number": "XXXX-XXXXXXX-XX", 
        "confidence": {
            "id_number": 0.9999, 
            "laser_number": 0.9994
        }
    }
}

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.

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel