更新时间:2024-01-11 gmt 08:00

火车票识别-凯发k8国际娱乐官网入口

功能介绍

识别火车票中的文字信息,并以json格式返回识别的结构化结果。该接口的使用限制请参见约束与限制,详细使用指导请参见ocr服务使用简介章节。

图1 火车票示例图

如果图片中包含多张卡证票据,请调用智能分类识别服务。

约束与限制

  • 只支持识别png、jpg、jpeg、bmp、tiff格式图片。
  • 图像各边的像素大小在15px到8192px之间。
  • 图像中火车票区域有效占比超过25%。
  • 支持图像中火车票任意角度的水平旋转。
  • 支持少量扭曲,扭曲后图像中的火车票长宽比与实际火车票相差不超过10%。

调用方法

请参见。

前提条件

在使用之前,需要您完成服务申请和认证鉴权,具体操作流程请参见开通服务和章节。

用户首次使用需要先。服务只需要开通一次即可,后面使用时无需再次申请。如未开通服务,调用服务时会提示modelarts.4204报错,请在调用服务前先进入控制台开通服务,并注意开通服务区域与调用服务的区域保持一致。

uri

post /v2/{project_id}/ocr/train-ticket

表1 路径参数

参数

是否必选

说明

endpoint

终端节点,即调用api的请求地址。

不同服务不同区域的endpoint不同,您可以从中获取。

例如,ocr服务在“华北-北京四”区域的“endpoint”“ocr.cn-north-4.myhuaweicloud.com”

project_id

项目id,您可以从中获取。

请求参数

表2 请求header参数

参数

是否必选

参数类型

描述

x-auth-token

string

用户token。

用于获取操作api的权限。响应消息头中x-subject-token的值即为token。

content-type

string

发送的实体的mime类型,参数值为“application/json”。

enterprise-project-id

string

企业项目id。ocr支持通过企业项目管理(eps)对不同用户组和用户的资源使用,进行分账。

获取方法:进入“”页面,单击企业项目名称,在企业项目详情页获取enterprise-project-id(企业项目id)。

企业项目创建步骤请参见用户指南。

说明:

创建企业项目后,在传参时,有以下三类场景。

  • 携带正确的id,正常使用ocr服务,账单归到企业id对应的企业项目中。
  • 携带错误的id,正常使用ocr服务,账单的企业项目会被分类为“未归集”。
  • 不携带id,正常使用ocr服务,账单的企业项目会被分类为“未归集”。
表3 请求body参数

参数

是否必选

参数类型

说明

image

string

该参数与url二选一。

图片的base64编码,要求base64编码后大小不超过10mb。

图片最短边不小于15px,最长边不超过8192px,支持jpeg、jpg、png、bmp、tiff格式。

图片base64编码示例如/9j/4aaqskzjrgabag...,带有多余前缀会产生the image format is not supported报错。

url

string

该参数与image二选一。图片的url路径,目前支持:

  • 公网http/https url
  • obs提供的url,使用obs数据需要进行授权。包括对服务授权、临时授权、匿名公开授权,详情参见。
说明:
  • 接口响应时间依赖于图片的下载时间,如果图片下载时间过长,会返回接口调用失败。
  • 请保证被检测图片所在的存储服务稳定可靠,推荐使用obs服务存储图片数据。
  • url中不能存在中文字符,若存在,中文需要进行utf8编码。

return_text_location

boolean

识别到的文字块的区域位置信息。可选值包括:

true:返回各个文字块区域

false:不返回各个文字块区域

如果无该参数,系统默认不返回文字块区域。如果输入参数不是boolean类型,则会报非法参数错误。

响应参数

根据识别的结果,可能有不同的http响应状态码(status code)。例如,200表示api调用成功,400表示调用失败,详细的状态码和响应参数说明如下。

状态码: 200

表4 响应body参数

参数

参数类型

描述

result

trainticketresult object

识别结果。

调用失败时不返回此字段。

表5 trainticketresult

参数

参数类型

描述

ticket_id

string

火车票左上角的车票id。

check_port

string

检票口信息。

train_number

string

车次。

departure_station

string

始发站。

destination_station

string

终点站。

departure_station_en

string

始发站拼音。

destination_station_en

string

终点站拼音。

departure_time

string

开车时间。

seat_number

string

座位号。

ticket_price

string

票价。

sale_method

string

售票方式。

seat_category

string

座位类别。

ticket_changing

string

改签信息。

id_number

string

车票持有人的身份证号。

name

string

车票持有人姓名。

log_id

string

车票最下方的售票码。

sale_location

string

车票售票地点。

confidence

object

字段的置信度,取值范围0~1。

置信度越大,本次识别的字段的可靠性越高,在统计意义上,置信度越大,准确率越高。

置信度由算法给出,不直接等价于字段的准确率。

text_location

object

对应所有在原图上识别到的字段位置信息,包含所有文字区域四个顶点的二维坐标(x,y)。采用图像坐标系,坐标原点为图片左上角,x轴沿水平方向,y轴沿竖直方向。

状态码: 400

表6 响应body参数

参数

参数类型

说明

error_code

string

调用失败时的错误码,具体请参见。

当出现错误码“modelarts.4204”时,请参考为什么调用api时提示“modelarts.4204”?章节。

调用成功时不返回此字段。

error_msg

string

调用失败时返回的错误信息。

调用成功时不返回此字段。

请求示例

  • “endpoint”即调用api的请求地址,不同服务不同区域的“endpoint”不同,具体请参见。

    例如,火车票识别服务部署在“华北-北京四”区域的“endpoint”“ocr.cn-north-4.myhuaweicloud.com”“ocr.cn-north-4.myhuaweicloud.cn”,请求url为“https://ocr.cn-north-4.myhuaweicloud.com/v2/{project_id}/ocr/train-ticket”“project_id”为项目id,获取方法请参见

  • 如何获取token请参见。
  • 传入火车票图片的base64编码进行文字识别
    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..."
    }
  • 传入火车票图片的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"
    }

响应示例

状态码:200

成功响应示例

{
    "result": {
        "ticket_id": "kxxxx", 
        "check_port": "bxx", 
        "train_number": "gxxx", 
        "departure_station": "长沙南", 
        "destination_station": "武汉", 
        "departure_station_en": "changshanan", 
        "destination_station_en": "xxxx", 
        "departure_time": "2017年12月12日20:39", 
        "seat_number": "xx车xxx号", 
        "ticket_price": "164.5", 
        "sale_method": "网", 
        "seat_category": "二等座", 
        "ticket_changing": "改签", 
        "id_number": "1402271998****0012", 
        "name": "火车票", 
        "log_id": "65818301991213k09xxxx", 
        "sale_location": "长沙南", 
        "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
        }
    }
}

状态码:400

失败响应示例

{
    "error_code": "ais.0103", 
    "error_msg": "the image size does not meet the requirements." 
}

sdk代码示例

sdk代码示例如下。

  • 传入火车票图片的base64编码进行文字识别
     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 recognizetrainticketsolution {
        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(""))
                    .build();
            recognizetrainticketrequest request = new recognizetrainticketrequest();
            trainticketrequestbody body = new trainticketrequestbody();
            body.withimage("/9j/4aaqskzjrgabageasabiaad/4rfzrxhpzgaatu0akgaaaa...");
            request.withbody(body);
            try {
                recognizetrainticketresponse response = client.recognizetrainticket(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());
            }
        }
    }
    
  • 传入火车票图片的url进行文字识别
     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 recognizetrainticketsolution {
        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(""))
                    .build();
            recognizetrainticketrequest request = new recognizetrainticketrequest();
            trainticketrequestbody body = new trainticketrequestbody();
            body.withurl("https://bucketname.obs.myhuaweicloud.com/objectname");
            request.withbody(body);
            try {
                recognizetrainticketresponse response = client.recognizetrainticket(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());
            }
        }
    }
    
  • 传入火车票图片的base64编码进行文字识别
     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("")) \
            .build()
        try:
            request = recognizetrainticketrequest()
            request.body = trainticketrequestbody(
                image="/9j/4aaqskzjrgabageasabiaad/4rfzrxhpzgaatu0akgaaaa..."
            )
            response = client.recognize_train_ticket(request)
            print(response)
        except exceptions.clientrequestexception as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 传入火车票图片的url进行文字识别
     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("")) \
            .build()
        try:
            request = recognizetrainticketrequest()
            request.body = trainticketrequestbody(
                url="https://bucketname.obs.myhuaweicloud.com/objectname"
            )
            response = client.recognize_train_ticket(request)
            print(response)
        except exceptions.clientrequestexception as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 传入火车票图片的base64编码进行文字识别
     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("")).
                withcredential(auth).
                build())
        request := &model.recognizetrainticketrequest{}
    	imagetrainticketrequestbody:= "/9j/4aaqskzjrgabageasabiaad/4rfzrxhpzgaatu0akgaaaa..."
    	request.body = &model.trainticketrequestbody{
    		image: &imagetrainticketrequestbody,
    	}
    	response, err := client.recognizetrainticket(request)
    	if err == nil {
            fmt.printf("% v\n", response)
        } else {
            fmt.println(err)
        }
    }
    
  • 传入火车票图片的url进行文字识别
     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("")).
                withcredential(auth).
                build())
        request := &model.recognizetrainticketrequest{}
    	urltrainticketrequestbody:= "https://bucketname.obs.myhuaweicloud.com/objectname"
    	request.body = &model.trainticketrequestbody{
    		url: &urltrainticketrequestbody,
    	}
    	response, err := client.recognizetrainticket(request)
    	if err == nil {
            fmt.printf("% v\n", response)
        } else {
            fmt.println(err)
        }
    }
    

更多编程语言的sdk代码示例,请参见的代码示例页签,可生成自动对应的sdk代码示例。

状态码

状态码

描述

200

成功响应示例

400

失败响应样例

状态码请参见。

错误码

错误码请参见。

分享:
网站地图