帮助与文档 > 产品文档 > 文字识别OCR > API文档 > 整题识别(含公式)服务
整题识别(含公式)服务

整题识别(含公式)简介

Hi,您好,欢迎使用有道智云整题识别(含公式)OCRAPI接口服务。

本文档主要针对需要集成API的技术开发工程师,详细描述整题识别(含公式)OCR能力相关的技术内容。

如果您有与我们商务合作的需求,可以通过一下方式联系我们:

商务邮箱: AIcloud_Business@corp.youdao.com

如果您对文档内容有任何疑问,可以通过以下几种方式联系我们:

客服QQ:1906538062

官方交流群:861723255

联系邮箱: zyservice@corp.youdao.com

温馨提示:

本文档主要针对开发人员,接入测试前需要在后台创建API应用,并绑定整题识别(含公式)OCR实例;如果您还没有,请按照新手指南操作

平台向每个账户赠送50元的体验金,供用户集成前测试所用,具体资费规则详见整题识别(含公式)OCR服务报价

协议须知

调用方在集成文字识别OCRAPI时,请遵循以下规则。

规则描述
传输方式HTTP/HTTPS
请求方式POST
字符编码统一使用UTF-8编码
响应格式统一采用JSON格式

接口说明

有道智云OCR API HTTP地址:

http://openapi.youdao.com/ocr_formula

有道智云OCR API HTTPS地址:

https://openapi.youdao.com/ocr_formula

接口调用参数

调用API需要向接口发送以下字段来访问服务。

符号含义必填举例
detectType识别类型,10011:识别结果公式完美还原;10012:识别结果适合搜题,默认10011false10011
imageType支持的图片类型,目前只支持Base64true1
appKey应用申请的key,测试使用tb094x的openapi库中的App表,请自行添加,服务会每分钟同步表中的appKey,该表中的数据可设置为无效,请不要删除truetestKey
img要识别的图片,目前只支持Base64编码true
curtime时间戳true
sign签名,通过sha256(appKey+input+curtime+salt+appSecret)生成,appKey,salt都为原始值,img是Base64编码truewhatever
salt随机值,建议北京当前时间戳truewhatever
signType签名类型truev3
docType服务器响应类型,目前只支持jsonfalsejson

input=img字符串的前10个字符 + img字符串长度 + img字符串后十个字符(当q长度大于20)或 img字符串(当q长度小于等于20).

具体示例如下:

a.比如:img="Welcome to youdao AICloud.",字符串长度为26,大于20,其input="Welcome to"(前10个字符) + 26(字符串长度) + "o AICloud."(q字符串后10个字符)。
b.比如:img = "sdT4bWrjS",字符串长度为9,小于20,input = "sdT4bWrjS".
注意:

  1. 请先将需要识别的图片转换为 Base64 编码。
  2. 在发送HTTP请求之前需要对各字段做 URL encode。
  3. 在生成签名拼接 appKey+q+salt+密钥 字符串时,q 不需要做 URL encode,在生成签名之后,发送 HTTP 请求之前才需要对要发送的 img 做URL encode。
  4. 签名和请求有疑问,可以参考文档最后的常用语言demo

输出结果

返回的结果是json格式,具体说明如下:

字段类型字段说明
errorCodetext错误码
ResultjsonObject错误
-orientationtext识别方向
-regionstext区域
--boundingBoxtext识别区域坐标
--dirtext方向:行或列
--linesjsonArray行数组
---boundingBoxtext行坐标信息
---text_heighttext行高
---texttext识别的内容
---typetext内容类型:文本或公式

示例

返回结果示例:

{
    "errorCode": "0",
    "Result": {
        "orientation": "",
        "regions": [{
            "boundingBox": "13,15,584,15,584,83,13,83",
            "dir": "h",
            "lang": "",
            "lines": [
                [{
                    "boundingBox": "13,22,110,22,110,76,13,76",
                    "text_height": 53,
                    "words": [{
                        "boundingBox": "13,23,74,22,74,75,13,76",
                        "word": "13."
                    }, {
                        "boundingBox": "86,22,110,22,110,75,86,75",
                        "word": "若"
                    }],
                    "text": "13.若",
                    "type": "text"
                }, {
                    "boundingBox": "111,15,326,15,326,82,111,82",
                    "text_height": 67,
                    "words": [{
                        "boundingBox": "111,15,326,15,326,82,111,82",
                        "word": " x = ( \\sqrt { 3 } { - 5 } ) ^ { 3 }"
                    }],
                    "text": "x = ( \\sqrt { 3 } { - 5 } ) ^ { 3 }",
                    "type": "formula"
                }, {
                    "boundingBox": "327,22,384,22,384,76,327,76",
                    "text_height": 53,
                    "words": [{
                        "boundingBox": "327,22,338,22,338,75,327,75",
                        "word": ","
                    }, {
                        "boundingBox": "350,23,384,23,384,76,350,76",
                        "word": "则"
                    }],
                    "text": ",则",
                    "type": "text"
                }, {
                    "boundingBox": "385,15,584,15,584,83,385,83",
                    "text_height": 67,
                    "words": [{
                        "boundingBox": "385,16,584,15,584,82,385,83",
                        "word": " \\sqrt { - x - 1 } ="
                    }],
                    "text": "\\sqrt { - x - 1 } =",
                    "type": "formula"
                }]
            ]
        }],
        "exif": "UP"
    }
}

