文字识别API
接口描述
文字识别接口,支持返回位置。
请求说明
URL
https://openapi.ocr.sys303.com/api/v1/ocr/general?access_token={token}
参数
URL参数
参数 | 值 |
---|---|
access_token | 通过API Key和Secret Key获取的access_token |
Header参数
参数 | 值 |
---|---|
Content-Type | application/x-www-form-urlencoded |
Body参数
参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
---|---|---|---|---|
image | 四选一 | string | - | 图像数据,base64编码后进行urlencode,大小不超过10M,最短边至少15px,最长边最大8192px,支持jpg/jpeg/png/bmp格式优先级:image > url > pdf_file > ofd_file |
language_type | 否 | string | 0 | 识别语言类型,默认藏文【0:藏文,1:中文】 |
url | 四选一 | string | 【开发中】 | 图片完整url,url长度不超过1024字节,url对应的图 片base64编码后大小不超过10M,最短边至少15px,最长边最大8192px,支持jpg/jpeg/png/bmp格式优先级 |
pdf_file | 四选一 | string | 【开发中】 | PDF文件,base64编码后进行urlencode,大小不超过10M,最短边至少15px,最长边最大8192px优先级:image > url > pdf_file > ofd_file |
ofd_file | 四选一 | string | 【开发中】 | OFD文件,base64编码后进行urlencode,大小不超过10M,最短边至少15px,最长边最大8192px优先级:image > url > pdf_file > ofd_file |
pdf_file_num | 否 | string | 【开发中】 | 需要识别的PDF文件的对应页码,当 pdf_file 参数有效时,识别传入页码的对应页面内容,若不传入,则默认识别第 1 页 |
ofd_file_num | 否 | string | 【开发中】 | 需要识别的OFD文件的对应页码,当 ofd_file 参数有效时,识别传入页码的对应页面内容,若不传入,则默认识别第 1 页 |
recognize_granularity | 否 | string | 【开发中】 | 是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置 |
detect_direction | 否 | string | 【开发中】 | 是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:- true:检测朝向- false:不检测朝向输入非正向图片时,若想要达到较好识别效果,建议将此参数设置为“true” |
vertexes_location | 否 | string | 【开发中】 | 是否返回文字外接多边形顶点位置,不支持单字位置。默认为false |
paragraph | 否 | string | 【开发中】 | 是否输出段落信息 |
probability | 否 | string | 【开发中】 | 是否返回识别结果中每一行的置信度 |
请求样例
- bash
- python
- C#
- Java
curl --request POST \
--url 'https://openapi.ocr.sys303.com/api/v1/ocr/general?access_token=【替换为有效的 access_token】' \
--header 'content-type: multipart/form-data' \
--form 'image=【替换为图片路径】' --form language_type=0
# encoding:utf-8
import requests
import base64
def main():
request_url = 'https://openapi.ocr.sys303.com/api/v1/ocr/general'
access_token= '【替换为有效的 access_token】'
# 二进制方式打开图片文件
f = open('【替换为图片路径】', 'rb')
img = base64.b64encode(f.read())
params = {"image": img}
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())
if __name__ == '__main__':
main()
using System;
using System.IO;
using System.Net;
using System.Text;
public class AccurateBasic
{
public static string RunAccurateBasic()
{
string requestUrl = "https://openapi.ocr.sys303.com/api/v1/ocr/general";
string accessToken = "【替换为有效的 access_token】";
string imagePath = "【替换为图片路径】";
// 拼接带 access_token 的完整 URL
string fullUrl = requestUrl + "?access_token=" + accessToken;
string base64Image = GetFileBase64(imagePath);
// 设置请求参数
string postData = "image=" + Uri.EscapeDataString(base64Image);
// 初始化 HTTP 请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
// 写入 POST 数据
byte[] data = Encoding.UTF8.GetBytes(postData);
request.ContentLength = data.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(data, 0, data.Length);
}
// 获取响应
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string result = reader.ReadToEnd();
Console.WriteLine(result);
return result;
}
}
catch (WebException ex)
{
using (StreamReader reader = new StreamReader(ex.Response.GetResponseStream()))
{
string errorResponse = reader.ReadToEnd();
Console.WriteLine("Error Response:");
Console.WriteLine(errorResponse);
}
throw;
}
}
// 将文件转换为 Base64
public static string GetFileBase64(string filePath)
{
byte[] fileBytes = File.ReadAllBytes(filePath);
return Convert.ToBase64String(fileBytes);
}
public static void Main(string[] args)
{
RunAccurateBasic();
}
}
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Base64;
public class RunAccurateBasic {
public static void main(String[] args) {
String requestUrl = "https://openapi.ocr.sys303.com/api/v1/ocr/general";
String accessToken = "【替换为有效的 access_token】";
String imagePath = "【替换为图片路径】";
try {
// 调用 OCR 接口
String result = runAccurateBasic(requestUrl, accessToken, imagePath);
System.out.println("OCR 识别结果: " + result);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String runAccurateBasic(String requestUrl, String accessToken, String imagePath) throws Exception {
// 将图片文件转换为 Base64 编码
String imageBase64 = encodeFileToBase64(imagePath);
// 拼接完整的请求 URL
String fullUrl = requestUrl + "?access_token=" + accessToken;
System.out.println("请求 URL: " + fullUrl);
// 打开 HTTP 连接
URL url = new URL(fullUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 构造表单参数
String params = "image=" + URLEncoder.encode(imageBase64, "UTF-8");
System.out.println("表单参数: " + params);
// 发送请求数据
try (OutputStream outputStream = connection.getOutputStream()) {
outputStream.write(params.getBytes("UTF-8"));
}
// 读取响应数据
int responseCode = connection.getResponseCode();
System.out.println("HTTP 响应码: " + responseCode);
if (responseCode == 200) {
StringBuilder response = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
}
return response.toString();
} else {
// 读取错误响应
StringBuilder errorResponse = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getErrorStream(), "UTF-8"))) {
String line;
while ((line = reader.readLine()) != null) {
errorResponse.append(line);
}
}
throw new Exception("Error Response: " + errorResponse.toString());
}
}
public static String encodeFileToBase64(String filePath) throws IOException {
File file = new File(filePath);
try (FileInputStream fis = new FileInputStream(file)) {
byte[] fileBytes = new byte[(int) file.length()];
fis.read(fileBytes);
return Base64.getEncoder().encodeToString(fileBytes);
}
}
}
返回说明
参数说明
字段 | 是否必选 | 类型 | 状态 | 说明 |
---|---|---|---|---|
log_id | 是 | uint64 | - | 唯一的log id,用于问题定位 |
words_result_num | 是 | uint32 | - | 识别结果数,表示words_result的元素个数 |
paragraphs_result_num | 是 | uint32 | 识别结果数,表示 paragraphs_result的元素个数 | |
words_result | 是 | array[] | - | 识别结果数组 |
+ words | 否 | string | - | 识别结果字符串 |
+ location | 否 | string | 【开发中】 | 字符串位置信息 |
++ top | 否 | string | 【开发中】 | 表示定位位置的长方形左上顶点的垂直坐标 |
++ left | 否 | string | 【开发中】 | 表示定位位置的长方形左上顶点的水平坐标 |
++ width | 否 | string | 【开发中】 | 表示定位位置的长方形的宽度 |
++ height | 否 | string | 【开发中】 | 表示定位位置的长方形的高度 |
+ probability | 否 | object | 【开发中】 | 识别结果中每一行的置信度值,包含average: 行置信度平均值,variance:行置信度方差,min:行置信度最小值,当 probability=true 时返回该字段 |
paragraphs_result | 否 | array[] | 【开发中】 | 段落检测结果,当 paragraph=true 时返回该字段 |
+ words_result_idx | 否 | array[] | 【开发中】 | 一个段落包含的行序号,当 paragraph=true 时返回该字段 |
pdf_file_size | 否 | string | 【开发中】 | 传入PDF文件的总页数,当 pdf_file 参数有效时返回该字段 |
ofd_file_size | 否 | string | 【开发中】 | 传入OFD文件的总页数,当 ofd_file 参数有效时返回该字段 |
direction | 否 | int32 | 【开发中】 | 图像方向,当 detect_direction=true 时返回该字段。- 1:未定义- 0:正向- 1:逆时针90度- 2:逆时针180度- 3:逆时针270度 |
返回样例
成功
{
"logId": 0,
"words_result_num": 2,
"words_result": [
{
"words": "藏文识别",
"location": {
"left": 0,
"top": 0,
"width": 0,
"height": 0
}
},
{
"words": " 晟邦科技",
"location": {
"left": 0,
"top": 0,
"width": 0,
"height": 0
}
}
]
}
失败
{
"error_code": 110,
"error_msg": "Access token invalid or no longer valid"
}