题目识别识别接口文档

调用地址:https://mquestion.market.alicloudapi.com/ocrservice/mathQuestion
请求方式:POST
返回类型:JSON

请求参数(Body):

{
//图像数据:base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,和url参数只能同时存在一个
"img": "",
//图像url地址:图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,和img参数只能同时存在一个
"url": ""
}

请求代码示例:

java版

    public static void main(String[] args) {
String url = "https://mquestion.market.alicloudapi.com/ocrservice/mathQuestion";
String appcode = "你自己的AppCode";
HashMap<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
//根据API的要求,定义相对应的Content-Type
headers.put("Content-Type", "application/json; charset=UTF-8");
//如果需要使用本地图片,需要将图片base64码放在img后面,如果使用网络图片,则需要将网络图片url放于url参数后面
String bodys = "{\"img\":\"\",\"url\":\"\"}";
try {
/**
* 重要提示如下:
* HttpClientUtils请从
* https://gitee.com/duguangdemo/publicclouddemo/blob/master/src/main/java/util/HttpClientUtils.java
* 下载
* HttpExecuteResponse请从
* https://gitee.com/duguangdemo/publicclouddemo/blob/master/src/main/java/util/HttpExecuteResponse.java
* 下载
*
* 相应的依赖请参照
* https://gitee.com/duguangdemo/publicclouddemo/blob/master/pom.xml
*/
HttpExecuteResponse response = HttpClientUtils.doPost(url,bodys, headers);
System.out.println(response.getResponseAsString());
System.out.println(response.toString());
// 需要检查response的headers信息时可用以下代码,方便排查问题用
// for (Object json : response.getHeaders()) {
// System.out.println(json);
// }

} catch (Exception e) {
e.printStackTrace();
}
}

c#版

//using System.IO;
//using System.Text;
//using System.Net;
//using System.Net.Security;
//using System.Security.Cryptography.X509Certificates;


private const String host = "https://mquestion.market.alicloudapi.com";
private const String path = "/ocrservice/mathQuestion";
private const String method = "POST";
private const String appcode = "你自己的AppCode";

static void Main(string[] args)
{
String querys = "";
String bodys = "{//图像数据:base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,和url参数只能同时存在一个\"img\":\"\",//图像url地址:图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,和img参数只能同时存在一个\"url\":\"\"}";
String url = host + path;
HttpWebRequest httpRequest = null;
HttpWebResponse httpResponse = null;

if (0 < querys.Length)
{
url = url + "?" + querys;
}

if (host.Contains("https://"))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
}
else
{
httpRequest = (HttpWebRequest)WebRequest.Create(url);
}
httpRequest.Method = method;
httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
//根据API的要求,定义相对应的Content-Type
httpRequest.ContentType = "application/json; charset=UTF-8";
if (0 < bodys.Length)
{
byte[] data = Encoding.UTF8.GetBytes(bodys);
using (Stream stream = httpRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
try
{
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
}
catch (WebException ex)
{
httpResponse = (HttpWebResponse)ex.Response;
}

Console.WriteLine(httpResponse.StatusCode);
Console.WriteLine(httpResponse.Method);
Console.WriteLine(httpResponse.Headers);
Stream st = httpResponse.GetResponseStream();
StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("\n");

}

public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}

PHP版:

<?php
$host = "https://mquestion.market.alicloudapi.com";
$path = "/ocrservice/mathQuestion";
$method = "POST";
$appcode = "你自己的AppCode";
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
//根据API的要求,定义相对应的Content-Type
array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
$querys = "";
$bodys = "{//图像数据:base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,和url参数只能同时存在一个\"img\":\"\",//图像url地址:图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式,和img参数只能同时存在一个\"url\":\"\"}";
$url = $host . $path;

$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if (1 == strpos("$".$host, "https://"))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
var_dump(curl_exec($curl));
?>

Python2:

import urllib, urllib2, sys
import ssl


host = 'https://mquestion.market.alicloudapi.com'
path = '/ocrservice/mathQuestion'
method = 'POST'
appcode = '你自己的AppCode'
querys = ''
bodys = {}
url = host + path

bodys[''] = "{\"img\":\"\",\"url\":\"\"}"
post_data = bodys['']
request = urllib2.Request(url, post_data)
request.add_header('Authorization', 'APPCODE ' + appcode)

request.add_header('Content-Type', 'application/json; charset=UTF-8')
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
response = urllib2.urlopen(request, context=ctx)
content = response.read()
if (content):
print(content)

Python3:

import urllib.request
import urllib.parse
import json
import time
import base64
with open('1.jpg', 'rb') as f: # 以二进制读取本地图片
data = f.read()
encodestr = str(base64.b64encode(data),'utf-8')
#请求头
# 请修改为你自己的appcode,可从云市场订单或者api网关处获得
AppCode = "你自己的AppCode"
headers = {
'Authorization': 'APPCODE ' + AppCode,
'Content-Type': 'application/json; charset=UTF-8'
}

