API 文档
通用文字识别 API
专注于中英文场景的高精度文字识别,以纯文本格式返回识别结果,适用于日常文档、截图等场景
概述
通用文字识别 API 可以从图像文件中提取文字,专门针对中英文文字识别进行了优化,具有高精度的识别效果。API 以纯文本格式返回识别结果,非常适用于日常文档、截图和一般文字提取任务。
它使用统一的 JSON 请求格式,支持 URL 引用或 base64 编码的图像数据。
身份验证
API 支持以下身份验证方法:
- API 密钥: 通过查询参数传递您的 API 密钥
?key=YOUR_API_KEY
从图像提取文字
使用高精度文字识别技术从图像文件中提取文字,专门针对中英文进行了优化。
请求
POST /api/text-recognition
参数:
参数 | 类型 | 必需 | 描述 |
---|---|---|---|
document | object | 是 | 文档对象 |
document.type | string | 是 | 固定值 "image_url" |
document.image_url | string | 是 | 图像 URL 或 base64 数据 |
filename | string | 否 | 文件名(推荐用于 base64 数据) |
key | string | 否 | API 密钥(查询参数,登录用户可选) |
支持的语言:
- 中文(中文)
- 英文(English)
示例:
使用图像 URL:
curl -X POST "https://llmocr.com/api/text-recognition?key=YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"document": {
"type": "image_url",
"image_url": "https://llmocr.com/image.jpg"
}
}'
使用 Base64 图像数据:
curl -X POST "https://llmocr.com/api/text-recognition?key=YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"document": {
"type": "image_url",
"image_url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD..."
},
"filename": "document.jpg"
}'
响应
成功响应 (200):
{
"id": "12345",
"filename": "document.jpg",
"content": "这是从图像中提取的文本内容。\n\nThis is the extracted text content from the image.",
"format": "text",
"timestamp": 1640995200000,
"payload": "https://llmocr.com/api/text-recognition?key=YOUR_API_KEY"
}
响应字段:
字段 | 类型 | 描述 |
---|---|---|
id | string | 已处理文档的唯一标识符 |
filename | string | 已处理图像的原始文件名 |
content | string | 以纯文本格式提取的文字内容 |
format | string | 输出格式(此 API 始终为 "text") |
timestamp | number | 处理时间戳(毫秒) |
payload | string | 用于处理的 API 端点 |
错误响应:
状态码 | 描述 |
---|---|
400 | 错误请求 - 无效的输入参数 |
401 | 未授权 - 无效或缺少 API 密钥 |
403 | 禁止访问 - 配额不足或权限不够 |
429 | 请求过多 - 超过速率限制 |
500 | 内部服务器错误 - 处理失败 |
错误响应格式:
{
"statusText": "错误描述"
}
支持的图像格式
- JPEG (.jpg, .jpeg)
- PNG (.png)
- GIF (.gif)
- WebP (.webp)
速率限制
- 免费版: 每月 30 次请求
- 基础版: 每月 1,000 次请求
- 专业版: 每月 5,000 次请求
- 旗舰版: 无限次请求
图像要求
- 最大文件大小: 10MB
- 最小分辨率: 100x100 像素
- 最大分辨率: 4000x4000 像素
- 支持格式: JPEG, PNG, GIF, WebP
最佳实践
- 图像质量: 使用高分辨率图像,文字清晰,以获得更好的准确性
- 文字对比度: 确保文字与背景有良好的对比度
- 图像方向: 确保文字方向正确(未旋转)
- 文件大小: 在保持质量的同时优化图像大小以加快处理速度
- 错误处理: 始终为 API 响应实现适当的错误处理
SDK 示例
JavaScript/Node.js
const response = await fetch('https://llmocr.com/api/text-recognition?key=YOUR_API_KEY', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
document: {
type: 'image_url',
image_url: 'https://example.com/image.jpg'
}
})
});
const result = await response.json();
console.log(result.content);
Python
import requests
url = "https://llmocr.com/api/text-recognition"
params = {"key": "YOUR_API_KEY"}
data = {
"document": {
"type": "image_url",
"image_url": "https://example.com/image.jpg"
}
}
response = requests.post(url, params=params, json=data)
result = response.json()
print(result["content"])