Hong Kong ID Card
Function
This API identifies and extracts text from images of Hong Kong-issued ID cards and converts the text into a structured format. For details about the constraints on using this API, see Constraints. For details about how to use this API, see Introduction to OCR.
Constraints
- Only images in PNG, JPG, BMP, or TIFF format can be recognized.
- No side of the image can be smaller than 15 or larger than 8,192 pixels.
- An ID card to be recognized must occupy more than 50% of the image. When scanning an ID card, ensure that the entire ID card is displayed in the image.
- Images can be rotated horizontally to any angle, but the recognition precision is affected.
- Illuminated or dark images can be recognized, but the accuracy may be compromised.
Prerequisites
Before using Hong Kong ID Card OCR, you need to apply for 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 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.
Debugging
You can debug this API through automatic authentication in API Explorer. API Explorer can automatically generate and debug sample SDK code.

API Explorer can be called in ap-southeast-1.
URI
POST https://{endpoint}/v2/{project_id}/ocr/hk-id-card
Parameter |
Mandatory |
Description |
---|---|---|
endpoint |
Yes |
Endpoint, which is the request address for calling an API. 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 Used to obtain the permission to operate APIs. The token is the value of X-Subject-Token in the response header in Authentication. |
Content-Type |
Yes |
String |
MIME type of the request body. The value is application/json. |
Enterprise-Project-Id |
No |
String |
Enterprise project ID. OCR uses 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 page, 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:
|
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
image |
No |
String |
Configure either this parameter or url. 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 JPG, PNG, BMP, or TIFF format can be recognized. An example is /9j/4AAQSkZJRgABAg.... If the image data contains an unnecessary prefix, the error "The image format is not supported" is reported. |
url |
No |
String |
Configure either this parameter or image. Image URL. Currently, the following URLs are supported:
NOTE:
|
return_portrait_image |
No |
Boolean |
Whether to return the portrait. Value options are as follows:
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:
If this parameter is not specified, false is used by default. In this case, the location of the portrait on the ID card will not be returned. |
Response Parameters
Status code: 200
Parameter |
Type |
Description |
---|---|---|
result |
HkIdCardResult object |
Calling result of a successful API call This parameter is not included when the API fails to be called. |
Parameter |
Type |
Description |
---|---|---|
name |
String |
Chinese name |
name_en |
String |
English name |
sex |
String |
Gender M: male; F: female |
birth_date |
String |
Date of birth |
number |
String |
ID card number |
symbols |
String |
Symbol on the ID card |
name_telegraph_code |
String |
Name in Chinese telegraph code |
permanent |
Boolean |
Whether the ID card is a Hong Kong permanent ID card. Value options are as follows: true: permanent; false: non-permanent |
initial_issue_date |
String |
Date of first issue |
issue_date |
String |
Date of issue |
portrait_location |
Array<Array<Integer>> |
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. |
portrait_image |
String |
Base64-encoded profile image. The profile with a larger size is returned by default. This parameter is available only when return_portrait_image is set to true. |
confidence |
Object |
Confidence of each field |
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 returned when the API is successfully called. |
error_msg |
String |
Error message when the API call fails This parameter is not returned when the API is successfully called. |
Example Requests
- Request example (Method 1: Use the image Base64 string.)
POST https://ocr.ap-southeast-1.myhuaweicloud.com/v2/<project_id>/ocr/hk-id-card 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://ocr.ap-southeast-1.myhuaweicloud.com/v2/<project_id>/ocr/hk-id-card Request Header: Content-Type: application/json X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG... Request Body: { "url":"https://BucketName.obs.myhwclouds.com/ObjectName" }
Example Responses
Status code: 200
Example of a successful response
{ "result": { "name": "Name recognized from the image", "name_en": "ZHANG, SAN", "sex": "F", "birth_date": "01-01-1988", "number": "C66xxxx(E)", "symbols": "***AZ", "name_telegraph_code": "39xxxxxxx174", "permanent": true, "initial_issue_date": "(01-99)", "issue_date": "15-09-18", "confidence": { "name": 0.8876, "name_en": 0.9988, "sex": 0.9997, "birth_date": 0.9993, "number": 0.9832, "symbols": 0.959, "name_telegraph_code": 0.9998, "permanent": 0.9223, "initial_issue_date": 0.963, "issue_date": 0.9998 }, "portrait_location": [ [ 35, 115 ], [ 154, 115 ], [ 153, 263 ], [ 34, 263 ] ] } }
Status code: 400
Example of a failed response
{ "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.
Feedback
Was this page helpful?
Provide feedbackFor any further questions, feel free to contact us through the chatbot.
Chatbot