def posturl(url,data={}):
try:
params=json.dumps(dict).encode(encoding='UTF8')
req = urllib.request.Request(url, params, headers)
r = urllib.request.urlopen(req)
html =r.read()
r.close();
return html.decode("utf8")
except urllib.error.HTTPError as e:
print(e.code)
print(e.read().decode("utf8"))
time.sleep(1)
if __name__=="__main__":
url_request="https://mquestion.market.alicloudapi.com/ocrservice/mathQuestion"
dict = {'img': encodestr}

html = posturl(url_request, data=dict)
print(html)

正常返回示例:

{
//唯一id,用于问题定位
"sid": "9902c7f5ef43f9983cb8ce3d017addb418aa6c0f357e93080e444e30b800cb54eb5dd7cc",
//算法版本
"prism_version": "1.0.9",
//识别的文字块的数量,prism_wordsInfo数组大小
"prism_wnum": 6,
//识别的文字的具体内容
"prism_wordsInfo": [
{
//文字块
"word": "9.对于任意实数a,下列各式一定成立的是",
//文字块的位置,按照文字块四个角的坐标顺时针排列,分别为左上XY坐标、右上XY坐标、右下XY坐标、左下XY坐标
"pos": [
{
"x": 1,
"y": 12
},
{
"x": 532,
"y": 12
},
{
"x": 532,
"y": 45
},
{
"x": 1,
"y": 44
}
],
"direction": 0,
// 文字属性分类:{0, "中文印刷"}, {1, "拉丁语种"}, {2, "手写体"}, {3, "韩语"}, {4, "泰文"} ,{51,"公式"}
"recClassify": 0,
"angle": -90,
"x": 251,
"y": -237,
"width": 32,
"height": 532
},
{
"word": "()",
"pos": [
{
"x": 757,
"y": 10
},
{
"x": 860,
"y": 11
},
{
"x": 860,
"y": 41
},
{
"x": 757,
"y": 40
}
],
"direction": 0,
"recClassify": 0,
"angle": -89,
"x": 793,
"y": -26,
"width": 30,
"height": 104
},
{
"word": "A . \\sqrt { a ^ { 2 } - 1 } = \\sqrt { a - 1 } \\cdot \\sqrt { a + 1 }",
"pos": [
{
"x": 37,
"y": 56
},
{
"x": 402,
"y": 57
},
{
"x": 402,
"y": 94
},
{
"x": 37,
"y": 93
}
],
"direction": 0,
"recClassify": 51,
"angle": -89,
"x": 201,
"y": -106,
"width": 37,
"height": 365
},
{
"word": "B . \\sqrt { \\left( a + 6 \\right) ^ { 2 } } = a + 6",
"pos": [
{
"x": 37,
"y": 106
},
{
"x": 284,
"y": 107
},
{
"x": 284,
"y": 146
},
{
"x": 37,
"y": 145
}
],
"direction": 0,
"recClassify": 51,
"angle": -89,
"x": 141,
"y": 2,
"width": 38,
"height": 248
},
{
"word": "C . \\sqrt { - 1 6 \\cdot \\left( - a \\right) } = - 4 \\sqrt { - a }",
"pos": [
{
"x": 37,
"y": 156
},
{
"x": 409,
"y": 158
},
{
"x": 409,
"y": 196
},
{
"x": 37,
"y": 193
}
],
"direction": 0,
"recClassify": 51,
"angle": -89,
"x": 205,
"y": -10,
"width": 36,
"height": 372
},
{
"word": "D . \\sqrt { 2 5 a ^ { 4 } } = 5 a ^ { 2 }",
"pos": [
{
"x": 37,
"y": 208
},
{
"x": 223,
"y": 209
},
{
"x": 223,
"y": 245
},
{
"x": 37,
"y": 244
}
],
"direction": 0,
"recClassify": 51,
"angle": -89,
"x": 112,
"y": 134,
"width": 36,
"height": 187
}
],
"height": 265,
"width": 890,
"orgHeight": 265,
"orgWidth": 890,
"content": "9.对于任意实数a,下列各式一定成立的是 () A . \\sqrt { a ^ { 2 } - 1 } = \\sqrt { a - 1 } \\cdot \\sqrt { a + 1 } B . \\sqrt { \\left( a + 6 \\right) ^ { 2 } } = a + 6 C . \\sqrt { - 1 6 \\cdot \\left( - a \\right) } = - 4 \\sqrt { - a } D . \\sqrt { 2 5 a ^ { 4 } } = 5 a ^ { 2 } "
}

失败返回示例:

{
"error_code": 400,
"error_msg": "img和url参数不能同时存在"
}

错误码定义:

错误码错误信息描述
400参数错误具体错误请参考返回的error_msg
401您无该功能的权限,请开通后使用您无该功能的权限,请开通后使用
403购买的容量已用完或者签名错误购买的容量已用完或者签名错误
500服务器错误,请稍后重试服务器错误,请稍后重试