错误代码列表

错误码含义
101缺少必填的参数,出现这个情况还可能是et的值和实际加密方式不对应
102不支持的语言类型
103翻译文本过长
104不支持的API类型
105不支持的签名类型
106不支持的响应类型
107不支持的传输加密类型
108appKey无效,注册账号, 登录后台创建应用和实例并完成绑定, 可获得应用ID和密钥等信息,其中应用ID就是appKey( 注意不是应用密钥)
109batchLog格式不正确
110无相关服务的有效实例
111开发者账号异常
113请求q不能为空
201解密失败,可能为DES,BASE64,URLDecode的错误
202签名检验失败
203访问IP地址不在可访问IP列表
205请求的接口与应用的平台类型不一致,如有疑问请参考入门指南
206因为时间戳无效导致签名校验失败
207重放请求
301辞典查询失败
302小语种查询失败
303服务端的其它异常
401账户已经欠费停
411访问频率受限,请稍后访问
412大图片请求过于频繁,请稍后访问
10001无效的OCR类型
10002不支持的OCR image类型
10004识别图片过大
10201图片base64解密失败
10301OCR段落识别失败
10411访问频率受限
10412超过最大识别流量

常见问题及注意事项

  • 返回110

应用没有绑定服务实例,可以新建服务实例,绑定服务实例。

  • 返回108

appKey无效,注册账号, 登录后台创建应用和实例并完成绑定, 可获得应用ID和密钥等信息,其中应用ID就是appKey( 注意不是应用密钥)

  • 返回101

首先确保必填参数齐全,然后,确认参数书写是否正确。

  • 返回202

如果确认 appKeyappSecret 的正确性,仍返回202,一般是编码问题。请确保 q 为UTF-8编码.

版本更新记录

上线日期版本号更新内容
2019-05-23v1.0.0整题识别(含公式)API上线,支持识别中英文场景下的公式,返回可编辑的latex结果

常用语言 Demo

