Passport OCR
Function
Passport OCR recognizes the text on the first page of a passport and returns the structured information in JSON format.
In the current version, all fields of a Chinese passport can be recognized. For non-Chinese passports, two lines of internationally standardized machine-readable codes on the bottom of each passport can be recognized, and 6 to 7 key fields can be extracted from the codes.For details about the constraints on using this API, see Constraints. For details about how to use this API, see Introduction to OCR.

Prerequisites
Before using Passport 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/passport
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. |
project_id |
Yes |
Project ID, which can be obtained from Obtaining a Project ID. |
Request 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. |
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. No side of the image can be smaller than 15 or larger than 4,096 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:
NOTE:
|
country_code |
No |
String |
Country code on the passport. The recognition mode is determined based on the country code.
|
Response Parameters
Response parameters and status codes vary in different recognition results. They are described as below.
Status code: 200
Parameter |
Type |
Description |
---|---|---|
result |
PassportResult object |
Calling result of a successful API call result consists of the following three parts: 13 key fields, expressed in English; extra_info, passport information in the local official language; confidence, confidence scores of key fields. A higher confidence score indicates a more accurate result. This parameter is not included when the API fails to be called. |
Parameter |
Type |
Description |
---|---|---|
passport_type |
String |
Passport type. Possible values are as follows:
|
country_code |
String |
Country code |
passport_number |
String |
Passport ID |
nationality |
String |
Nationality of the passport holder |
surname |
String |
Last name |
given_name |
String |
Given game |
sex |
String |
Gender |
date_of_birth |
String |
Date of birth |
date_of_expiry |
String |
Passport date of expiry |
date_of_issue |
String |
Date of issue |
place_of_birth |
String |
Place of birth |
place_of_issue |
String |
Place of issue |
issuing_authority |
String |
Issuing authority The abbreviation of the issuing authority of each consulate is not unified. The abbreviation of Chinese issuing authority is P.R.China. For example, if the issuing authority is P.R.C, the recognition result is P.R.China. |
confidence |
Object |
Confidence information of a related field. The value ranges from 0 to 1. Confidence of related fields. A higher confidence indicates a more accurate result. The confidence is not equal to the accuracy, and is calculated through related algorithms. |
extra_info |
Object |
By default, this parameter is left blank. For a Chinese passport, extra_info contains Chinese character-described fields on the passport, such as the name and place of birth. |
Status code: 400
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, Passport OCR is deployed in the CN-Hong Kong region. The endpoint is ocr.ap-southeast-1.myhuaweicloud.com. The request URL is https://ocr.ap-southeast-1.myhuaweicloud.com/v2/{project_id}/ocr/passport. 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/passport Request Header: Content-Type: application/json X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG... Request Body: { "image":"/9j/4AAQSkZJRgABAgEASABIAAD/4RFZRXhpZgAATU0AKgAAAA...", "country_code": "GENERAL" }
- Request example (Method 2: Use the image URL.)
POST https://{endpoint}/v2/{project_id}/ocr/passport Request Header: Content-Type: application/json X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG... Request Body: { "url":"https://BucketName.obs.xxxx.com/ObjectName", "country_code": "GENERAL" }
- 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/passport" token = "Actual token value obtained by the user" headers = {'Content-Type': 'application/json', 'X-Auth-Token': token} imagepath = r'./data/passport-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
Chinese passport
{ "result": { "passport_type": "P", "country_code": "CHN", "passport_number": "ED999XXXX", "nationality": "CHINESE", "surname": "ZHANG", "given_name": "SAN", "sex": "F", "date_of_birth": "1990-12-12", "date_of_expiry": "2020-07-08", "date_of_issue": "2010-07-09", "place_of_birth": "HUNAN", "place_of_issue": "GUANGDONG", "issuing_authority": "MPS Exit & Entry Administration", "extra_info": { "local_language": { "name": "Name recognized from the image", "sex": "Gender recognized from the image", "place_of_birth": "Place of birth recognized from the image", "place_of_issue": "Place of issue recognized from the image", "issuing_authority": "Issuing authority recognized from the image", "nationality": "Nationality recognized from the image", } }, "confidence": { "passport_type": 1.0, "country_code": 1.0, "passport_number": 0.9997, "nationality": 1.0, "surname": 0.9729, "given_name": 0.9729, "sex": 1.0, "date_of_birth": 0.9998, "date_of_expiry": 0.9995, "date_of_issue": 0.9969, "place_of_birth": 1.0, "place_of_issue": 1.0, "issuing_authority": 0.9985 } } }
Non-chinese passport
{ "result": { "country_code": "ETF", "surname": "HUZHAO", "given_name": "ZHAOMIN DESALEGN ", "passport_number": "EP435XXXX", "date_of_birth": "1985-09-18", "sex": "M", "date_of_expiry": "2022-01-15", "machine_code": "P<ETFHUZHAO<< ZHAOMIN <DESALEGN<<<<<<<<<<<<<<<", "machine_code2": "EP435XXXX7ETF8509185M2201155<<<<<<<<<<<<<<08", "extra_info": {}, "confidence": { "country_code": 0.9727, "surname": 0.9727, "given_name": 0.9727, "passport_number": 0.9558, "date_of_birth": 0.9558, "sex": 0.9558, "date_of_expiry": 0.9558 } } }
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.
