文字识别API
接口描述
文字识别接口,支持返回位置。
请求说明
URL
https://openapi.sys303.com/api/cognitive/ocr
参数
Header参数
| 参数 | 值 |
|---|---|
| Content-Type | multipart/form-data |
| X-Secret | 点击获取 |
Body参数
| 参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
|---|---|---|---|---|
| file | 是 | stream | - | 图像文件,大小不超过10M,最短边至少15px,最长边最大8192px |
| lang | 是 | int | [0, 1, 2, 3] | 识别语言类型,默认藏文【0:藏文,1:中文,2:英文,3:中藏混合】 |
请求样例
- bash
- python
- C#
- Java
curl --request POST \
--url https://openapi.sys303.com/api/cognitive/ocr \
--header 'Accept: */*' \
--header 'X-Secret: {X-Secret}' \
--header 'content-type: multipart/form-data' \
--form lang=0 \
--form 'file=@[object Object]'
# encoding:utf-8
import requests
url = "https://openapi.sys303.com/api/cognitive/ocr"
headers = {
"Accept": "*/*",
"X-Secret": "你的X-Secret"
}
files = {
"file": open("test.png", "rb") # 改成你的图片路径
}
data = {
"lang": "0"
}
response = requests.post(url, headers=headers, files=files, data=data)
print(response.status_code)
print(response.text)
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
var url = "https://openapi.sys303.com/api/cognitive/ocr";
var secret = "你的X-Secret";
var filePath = "test.png";
using var httpClient = new HttpClient();
using var form = new MultipartFormDataContent();
// lang 参数
form.Add(new StringContent("0"), "lang");
// 文件参数
var fileBytes = File.ReadAllBytes(filePath);
var fileContent = new ByteArrayContent(fileBytes);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
form.Add(fileContent, "file", Path.GetFileName(filePath));
// header
httpClient.DefaultRequestHeaders.Add("X-Secret", secret);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
var response = await httpClient.PostAsync(url, form);
var result = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Status: {response.StatusCode}");
Console.WriteLine(result);
}
}
import okhttp3.*;
import java.io.File;
import java.io.IOException;
public class OcrDemo {
public static void main(String[] args) throws IOException {
String url = "https://openapi.sys303.com/api/cognitive/ocr";
String secret = "你的X-Secret";
String filePath = "test.png";
OkHttpClient client = new OkHttpClient();
File file = new File(filePath);
RequestBody fileBody = RequestBody.create(
file,
MediaType.parse("application/octet-stream")
);
MultipartBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("lang", "0")
.addFormDataPart(
"file",
file.getName(),
fileBody
)
.build();
Request request = new Request.Builder()
.url(url)
.addHeader("Accept", "*/*")
.addHeader("X-Secret", secret)
.post(requestBody)
.build();
try (Response response = client.newCall(request).execute()) {
System.out.println("Status: " + response.code());
System.out.println(response.body().string());
}
}
}
返回说明
参数说明
| 字段 | 是否必选 | 类型 | 状态 | 说明 |
|---|---|---|---|---|
| traceId | 是 | String | - | 唯一的traceId,用于问题定位 |
| msgId | 是 | String | - | 业务状态码 |
| msg | 是 | String | 错误描述 | |
| data | 是 | dict | - | 识别结果数组 |
| + lines | 否 | string | - | 识别结果字符串 |
| ++ text | 否 | string | - | 当前行文字 |
| ++ left | 否 | int | - | 文本行top位置 |
| ++ top | 否 | int | - | 文本行left位置 |
| ++ width | 否 | int | - | 文本行宽度 |
| ++ height | 否 | int | - | 文本行高度 |
返回样例
成功
{
"traceId": "0HNL06O44AU0U:00000001",
"msgId": "2000000",
"msg": "success",
"data": {
"lines": [
{
"text": "识别文字1",
"left": 105,
"top": 45,
"width": 355,
"height": 74
},
{
"text": "识别文字2",
"left": 241,
"top": 513,
"width": 81,
"height": 38
}
]
}
}
失败
认证失败:
{
"traceId": "0HNLN3J9DCJSE:00000001",
"msgId": "4011001",
"msg": "AuthFailed",
"data": null
}
传入参数错误:
{
"traceId": "0HNL06O44AU14:00000001",
"msgId": "500",
"msg": "Specified argument was out of the range of valid values. (Parameter 'langType')",
"data": null
}
次数不足:
{
"traceId": "0HNL06O44AU1F:00000001",
"msgId": "2001001",
"msg": "InsufficientInventory",
"data": null
}