Java 示例

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class OcrV3Demo {

    private static Logger logger = LoggerFactory.getLogger(OcrV3Demo.class);

    private static final String YOUDAO_URL = "https://openapi.youdao.com/ocr_formula";

    private static final String APP_KEY = "您的应用ID";

    private static final String APP_SECRET = "您的应用密钥";

    public static void main(String[] args) throws IOException {

        Map<String,String> params = new HashMap<String,String>();
        String q = loadAsBase64("图片的路径");
        String salt = String.valueOf(System.currentTimeMillis());
        String detectType = "识别类型";
        String imageType = "1";
        params.put("detectType", detectType);
        params.put("imageType", imageType);
        params.put("img", q);
        params.put("docType", "json");
        params.put("signType", "v3");
        String curtime = String.valueOf(System.currentTimeMillis() / 1000);
        params.put("curtime", curtime);
        String signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET;
        String sign = getDigest(signStr);
        params.put("appKey", APP_KEY);
        params.put("salt", salt);
        params.put("sign", sign);
        String result = requestForHttp(YOUDAO_URL,params);
        /** 处理结果 */
        System.out.println(result);
    }

    public static String requestForHttp(String url,Map<String,String> params) throws IOException {
        String result = "";

        /** 创建HttpClient */
        CloseableHttpClient httpClient = HttpClients.createDefault();

        /** httpPost */
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
        Iterator<Map.Entry<String,String>> it = params.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String,String> en = it.next();
            String key = en.getKey();
            String value = en.getValue();
            paramsList.add(new BasicNameValuePair(key,value));
        }
        httpPost.setEntity(new UrlEncodedFormEntity(paramsList,"UTF-8"));
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        try{
            HttpEntity httpEntity = httpResponse.getEntity();
            result = EntityUtils.toString(httpEntity,"UTF-8");
            EntityUtils.consume(httpEntity);
        }finally {
            try{
                if(httpResponse!=null){
                    httpResponse.close();
                }
            }catch(IOException e){
                logger.info("## release resouce error ##" + e);
            }
        }
        return result;
    }

    /**
     * 生成加密字段
     */
    public static String getDigest(String string) {
        if (string == null) {
            return null;
        }
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        byte[] btInput = string.getBytes();
        try {
            MessageDigest mdInst = MessageDigest.getInstance("SHA-256");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (byte byte0 : md) {
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }

    public static String loadAsBase64(String imgFile)
  {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理

    File file = new File(imgFile);
    if(!file.exists()){
        logger.error("文件不存在");
        return null;
    }
    InputStream in = null;
    byte[] data = null;
      //读取图片字节数组
    try
    {
        in = new FileInputStream(imgFile);
        data = new byte[in.available()];
        in.read(data);
        in.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
      //对字节数组Base64编码
    return Base64.getEncoder().encodeToString(data);//返回Base64编码过的字节数组字符串
  }

  public static String truncate(String q) {
        if (q == null) {
            return null;
        }
        int len = q.length();
        String result;
        return len <= 20 ? q : (q.substring(0, 10) + len + q.substring(len - 10, len));
    }
}

Python 示例

# -*- coding: utf-8 -*-
import sys
import uuid
import requests
import base64
import hashlib
import time

reload(sys)
sys.setdefaultencoding('utf-8')

YOUDAO_URL = 'https://openapi.youdao.com/ocr_formula'
APP_KEY = '您的应用ID'
APP_SECRET = '您的应用密钥'


def truncate(q):
    if q is None:
        return None
    size = len(q)
    return q if size <= 20 else q[0:10] + str(size) + q[size - 10:size]


def encrypt(signStr):
    hash_algorithm = hashlib.sha256()
    hash_algorithm.update(signStr.encode('utf-8'))
    return hash_algorithm.hexdigest()


def do_request(data):
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    return requests.post(YOUDAO_URL, data=data, headers=headers)


def connect():
    f = open(r'图片的路径', 'rb')  # 二进制方式打开图文件
    q = base64.b64encode(f.read())  # 读取文件内容,转换为base64编码
    f.close()

    data = {}
    data['detectType'] = '识别类型'
    data['imageType'] = '1'
    data['img'] = q
    data['docType'] = 'json'
    data['signType'] = 'v3'
    curtime = str(int(time.time()))
    data['curtime'] = curtime
    salt = str(uuid.uuid1())
    signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET
    sign = encrypt(signStr)
    data['appKey'] = APP_KEY
    data['salt'] = salt
    data['sign'] = sign

    response = do_request(data)
    print response.content


if __name__ == '__main__':
    connect()

C# 示例

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.Security.Cryptography;

namespace zhiyun_csharp_demo
{
    class OcrV3Demo
    {
        public static void Main()
        {
            Dictionary dic = new Dictionary();
            string url = "https://openapi.youdao.com/ocr_formula";
            string q = LoadAsBase64("图片的路径");
            string appKey = "您的应用ID";
            string appSecret = "您的应用密钥";
            string salt = DateTime.Now.Millisecond.ToString();
            string detectType = "识别类型";
            string imageType = "1";
            dic.Add("detectType", detectType);
            dic.Add("imageType", imageType);
            dic.Add("docType", "json");
            dic.Add("signType", "v3");
            dic.Add("img", System.Web.HttpUtility.UrlEncode(q));
            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
            long millis = (long) ts.TotalMilliseconds;
            string curtime = Convert.ToString(millis / 1000);
            dic.Add("curtime", curtime);
            string signStr = appKey + Truncate(q) + salt + curtime + appSecret;;
            string sign = ComputeHash(signStr, new SHA256CryptoServiceProvider());
            dic.Add("appKey", appKey);
            dic.Add("salt", salt);
            dic.Add("sign", sign);
            string result = Post(url, dic);
            Console.WriteLine(result);
        }

        protected static string ComputeHash(string input, HashAlgorithm algorithm)
        {
          Byte[] inputBytes = Encoding.UTF8.GetBytes(input);
          Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);
          return BitConverter.ToString(hashedBytes).Replace("-", "");
        }

        protected static string Truncate(string q)
        {
            if (q == null)
            {
               return null;
            }
            int len = q.Length;
            return len <= 20 ? q : (q.Substring(0, 10) + len + q.Substring(len - 10, 10));
        }

        protected static string LoadAsBase64(string filename)
        {
            try
            {
                FileStream filestream = new FileStream(filename, FileMode.Open);
                byte[] arr = new byte[filestream.Length];
                filestream.Position = 0;
                filestream.Read(arr, 0, (int)filestream.Length);
                filestream.Close();
                return Convert.ToBase64String(arr);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        protected static string Post(string url, Dictionary dic)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            StringBuilder builder = new StringBuilder();
            int i = 0;
            foreach (var item in dic)
            {
                if (i > 0)
                    builder.Append("&");
                builder.AppendFormat("{0}={1}", item.Key, item.Value);
                i++;
            }
            byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }
    }
}

PHP 示例

<?php
define("CURL_TIMEOUT",   2000);
define("URL",            "https://openapi.youdao.com/ocr_formula");
define("APP_KEY",        "您的应用ID"); // 替换为您的应用ID
define("SEC_KEY",        "您的应用密钥"); // 替换为您的密钥

function do_request($q)
{
     $salt = create_guid();
     $args = array(
        'appKey' => APP_KEY,
        'salt' => $salt,
     );
     $detectType = '识别类型';
     $imageType = '1';
     $args['detectType'] = $detectType;
     $args['imageType'] = $imageType;
     $args['img'] = $q;
     $args['docType'] = 'json';
     $args['signType'] = 'v3';
     $curtime = strtotime("now");
     $args['curtime'] = $curtime;
     $signStr = APP_KEY . truncate($q) . $salt . $curtime . SEC_KEY;
     $args['sign'] = hash("sha256", $signStr);
     $ret = call(URL, $args);
     print_r($ret);
     $ret = json_decode($ret, true);
     return $ret;
}

// 发起网络请求
function call($url, $args=null, $method="post", $testflag = 0, $timeout = CURL_TIMEOUT, $headers=array())
{
    $ret = false;
    $i = 0;
    while($ret === false)
    {
        if($i > 1)
            break;
        if($i > 0)
        {
            sleep(1);
        }
         $ret = callOnce($url, $args, $method, false, $timeout, $headers);
         $i++;
    }
    return $ret;
}

function callOnce($url, $args=null, $method="post", $withCookie = false, $timeout = CURL_TIMEOUT, $headers=array())
{
    $ch = curl_init();
    if($method == "post")
    {
        $data = convert($args);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_POST, 1);
    }
    else
    {
        $data = convert($args);
        if($data)
        {
            if(stripos($url, "?") > 0)
            {
                 $url .= "&$data";
            }
            else
            {
                 $url .= "?$data";
            }
        }
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if(!empty($headers))
    {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    if($withCookie)
    {
        curl_setopt($ch, CURLOPT_COOKIEJAR, $_COOKIE);
    }
    $r = curl_exec($ch);
    curl_close($ch);
    return $r;
}

function convert(&$args)
{
    $data = '';
    if (is_array($args))
    {
        foreach ($args as $key=>$val)
        {
            if (is_array($val))
            {
                foreach ($val as $k=>$v)
                {
                     $data .= $key.'['.$k.']='.rawurlencode($v).'&';
                }
            }
            else
            {
                     $data .="$key=".rawurlencode($val)."&";
            }
        }
        return trim($data, "&");
    }
    return $args;
}

// uuid generator
function create_guid(){
    $microTime = microtime();
    list($a_dec, $a_sec) = explode(" ", $microTime);
    $dec_hex = dechex($a_dec* 1000000);
    $sec_hex = dechex($a_sec);
    ensure_length($dec_hex, 5);
    ensure_length($sec_hex, 6);
    $guid = "";
    $guid .= $dec_hex;
    $guid .= create_guid_section(3);
    $guid .= '-';
    $guid .= create_guid_section(4);
    $guid .= '-';
    $guid .= create_guid_section(4);
    $guid .= '-';
    $guid .= create_guid_section(4);
    $guid .= '-';
    $guid .= $sec_hex;
    $guid .= create_guid_section(6);
    return $guid;
}

function truncate($q) {
    $len = strlen($q);
    return $len <= 20 ? $q : (substr($q, 0, 10) . $len . substr($q, $len - 10, $len));
}

function ensure_length(&$string, $length){
    $strlen = strlen($string);
    if($strlen < $length)
    {
         $string = str_pad($string, $length, "0");
    }
    else if($strlen > $length)
    {
         $string = substr($string, 0, $length);
    }
}

function create_guid_section($characters){
    $return = "";
    for($i = 0; $i < $characters; $i++)
    {
         $return .= dechex(mt_rand(0,15));
    }
    return $return;
}
// 输入
$file = "图片的路径";
$fp = fopen($file, "r") or die("Can't open file");
// base64编码
$q = base64_encode(fread($fp, filesize($file)));
fclose($fp);
do_request($q);
?>