Chinese Mainland Travel Permit
Function
This API detects and extracts text from images of Chinese Mainland Travel Permit for Hong Kong (China), Macao (China), and Taiwan (China) Residents and converts the text into a structured format. This OCR service supports the following two types of Chinese mainland travel permits: Chinese mainland travel permit for Hong Kong (China) and Macao (China) residents and Chinese mainland travel permit for Taiwan (China) residents. For details about the constraints on using this API, see Constraints and Limitations. For details about how to use this API, see Introduction to OCR.
Constraints and Limitations
- 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.
- A travel permit to be recognized must occupy more than 50% of the image. When scanning a travel permit, ensure that the entire travel permit 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.
Calling Method
For details, see Calling APIs.
Prerequisites
Before using this API, subscribe to the service and complete authentication. For details, see Subscribing to an OCR Service and Authentication.
Before you use the service for the first time, subscribe to the service by clicking Subscribe. You only need to subscribe to the service once. If you have not subscribed to the service yet, error "ModelArts.4204" will be displayed when you call this API. Before you call the API, log in to the OCR console and subscribe to the corresponding service. Ensure that you make the subscription to the service in the same region where you want to call this API.
URI
POST /v2/{project_id}/ocr/mainland-travel-permit
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 by referring to Obtaining a Project ID. |
Request Parameters
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
X-Auth-Token |
Yes |
String |
User token Used to obtain the permission to call 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 console, 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 |
Set either this parameter or url. Base64-encoded image data. 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 |
Set either this parameter or image. Image URL. Currently, the following URLs are supported:
NOTE:
|
return_portrait_image |
No |
Boolean |
Whether to return the portrait. The options are:
If this parameter is not specified, false is used by default. In this case, the Base64 encoded string of the portrait on the travel permit will not be returned. |
return_portrait_location |
No |
Boolean |
Whether to return the location of the portrait on the travel permit. The options are:
If this parameter is not specified, false is used by default. |
Response Parameters
Status code: 200
Parameter |
Type |
Description |
---|---|---|
result |
MainlandTravelPermitResult 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 |
Name in English |
birth_date |
String |
Date of birth |
sex |
String |
Gender |
valid_period |
String |
Date of expiry |
issuing_authority |
String |
Issuing authority |
number |
String |
Permit number |
issue_place |
String |
Place where the permit is issued |
issue_times |
String |
Number of issuing times |
id_name |
String |
Name on the back of the travel permit |
id_number |
String |
Number on the back of the travel permit |
machine_code1 |
String |
Machine-readable code in the first line |
machine_code2 |
String |
Machine-readable code in the second line |
machine_code3 |
String |
Machine-readable code in the third line |
type |
String |
Permit type. The options are:
|
side |
String |
Whether the front or back of the travel permit is displayed. The options are:
|
portrait_image |
String |
Base64 encoded string of the portrait. This parameter is returned only when return_portrait_image is set to true. |
portrait_location |
Array<Array<Integer>> |
Location of the portrait on the original image. This parameter is returned 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. |
confidence |
Confidence of each field |
Parameter |
Type |
Description |
---|---|---|
name |
Float |
Confidence of the Chinese name |
name_en |
Float |
Confidence of the English name |
birth_date |
Float |
Confidence of the birth date |
sex |
Float |
Confidence of the gender |
valid_period |
Float |
Confidence of the validity period |
issuing_authority |
Float |
Confidence of the issuing authority |
number |
Float |
Confidence of the permit number |
issue_place |
Float |
Confidence of the place of issuance |
issue_times |
Float |
Confidence of the number of issuing times |
type |
Float |
Confidence of the permit type |
side |
Float |
Confidence of information on the front and back sides of image of the permit |
id_name |
Float |
Confidence of the name on the back of the travel permit |
id_number |
Float |
Confidence of the number on the back of the travel permit |
machine_code1 |
Float |
Confidence of the machine-readable code in the first line |
machine_code2 |
Float |
Confidence of the machine-readable code in the second line |
machine_code3 |
Float |
Confidence of the machine-readable code in the third line |
Status code: 400
Parameter |
Type |
Description |
---|---|---|
error_code |
String |
Error code of a failed API call. For details, see Error Codes. This parameter is not included when the API is successfully called. |
error_msg |
String |
Error message when the API call fails. This parameter is not included when the API is successfully called. |
Example Request
- Example request (Method 1: Use the image Base64 string.)
POST https://{endpoint}/v2/{project_id}/ocr/mainland-travel-permit Request Header: Content-Type: application/json, X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG... Request Body: { "image": "/9j/4AAQSkZJRgABAgEASABIAAD/4RFZRXhpZgAATU0AKgA..." "return_portrait_image": True, "return_portrait_location": True }
- Example request (Method 2: Use the image URL.)
POST https://{endpoint}/v2/{project_id}/ocr/mainland-travel-permit Request Header: Content-Type: application/json, X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG... Request Body: { "url": "https://BucketName.obs.xxxx.com/ObjectName", "return_portrait_image": True, "return_portrait_location": True }
Example Response
Status code: 200
Example response for a successful request
{ "result" : { "name": "Zhang San", "name_en" : "ZHANG, SAN", "birth_date" : "1992.10.11", "sex": "M", "valid_period" : "1992.10.11-2002.04.04", "issuing_authority": "Bureau of Exit and Entry Administration of the Ministry of Public Security", "number" : "E23xxxxxx", "issue_times" : "00", "type": "Mainland Travel Permit for Hong Kong and Macao Residents", "side" : "front", "confidence" : { "name" : 0.9999, "name_en" : 0.9868, "birth_date" : 0.9984, "sex" : 0.9999, "valid_period" : 0.9988, "issuing_authority" : 0.9998, "number" : 0.9999, "issue_times" : 0.9997, "type" : 0.9999, "side" : 1.0 } } }
Status code: 400
Example response for a failed request
{ "error_code" : "AIS.0103", "error_msg" : "The image size does not meet the requirements." }
Example SDK Code
The example SDK code is as follows:
You are advised to update the SDKs to the latest versions before use to prevent the local outdated SDKs from being unable to use the latest OCR functions.
- Transfer the Base64 encoded string of the Chinese mainland travel permit image for recognition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
package com.huaweicloud.sdk.test; import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.ocr.v1.region.OcrRegion; import com.huaweicloud.sdk.ocr.v1.*; import com.huaweicloud.sdk.ocr.v1.model.*; public class RecognizeMainlandTravelPermitSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); OcrClient client = OcrClient.newBuilder() .withCredential(auth) .withRegion(OcrRegion.valueOf("<YOUR REGION>")) .build(); RecognizeMainlandTravelPermitRequest request = new RecognizeMainlandTravelPermitRequest(); MainlandTravelPermitRequestBody body = new MainlandTravelPermitRequestBody(); body.withImage("/9j/4AAQSkZJRgABAgEASABIAAD/4RFZRXhpZgAATU0AKgAAAA..."); request.withBody(body); try { RecognizeMainlandTravelPermitResponse response = client.recognizeMainlandTravelPermit(request); System.out.println(response.toString()); } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { e.printStackTrace(); } catch (ServiceResponseException e) { e.printStackTrace(); System.out.println(e.getHttpStatusCode()); System.out.println(e.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
- Transfer the URL of the Chinese mainland travel permit image for recognition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
package com.huaweicloud.sdk.test; import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.ocr.v1.region.OcrRegion; import com.huaweicloud.sdk.ocr.v1.*; import com.huaweicloud.sdk.ocr.v1.model.*; public class RecognizeMainlandTravelPermitSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); OcrClient client = OcrClient.newBuilder() .withCredential(auth) .withRegion(OcrRegion.valueOf("<YOUR REGION>")) .build(); RecognizeMainlandTravelPermitRequest request = new RecognizeMainlandTravelPermitRequest(); MainlandTravelPermitRequestBody body = new MainlandTravelPermitRequestBody(); body.withUrl("https://BucketName.obs.myhuaweicloud.com/ObjectName"); request.withBody(body); try { RecognizeMainlandTravelPermitResponse response = client.recognizeMainlandTravelPermit(request); System.out.println(response.toString()); } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { e.printStackTrace(); } catch (ServiceResponseException e) { e.printStackTrace(); System.out.println(e.getHttpStatusCode()); System.out.println(e.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
- Transfer the Base64 encoded string of the Chinese mainland travel permit image for recognition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# coding: utf-8 from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkocr.v1.region.ocr_region import OcrRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkocr.v1 import * if __name__ == "__main__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = os.getenv("CLOUD_SDK_AK") sk = os.getenv("CLOUD_SDK_SK") credentials = BasicCredentials(ak, sk) \ client = OcrClient.new_builder() \ .with_credentials(credentials) \ .with_region(OcrRegion.value_of("<YOUR REGION>")) \ .build() try: request = RecognizeMainlandTravelPermitRequest() request.body = MainlandTravelPermitRequestBody( image="/9j/4AAQSkZJRgABAgEASABIAAD/4RFZRXhpZgAATU0AKgAAAA..." ) response = client.recognize_mainland_travel_permit(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
- Transfer the URL of the Chinese mainland travel permit image for recognition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# coding: utf-8 from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkocr.v1.region.ocr_region import OcrRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkocr.v1 import * if __name__ == "__main__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = os.getenv("CLOUD_SDK_AK") sk = os.getenv("CLOUD_SDK_SK") credentials = BasicCredentials(ak, sk) \ client = OcrClient.new_builder() \ .with_credentials(credentials) \ .with_region(OcrRegion.value_of("<YOUR REGION>")) \ .build() try: request = RecognizeMainlandTravelPermitRequest() request.body = MainlandTravelPermitRequestBody( url="https://BucketName.obs.myhuaweicloud.com/ObjectName" ) response = client.recognize_mainland_travel_permit(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
- Transfer the Base64 encoded string of the Chinese mainland travel permit image for recognition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" ocr "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() client := ocr.NewOcrClient( ocr.OcrClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.RecognizeMainlandTravelPermitRequest{} imageMainlandTravelPermitRequestBody:= "/9j/4AAQSkZJRgABAgEASABIAAD/4RFZRXhpZgAATU0AKgAAAA..." request.Body = &model.MainlandTravelPermitRequestBody{ Image: &imageMainlandTravelPermitRequestBody, } response, err := client.RecognizeMainlandTravelPermit(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
- Transfer the URL of the Chinese mainland travel permit image for recognition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" ocr "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() client := ocr.NewOcrClient( ocr.OcrClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.RecognizeMainlandTravelPermitRequest{} urlMainlandTravelPermitRequestBody:= "https://BucketName.obs.myhuaweicloud.com/ObjectName" request.Body = &model.MainlandTravelPermitRequestBody{ Url: &urlMainlandTravelPermitRequestBody, } response, err := client.RecognizeMainlandTravelPermit(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
For more SDK code examples in various programming languages, see the Sample Code tab on the right of the API Explorer page, which can automatically generate corresponding SDK code examples.
Status Codes
Status Code |
Description |
---|---|
200 |
Example response for a successful request |
400 |
Example response for a failed request |
See Status Codes.
Error Codes
See Error Codes.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot