文字识别

    行程单识别

    接口描述

    支持对飞机行程单的18个关键字段进行结构化识别,包括电子客票号、印刷序号、姓名、始发站、目的站、航班号、日期、时间、票价、身份证号、承运人、民航发展基金、保险费、燃油附加费、其他税费、合计金额、填开日期、订票渠道。 同时,支持单张行程单上的多航班信息识别。

    请求说明

    请求示例

    HTTP 方法:POST

    请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/air_ticket

    URL参数:

    参数
    access_token 通过API Key和Secret Key获取的access_token,参考“Access Token获取

    Header如下:

    参数
    Content-Type application/x-www-form-urlencoded

    Body中放置请求参数,参数详情如下:

    请求参数

    参数 是否必选 类型 可选值范围 说明
    image string - 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式
    multi_detect bool true/false 控制是否开启多航班信息识别功能,默认值:false
    - true:开启多航班信息识别功能,开启后返回结果中对应字段格式将改为数组类型
    - false: 不开启,仅识别单一航班信息

    请求代码示例

    提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。

    提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。

    curl -i -k 'https://aip.baidubce.com/rest/2.0/ocr/v1/air_ticket?access_token=【调用鉴权接口获取的token】' --data 'image=【图片Base64编码,需UrlEncode】' -H 'Content-Type:application/x-www-form-urlencoded'
    # encoding:utf-8
    
    import requests
    import base64
    
    '''
    行程单识别
    '''
    
    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/air_ticket"
    # 二进制方式打开图片文件
    f = open('[本地文件]', 'rb')
    img = base64.b64encode(f.read())
    
    params = {"image":img}
    access_token = '[调用鉴权接口获取的token]'
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        print (response.json())
    package com.baidu.ai.aip;
    
    import com.baidu.ai.aip.utils.Base64Util;
    import com.baidu.ai.aip.utils.FileUtil;
    import com.baidu.ai.aip.utils.HttpUtil;
    
    import java.net.URLEncoder;
    
    /**
    * 行程单识别
    */
    public class AirTicket {
    
        /**
        * 重要提示代码中所需工具类
        * FileUtil,Base64Util,HttpUtil,GsonUtils请从
        * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
        * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
        * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
        * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
        * 下载
        */
        public static String airTicket() {
            // 请求url
            String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/air_ticket";
            try {
                // 本地文件路径
                String filePath = "[本地文件路径]";
                byte[] imgData = FileUtil.readFileByBytes(filePath);
                String imgStr = Base64Util.encode(imgData);
                String imgParam = URLEncoder.encode(imgStr, "UTF-8");
    
                String param = "image=" + imgParam;
    
                // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
                String accessToken = "[调用鉴权接口获取的token]";
    
                String result = HttpUtil.post(url, accessToken, param);
                System.out.println(result);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        public static void main(String[] args) {
            AirTicket.airTicket();
        }
    }
    #include <iostream>
    #include <curl/curl.h>
    
    // libcurl库下载链接:https://curl.haxx.se/download.html
    // jsoncpp库下载链接:https://github.com/open-source-parsers/jsoncpp/
    const static std::string request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/air_ticket";
    static std::string airTicket_result;
    /**
    * curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中
    * @param 参数定义见libcurl文档
    * @return 返回值定义见libcurl文档
    */
    static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream) {
        // 获取到的body存放在ptr中,先将其转换为string格式
        airTicket_result = std::string((char *) ptr, size * nmemb);
        return size * nmemb;
    }
    /**
    * 行程单识别
    * @return 调用成功返回0,发生错误返回其他错误码
    */
    int airTicket(std::string &json_result, const std::string &access_token) {
        std::string url = request_url + "?access_token=" + access_token;
        CURL *curl = NULL;
        CURLcode result_code;
        int is_success;
        curl = curl_easy_init();
        if (curl) {
            curl_easy_setopt(curl, CURLOPT_URL, url.data());
            curl_easy_setopt(curl, CURLOPT_POST, 1);
            curl_httppost *post = NULL;
            curl_httppost *last = NULL;
            curl_formadd(&post, &last, CURLFORM_COPYNAME, "image", CURLFORM_COPYCONTENTS, "【base64_img】", CURLFORM_END);
    
            curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
            result_code = curl_easy_perform(curl);
            if (result_code != CURLE_OK) {
                fprintf(stderr, "curl_easy_perform() failed: %s\n",
                        curl_easy_strerror(result_code));
                is_success = 1;
                return is_success;
            }
            json_result = airTicket_result;
            curl_easy_cleanup(curl);
            is_success = 0;
        } else {
            fprintf(stderr, "curl_easy_init() failed.");
            is_success = 1;
        }
        return is_success;
    }
    <?php
    /**
    * 发起http post请求(REST API), 并获取REST请求的结果
    * @param string $url
    * @param string $param
    * @return - http response body if succeeds, else false.
    */
    function request_post($url = '', $param = '')
    {
        if (empty($url) || empty($param)) {
            return false;
        }
    
        $postUrl = $url;
        $curlPost = $param;
        // 初始化curl
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $postUrl);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        // post提交方式
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        // 运行curl
        $data = curl_exec($curl);
        curl_close($curl);
    
        return $data;
    }
    
    $token = '[调用鉴权接口获取的token]';
    $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/air_ticket?access_token=' . $token;
    $img = file_get_contents('[本地文件路径]');
    $img = base64_encode($img);
    $bodys = array(
        'image' => $img
    );
    $res = request_post($url, $bodys);
    
    var_dump($res);
    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Web;
    
    namespace com.baidu.ai
    {
        public class AirTicket
        {
            // 行程单识别
            public static string airTicket()
            {
                string token = "[调用鉴权接口获取的token]";
                string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/air_ticket?access_token=" + token;
                Encoding encoding = Encoding.Default;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
                request.Method = "post";
                request.KeepAlive = true;
                // 图片的base64编码
                string base64 = getFileBase64("[本地图片文件]");
                String str = "image=" + HttpUtility.UrlEncode(base64);
                byte[] buffer = encoding.GetBytes(str);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                string result = reader.ReadToEnd();
                Console.WriteLine("行程单识别:");
                Console.WriteLine(result);
                return result;
            }
    
            public static String getFileBase64(String fileName) {
                FileStream filestream = new FileStream(fileName, FileMode.Open);
                byte[] arr = new byte[filestream.Length];
                filestream.Read(arr, 0, (int)filestream.Length);
                string baser64 = Convert.ToBase64String(arr);
                filestream.Close();
                return baser64;
            }
        }
    }

    返回说明

    返回参数

    字段 是否必选 类型 说明
    log_id uint64 唯一的log id,用于问题定位
    words_result_num uint32 识别结果数,表示words_result的元素个数
    words_result object{} 识别结果
    name string 姓名
    starting_station string 始发站
    destination_station string 目的站
    flight string 航班号
    date string 日期
    ticket_number string 电子客票号码
    fare string 票价
    dev_fund string 民航发展基金/基建费
    fuel_surcharge string 燃油附加费
    other_tax string 其他税费
    ticket_rates string 合计金额
    issued_date string 填开日期
    id_num string 身份证号
    carrier string 承运人
    time string 时间
    issued_by string 订票渠道
    serial_number string 印刷序号
    insurance string 保险费

    返回示例

    // 识别单航班信息(multi_detect=false,或参数缺省)
    {
        "log_id": 7306800033425229106,
        "direction": 0,
        "words_result_num": 18,
        "words_result": {
            "fare": "1220.00",
            "fuel_surcharge": "EXEMPT",
            "flight": "8121",
            "name": "陆辉轩",
            "other_tax": "",
            "destination_station": "呼和浩特",
            "issued_by": "海南航空股份有限公司",
            "serial_number": "13112900342",
            "insurance": "XXX",
            "id_num": "101506011111090539234",
            "carrier": "",
            "dev_fund": "EXEMPT",
            "ticket_rates": "1220.00",
            "time": "07:15",
            "date": "2019-01-24",
            "ticket_number": "87001182672",
            "issued_date": "2019-01-24",
            "starting_station": "深圳"
        }
    }
    
    // 识别多航班信息(multi_detect=true)
    {
        "words_result": {
            "insurance": [
                {
                    "word": "XXX"
                }
            ],
            "date": [
                {
                    "word": "2019-10-18"
                },
                {
                    "word": "2019-10-21"
                }
            ],
            "flight": [
                {
                    "word": "CZ3565"
                },
                {
                    "word": "CZ3566"
                }
            ],
            "issued_by": [
                {
                    "word": "上海携程旅行社有限公司"
                }
            ],
            "starting_station": [
                {
                    "word": "北京"
                }
            ],
            "fare": [
                {
                    "word": "1080.00"
                }
            ],
            "ticket_rates": [
                {
                    "word": "1420.00"
                }
            ],
            "serial_number": [
                {
                    "word": "45956029770"
                }
            ],
            "ticket_number": [
                {
                    "word": "7849648364314"
                }
            ],
            "fuel_surcharge": [
                {
                    "word": "240.00"
                }
            ],
            "carrier": [
                {
                    "word": "南航"
                },
                {
                    "word": "南航"
                }
            ],
            "issued_date": [
                {
                    "word": "2019-09-18"
                }
            ],
            "other_tax": [],
            "id_num": [
                {
                    "word": "0789654700"
                }
            ],
            "destination_station": [
                {
                    "word": "深圳"
                },
                {
                    "word": "北京"
                }
            ],
            "name": [
                {
                    "word": "姚佳"
                }
            ],
            "time": [
                {
                    "word": "13:55"
                },
                {
                    "word": "16:30"
                }
            ],
            "dev_fund": [
                {
                    "word": "100.00"
                }
            ]
        },
        "log_id": "1280814270572920832",
        "words_result_num": 18
    }
    上一篇
    出租车票识别
    下一篇
    汽车票识别