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"])