文字识别

    API文档

    请求说明

    请求示例

    HTTP 方法:POST

    请求URL: https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise/finance

    URL参数:

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

    Header如下:

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

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

    请求参数

    参数 是否必选 类型 可选值范围 说明
    image true string - 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式
    templateSign false string - 模板 ID,自定义模板或预置模板的唯一标示,可用于调用指定的识别模板进行结构化识别,可在「模板管理」页查看并复制使用
    detectorId false int 0 检测器ID,可选值仅有0。detectorId = 0时,启用混贴票据识别功能,可对发票粘贴单上的多张不同种类票据进行分类检测和识别
    classifierId false int - 分类器Id,分类器的唯一标示,可用于调用指定的分类器对传入的图片进行自动分类及识别,与 templateSign、detectorId 至少存在一个,如同时存在,则优先级 detectorId > templateSign > classfierId

    请求代码示例

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

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

    # 请求模板id
    curl -i -k 'https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise/finance?access_token=【调用鉴权接口获取的token】' --data 'templateSign=xxx&image=【图片Base64编码,需UrlEncode】' -H 'Content-Type:application/x-www-form-urlencoded'
    # 请求分类器id
    curl -i -k 'https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise/finance?access_token=【调用鉴权接口获取的token】' --data 'classifierId=xxx&image=【图片Base64编码,需UrlEncode】' -H 'Content-Type:application/x-www-form-urlencoded'
    # 请求混贴票据
    curl -i -k 'https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise/finance?access_token=【调用鉴权接口获取的token】' --data 'detectorId=0&image=【图片Base64编码,需UrlEncode】' -H 'Content-Type:application/x-www-form-urlencoded'
    # -*- coding:UTF-8 -*-
    # -*- encoding: utf-8 -*-
    import base64
    import requests
    
    if sys.version_info.major == 2:
        from urllib import quote
    else:
        from urllib.parse import quote
    
    
    headers = {
            'Content-Type': "application/x-www-form-urlencoded",
            'charset': "utf-8"
        }
    if __name__ == '__main__':
        # 代码中所需的工具包requests
        # 安装方式:pip install requests
        # iocr识别api_url
        recognise_api_url = "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise/finance"
    
    
        access_token = "your_access_token"
        templateSign = "your_templateSign"
        detectorId = 0
        classifierId = "your_classifier_id"
        # 测试数据路径
        image_path = "your_file_path"
        try:
            with open(image_path, 'rb') as f:
                image_data = f.read()
            
            if sys.version_info.major == 2:
                image_b64 = base64.b64encode(image_data).replace("\r", "")
            else:
                image_b64 = base64.b64encode(image_data).decode().replace("\r", "")
            
            # 请求模板的bodys
            recognise_bodys = "access_token=" + access_token + "&templateSign=" + templateSign + \
                    "&image=" + quote(image_b64.encode("utf8"))
            # 请求分类器的bodys
            classifier_bodys = "access_token=" + access_token + "&classifierId=" + classifierId + \
                    "&image=" + quote(image_b64.encode("utf8"))
            # 混贴票据识别的bodys
            detector_bodys = "access_token=" + access_token + "&detectorId=" + str(detectorId) + \
                    "&image=" + quote(image_b64.encode("utf8"))
            # 请求模板识别
            response = requests.post(recognise_api_url, data=recognise_bodys, headers=headers)
            # 请求分类器识别
            # response = requests.post(recognise_api_url, data=classifier_bodys, headers=headers)
            # 请求混贴票据识别
            # response = requests.post(recognise_api_url, data=detector_bodys, headers=headers)
            print response.text
        except Exception as e:
            print e
    package com.baidu.ocr;
    
    import com.baidu.ai.aip.utils.Base64Util;
    import com.baidu.ai.aip.utils.FileUtil;
    import com.baidu.ai.aip.utils.HttpUtil;
    
    
    public class App 
    {
        public static void main(String[] args) throws Exception
        {
            /**
             * 重要提示代码中所需工具类
             * 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
             * 下载
             */
            // iocr识别apiUrl
            String recogniseUrl = "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise/finance";
    
    
            String filePath = "path\to\your\image.jpg";
            try {
                    byte[] imgData = FileUtil.readFileByBytes(filePath);
                    String imgStr = Base64Util.encode(imgData);
                    // 请求模板参数
                    String recogniseParams = "templateSign=your_template_sign&image=" + URLEncoder.encode(imgStr, "UTF-8");
                    // 请求分类器参数
                    String classifierParams = "classifierId=your_classfier_id&image=" + URLEncoder.encode(imgStr, "UTF-8");
                    // 请求混贴票据识别参数
                    String detectorParams = "detectorId=0&image=" + URLEncoder.encode(imgStr, "UTF-8");
                    
                    
                    String accessToken = "your_access_token";
                    // 请求模板识别
                    String result = HttpUtil.post(recogniseUrl, accessToken, recogniseParams);
                    // 请求分类器识别
                    // String result = HttpUtil.post(recogniseUrl, accessToken, classifierParams);
                    // 请求混贴票据识别
                    // String result = HttpUtil.post(recogniseUrl, accessToken, detectorParams);
                    
                    System.out.println(result);
            } catch (Exception e) {
                    e.printStackTrace();
            }
        }
    }
    #include <iostream>
    #include "curl_install/include/curl/curl.h"
    #include <fstream>
    #include "Base64.h"
    #include <cctype>
    #include <iomanip>
    #include <sstream>
    #include <string>
    
    using namespace std;
    // libcurl库下载链接:https://curl.haxx.se/download.html
    // 通用iocr的接口url
    static string ocr_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格式
        ocr_result = string((char *) ptr, size * nmemb);
        return size * nmemb;
    }
    
    string url_encode(const std::string &value) {
        ostringstream escaped;
        escaped.fill('0');
        escaped << hex;
        for (std::string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) {
            std::string::value_type c = (*i);
            // Keep alphanumeric and other accepted characters intact
            if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
                escaped << c;
                continue;
            }
            // Any other characters are percent-encoded
            escaped << uppercase;
            escaped << '%' << setw(2) << int((unsigned char) c);
            escaped << nouppercase;
        }
    
        return escaped.str();
    }
    // base64.h下载地址
    // https://bj.bcebos.com/v1/iocr-movie/Base64.h
    int iocr_regenize(const std::string &image_base64, const std::string &access_token, const std::string &templateSign, 
        const std::int classifierId, const std::int detectorId) {
        // iocr识别apiUrl
        const static string recognise_api_url = "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise/finance";
        // 请求模板的参数
        std::string recognise_params = "access_token=" + access_token + "&templateSign=" + templateSign + "&image=" + image_base64;
    
        // 请求分类器的参数
        std::string classifier_params = "access_token=" + access_token 
            + "&classifierId=" + std::to_string(classifierId) 
            + "&image=" + image_base64;
        // 请求混贴票据识别的参数
        std::string detector_params = "access_token=" + access_token
            + "&detectorId=" + std::to_string(detectorId)
            + "&image=" + image_base64;
        
        struct curl_slist * headers = NULL;
        headers = curl_slist_append(headers, "Content-Type:application/x-www-form-urlencoded");
        headers = curl_slist_append(headers, "charset:utf-8");
        CURL *curl;
        CURLcode result_code;
        int is_success;
        curl = curl_easy_init();
        if (curl) {
            // 使用libcurl post数据:设定待post的url等
            curl_easy_setopt(curl, CURLOPT_URL, recognise_api_url.c_str());
            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
            curl_easy_setopt(curl, CURLOPT_POST, 1);
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,callback);
            // 请求模板
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, recognise_params.c_str());
            curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, recognise_params.size());
            // 请求分类器
            // curl_easy_setopt(curl, CURLOPT_POSTFIELDS, classifier_params.c_str());
            // curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, classifier_params.size());
            // 请求混贴票据识别
            // curl_easy_setopt(curl, CURLOPT_POSTFIELDS, detector_params.c_str());
            // curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, detector_params.size());
            result_code = curl_easy_perform(curl);
            // http post不成功时错误处理
            if (result_code != CURLE_OK) {
                fprintf(stderr, "curl_easy_perform() failed: %s\n",
                        curl_easy_strerror(result_code));
                is_success = 1;
                return is_success;
            }
            curl_easy_cleanup(curl);
            curl_slist_free_all(headers);
            // 控制台输出识别结果
            cout << ocr_result << endl;
            is_success = 0;
        } else {
            fprintf(stderr, "curl_easy_init() failed.");
            is_success = 1;
        }
        return is_success;
    }
    
    std::string get_image_b64(const std::string &image_path) {
        ifstream f;
        f.open(image_path, ios::in | ios::binary);
        // 定位指针到文件末尾
        f.seekg(0, ios::end);
        int size = f.tellg();
        // 指针重新回到文件头部
        f.seekg(0, ios::beg);
        char* buffer = (char*)malloc(sizeof(char)*size);
        f.read(buffer,size);
        f.close();
        // base64编码
        std::string image_b64 = base64_encode(buffer, size);
        return image_b64;
    }
    
    int main() {
        std::string access_token = "your_access_token";
        std::string image_path = "your_file_path";
        std::string templateSign = "your_templateSign";
        std::int classifierId = "your_classifier_id";
        std::int detectorId = 0;
        // 获取本地图片的base64
        std::string image_b64 = get_image_b64(image_path);
        // urlcode编码
        image_b64 = url_encode(image_b64);
        // 发送post请求
        iocr_regenize(image_b64, access_token, templateSign, classifierId, detectorId);
        return 0;
    }
    <?php
    /**
     * 发起http post请求(REST API), 并获取REST请求的结果
     * @param string $url
     * @param string $param
     * @return - http response body if succeeds, else false.
     */
    function request_post($url = '', $params = '')
    {
        if (empty($url) || empty($params)) {
            return false;
        }
        $headers = array("Content-Type" => "application/x-www-form-urlencoded", "charset" => "utf-8");
        // 初始化curl
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        // post提交方式
        //curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
        // 运行curl
        $data = curl_exec($curl);
        curl_close($curl);
    
        return $data;
    }
    
    function get_image_b64($image_path = '')
    {
        $image_b64 = '';
        if(file_exists($image_path)) {
            $image_data = file_get_contents($image_path);
            print("file exists\n");
            $image_b64_tmp = base64_encode($image_data);
            $image_b64 = urlencode($image_b64_tmp);
            //print($image_b64);
        } else {
            print("file doesn't exists\n");
        }
        return $image_b64;
    }
    // iocr识别api_url
    $recognise_api_url = "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise/finance";
    
    
    $access_token = "your_access_token";
    $templateSign = "your_templateSign";
    $detectorId = 0;
    $classifierId = "your_classifier_id";
    $image_path = "your_file_path";
    $image_b64 = get_image_b64($image_path);
    // iocr识别bodys
    $recognise_bodys = "access_token=". $access_token. "&templateSign=". $templateSign. "&image=". $image_b64;
    # 分类器请求的bodys
    $classifier_bodys = "access_token=". $access_token. "&classifierId=". $classifierId. "&image=". $image_b64;
    # 混贴票据识别的bodys
    $detector_bodys = "access_token=". $access_token. "&detectorId=". $detectorId. "&image=". $image_b64;
    # 请求模板
    $response = request_post($recognise_api_url, $recognise_bodys);
    # 请求分类器
    # $response = request_post($recognise_api_url, $classifier_bodys);
    # 请求混贴票据识别
    # $response = request_post($recognise_api_url, $detector_bodys);
    
    var_dump($response)
    ?>
    using System;
    using System.Net;
    using System.IO;
    using System.Text;
    using System.Web;
    
    namespace iocr_api_demo
    {
        class IocrApiDemo
        {
            static void Main(String[] args)
            {
                PostHttp();
            }
            public static void PostHttp(){
                // fileUtils.cs 类下载地址
                // https://bj.bcebos.com/v1/iocr-movie/FileUtils.cs
                // iocr识别api_url
                String recognise_api_url = "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise/finance";
                
                String access_token = "your_access_token";
                String image_path = "your_file_path";
                String image_b64 = FileUtils.getFileBase64(image_path);
                // iocr按模板id识别的请求bodys
                string templateSign = "yout_templateSign";
                string recognise_bodys = "access_token=" + access_token + "&templateSign=" + templateSign + 
                                "&image=" + HttpUtility.UrlEncode(image_b64);
                // iocr按分类器id识别的请求bodys
                int classifierId = "your_classifier_id";
                String classifier_bodys = "access_token=" + access_token + "&classifierId=" + classifierId + "&image=" + HttpUtility.UrlEncode(image_b64);
                // iocr混贴票据识别的请求bodys
                int detectorId = 0;
                String detector_bodys = "access_token=" + access_token + "&detectorId=" + detectorId + "&image=" + HttpUtility.UrlEncode(image_b64);
    
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(recognise_api_url);
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.Method = "POST";
                httpWebRequest.KeepAlive = true;
                try{
                    // 请求模板id
                    byte[] btBodys = Encoding.UTF8.GetBytes(recognise_bodys);
                    // 请求分类器id
                    // byte[] btBodys = Encoding.UTF8.GetBytes(classifier_bodys);
                    // 请求混贴票据识别
                    // byte[] btBodys = Encoding.UTF8.GetBytes(detector_bodys);
                    httpWebRequest.ContentLength = btBodys.Length;
                    httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
                    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                    string responseContent = streamReader.ReadToEnd();
                    Console.WriteLine(responseContent);
                    httpWebResponse.Close();
                    streamReader.Close();
                    httpWebRequest.Abort();
                    httpWebResponse.Close();
                } catch (Exception e){
                    Console.Write(e.Message);
                }    
            }
        }
    }

    返回说明

    返回参数

    字段 类型 说明
    error_code int 0代表成功,如果有错误码返回可以参考下方错误码列表排查问题
    error_msg string 如果error_code具体的失败信息,可以参考下方错误码列表排查问题
    data jsonObject 识别返回的结果
    logId string 调用的日志id
    ret jsonArray 识别出来的字段数组,每一个单元里包含以下几个元素
    word_name string isStructured 为 true 时存在,表示字段的名字;如果 isStructured 为 false 时,不存在
    word string 识别的字符串或单字
    location jsonObject 字段在原图上对应的矩形框位置,通过上边距、左边距、宽度、高度表示
    probability jsonObject 字段的置信度,包括最大,最小和方差
    templateSign string 图片分类结果对应的模板id或指定使用的模版id。
    detectorId = 0时,对上传的发票粘贴单中的多张不同票据进行检测分类,返回每张发票的类别,templateSign的对应关系为:
    - vat_invoice:增值税发票;
    - taxi:出租车票;
    - roll_ticket:卷票;
    - train_ticket:火车票;
    - quota_invoice:定额发票;
    - travel_itinerary:行程单;
    - car_invoice:汽车票;
    - toll_invoice:通行费发票;
    - printed_invoice:机打发票。
    scores float 分类置信度,如果指定templateSign,则该值为1
    isStructured string 表示是否结构化成功,true为成功,false为失败;成功时候,返回结构化的识别结果;失败时,如果能识别,按行返回结果,如果不能识别,返回空

    返回示例

    • 使用自定义模板及自定义分类器功能时,返回结果可参考 iOCR通用版-返回示例
    • detectorId = 0时,使用票据检测分类功能,返回结果如下所示:
    {
    	"data": {
    		"ret": [{
    			"ret": [{
    				"word_name": "invoice_code",
    				"word": "113001851019"
    			}, {
    				"word_name": "invoice_rate",
    				"word": "拾元整"
    			}, {
    				"word_name": "invoice_number",
    				"word": "25018254"
    			}],
    			"templateSign": "quota_invoice",
    			"scores": 0.95463621616364,
    			"isStructured": true
    		}, {
    			"ret": [{
    				"word_name": "invoice_code",
    				"word": "113001751019"
    			}, {
    				"word_name": "invoice_rate",
    				"word": "拾元整"
    			}, {
    				"word_name": "invoice_number",
    				"word": "25018226"
    			}],
    			"templateSign": "quota_invoice",
    			"scores": 0.95132255554199,
          "isStructured": true
    		}, {
    			"ret": [{
    				"word_name": "invoice_code",
    				"word": "113001851019"
    			}, {
    				"word_name": "invoice_rate",
    				"word": "拾元整"
    			}, {
    				"word_name": "invoice_number",
    				"word": "25018273"
    			}],
    			"templateSign": "quota_invoice",
    			"scores": 0.95567744970322,
    			"isStructured": true
    		}, {
    			"ret": [{
    				"word_name": "invoice_code",
    				"word": "113001851019"
    			}, {
    				"word_name": "invoice_rate",
    				"word": "拾元整"
    			}, {
    				"word_name": "invoice_number",
    				"word": "25018272"
    			}],
    			"templateSign": "quota_invoice",
    			"scores": 0.9886274933815,
    			"isStructured": true
    		}],
    		"logid": "155601882921432"
    	},
      "error_code": 0,
    	"error_msg": ""
    }
    上一篇
    使用流程
    下一篇
    常见问题