Train Ticket OCR

Function

Train Ticket OCR recognizes the text in a train ticket 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.

Currently, train ticket information cannot be recognized.

If the image contains multiple cards and tickets, call Auto Classification OCR.

Prerequisites

Before using Train Ticket 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/train-ticket

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 at most 8,192 pixels. The JPEG, JPG, PNG, BMP, and TIFF 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.

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

TrainTicketResult object

Calling result of a successful API call

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

Table 5 TrainTicketResult

Parameter

Type

Description

ticket_id

String

Ticket ID in the upper left corner of a train ticket

check_port

String

Ticket gate

train_number

String

Train number

departure_station

String

Departure station

destination_station

String

Destination station

departure_station_en

String

Departure station, in Pinyin

destination_station_en

String

Destination station, in Pinyin

departure_time

String

Departure time

seat_number

String

Seat number

ticket_price

String

Ticket price

sale_method

String

Ticket selling mode

seat_category

String

Seat type

ticket_changing

String

Whether a ticket is changed. Possible values are as follows:

  • Yes: changed ticket
  • No: normal ticket

id_number

String

ID number of the ticket holder

name

String

Name of the ticket holder

log_id

String

Serial number at the bottom of a ticket

sale_location

String

Ticket selling place

confidence

Object

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

A higher confidence indicates a higher reliability of the corresponding field identified this time. In the statistical sense, a higher confidence indicates a higher accuracy.

The confidence is not equal to the accuracy, and is calculated through related algorithms.

Status code: 400

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, Train Ticket OCR is deployed in the CN North-Beijing4 region. The endpoint is ocr.cn-north-4.myhuaweicloud.com. The request URL is https://ocr.cn-north-4.myhuaweicloud.com/v2/{project_id}/ocr/train-ticket. 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/train-ticket
    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://{endpoint}/v2/{project_id}/ocr/train-ticket
    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/train-ticket"
    token = "Actual token value obtained by the user"
    headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}
    
    imagepath = r'./data/train-ticket-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": {
        "ticket_id": "KXXXX", 
        "check_port": "BXX", 
        "train_number": "GXXX", 
        "departure_station": "Departure station recognized from the image",
        "destination_station": "Destination station recognized from the image",
        "departure_station_en": "Changshanan", 
        "destination_station_en": "xxxx", 
        "departure_time": "Departure time recognized from the image",
        "seat_number": "Seat number recognized from the image",
        "ticket_price": "164.5", 
        "sale_method": "Sale method recognized from the image",
        "seat_category": "Seat category recognized from the image",
        "ticket_changing": "No", 
        "id_number": "1402271998****0012", 
        "name": "Name recognized from the image",
        "log_id": "65818301991213K09XXXX", 
        "sale_location": "Sale location recognized from the image",
        "confidence": {
            "ticket_id": 0.9992, 
            "check_port": 0.9165, 
            "train_number": 0.9968, 
            "starting_station": 0.9982, 
            "destination_station": 0.9998, 
            "starting_station_en": 0.9254, 
            "destination_station_en": 0.9848, 
            "starting_time": 0.5141, 
            "seat_number": 0.9985, 
            "ticket_rates": 0.9977, 
            "sale_method": 0.9836, 
            "seat_category": 0.9958, 
            "ticket_changing": 0.9, 
            "id_number": 0.9986, 
            "name": 0.996, 
            "log_id": 0.9968, 
            "ticket_office": 0.9984
        }
    }
}

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.