⚠️ 待更新·2026-08-29核验 · 更新时间待核验 · 本文信息可能已过期,请以官方文档为准
更新时间:2026-08-29 · 核验状态:待更新 · 官方溯源待补
引言
OCR(光学字符识别)和文档解析是许多 AI 应用的入口环节——RAG 知识库构建、发票自动录入、试卷数字化都离不开它。2026 年,免费 OCR 方案已经能处理多语言、复杂版式、手写体甚至数学公式。本文从纯本地到云端 API 两个维度,给出完整选型与调用实战。
方案对比
| 方案 |
类型 |
中文识别 |
表格/版式 |
公式 |
部署成本 |
| Tesseract 5 |
本地开源 |
良 |
弱 |
❌ |
CPU 即可 |
| PaddleOCR |
本地开源 |
优 |
优(PP-Structure) |
❌ |
CPU/GPU 均可 |
| Surya |
本地开源 |
优 |
优 |
部分 |
需 GPU |
| Mistral OCR |
云端 API |
优 |
优 |
✅ |
免费 1000 页/月 |
| Google Vision |
云端 API |
优 |
优 |
❌ |
1000 次/月免费 |
| Mathpix |
云端 API |
良 |
良 |
✅ |
1000 次/月免费 |
本地方案:PaddleOCR 全流程
安装
pip install paddlepaddle paddleocr
基础文字识别
from paddleocr import PaddleOCR
ocr = PaddleOCR(use_angle_cls=True, lang='ch') # 中文+英文
result = ocr.ocr('invoice.png', cls=True)
for line in result[0]:
box, (text, conf) = line
print(f"[{conf:.2f}] {text}")
版面分析(表格 + 段落)
from paddleocr import PPStructure
table_engine = PPStructure(show_log=True, image_dir='./')
result = table_engine('document.png')
for region in result:
rtype = region['type'] # text, table, figure, title
if rtype == 'table':
html = region['res']['html']
print(f"TABLE HTML:\n{html}")
elif rtype == 'text':
text = region['res']
print(f"TEXT: {text}")
本地方案:Surya(多语言 + 版式)
pip install surya-ocr
from surya.recognition import RecognitionPredictor
from surya.detection import DetectionPredictor
# 自动检测语言,支持 90+ 种
det_predictor = DetectionPredictor()
rec_predictor = RecognitionPredictor()
from PIL import Image
img = Image.open('scan.png')
predictions = rec_predictor([img], langs=['zh', 'en'])
for page in predictions:
for line in page.text_lines:
print(line.text)
云端方案:Mistral OCR(推荐)
Mistral 在 2025 年底推出的 OCR API 能处理 PDF、图片,支持表格、公式、多语言,免费层每月 1000 页。
import requests, base64
MISTRAL_KEY = "your-mistral-api-key"
# 方式一:上传图片
with open('page.png', 'rb') as f:
img_b64 = base64.b64encode(f.read()).decode()
resp = requests.post("https://api.mistral.ai/v1/ocr", headers={
"Authorization": f"Bearer {MISTRAL_KEY}",
"Content-Type": "application/json"
}, json={
"model": "mistral-ocr-latest",
"document": {
"type": "image_url",
"image_url": f"data:image/png;base64,{img_b64}"
}
})
result = resp.json()
markdown_text = result['pages'][0]['markdown']
print(markdown_text)
# 方式二:直接传 PDF URL
resp = requests.post("https://api.mistral.ai/v1/ocr", headers={
"Authorization": f"Bearer {MISTRAL_KEY}",
"Content-Type": "application/json"
}, json={
"model": "mistral-ocr-latest",
"document": {
"type": "document_url",
"document_url": "https://example.com/paper.pdf"
}
})
for page in resp.json()['pages']:
print(page['markdown'])
云端方案:Google Vision API
from google.cloud import vision
import io
client = vision.ImageAnnotatorClient()
with io.open('receipt.jpg', 'rb') as f:
content = f.read()
image = vision.Image(content=content)
response = client.document_text_detection(image=image)
for page in response.full_text_annotation.pages:
for block in page.blocks:
text = ''.join(sym.text for par in block.paragraphs
for sym in par.symbols)
print(f"[{block.block_type}] {text}")
选型建议
- 纯中文 / 离线场景:PaddleOCR 是首选,PP-Structure 的表格识别已达到生产可用。
- 多语言 / 学术文档:Surya 支持 90+ 种语言,版面分析能力强。
- PDF / 含公式 / 含表格:Mistral OCR 输出 Markdown,直接可喂给 LLM 做 RAG,免费 1000 页/月足够原型验证。
- 发票 / 票据:Google Vision 的
document_text_detection 对复杂版式鲁棒性好。
- 数学公式:Mathpix 仍是公式识别的标杆,1000 次/月免费额度。
RAG 集成示例
# OCR → Markdown → Embedding → 向量库
from mistral_ocr_pipeline import ocr_document # 上面的 Mistral 调用
from openai import OpenAI
client = OpenAI(base_url="https://openrouter.ai/api/v1", api_key="sk-or-...")
def build_rag_from_pdf(pdf_url):
# 1. OCR 提取 Markdown
pages = ocr_document(pdf_url)
# 2. 分段
chunks = []
for i, page_md in enumerate(pages):
# 按段落切分
for para in page_md.split('\n\n'):
if len(para.strip()) > 50:
chunks.append({"page": i+1, "text": para.strip()})
# 3. 向量化(用免费 Embedding API)
embeddings = []
for chunk in chunks:
resp = client.embeddings.create(
model="bge-m3:free",
input=chunk["text"]
)
embeddings.append(resp.data[0].embedding)
# 4. 存入向量库(如 ChromaDB / Qdrant)
return chunks, embeddings
chunks, embs = build_rag_from_pdf("https://example.com/report.pdf")
print(f"Indexed {len(chunks)} chunks")
注意事项
- 图片预处理:扫描件先做去倾斜、去噪、二值化,识别率可提升 10-30%。
- PDF 分页:大 PDF 先拆页再逐页 OCR,避免超时和内存溢出。
- 表格还原:PaddleOCR 的 PP-Structure 输出 HTML,Mistral 输出 Markdown 表格,根据下游需求选择。
- 隐私合规:含敏感信息的文档避免用云端 API,改用本地 PaddleOCR/Surya。
- 成本估算:Mistral OCR 免费 1000 页/月,超出后约 $0.01/页;Google Vision 免费 1000 次/月,超出 $1.5/1000 次。
⚠️ Pending Update · 2026-08-29 Verification · Content may be outdated, please refer to official docs
Updated: 2026-08-29 · Status: Pending Verification
Introduction
OCR (Optical Character Recognition) and document parsing are the entry point for many AI applications — RAG knowledge base construction, invoice automation, exam digitization all depend on it. In 2026, free OCR solutions can handle multilingual text, complex layouts, handwriting, and even mathematical formulas. This article provides a complete selection guide and hands-on tutorial across local and cloud API dimensions.
Solution Comparison
| Solution |
Type |
Chinese OCR |
Tables/Layout |
Formulas |
Cost |
| Tesseract 5 |
Local OSS |
Good |
Weak |
❌ |
CPU only |
| PaddleOCR |
Local OSS |
Excellent |
Excellent (PP-Structure) |
❌ |
CPU/GPU |
| Surya |
Local OSS |
Excellent |
Excellent |
Partial |
GPU needed |
| Mistral OCR |
Cloud API |
Excellent |
Excellent |
✅ |
1000 pages/month free |
| Google Vision |
Cloud API |
Excellent |
Excellent |
❌ |
1000 requests/month free |
| Mathpix |
Cloud API |
Good |
Good |
✅ |
1000 requests/month free |
Local: PaddleOCR Full Pipeline
Installation
pip install paddlepaddle paddleocr
Basic Text Recognition
from paddleocr import PaddleOCR
ocr = PaddleOCR(use_angle_cls=True, lang='ch') # Chinese + English
result = ocr.ocr('invoice.png', cls=True)
for line in result[0]:
box, (text, conf) = line
print(f"[{conf:.2f}] {text}")
Layout Analysis (Tables + Paragraphs)
from paddleocr import PPStructure
table_engine = PPStructure(show_log=True, image_dir='./')
result = table_engine('document.png')
for region in result:
rtype = region['type'] # text, table, figure, title
if rtype == 'table':
html = region['res']['html']
print(f"TABLE HTML:\n{html}")
elif rtype == 'text':
text = region['res']
print(f"TEXT: {text}")
Local: Surya (Multilingual + Layout)
pip install surya-ocr
from surya.recognition import RecognitionPredictor
from surya.detection import DetectionPredictor
det_predictor = DetectionPredictor()
rec_predictor = RecognitionPredictor()
from PIL import Image
img = Image.open('scan.png')
predictions = rec_predictor([img], langs=['zh', 'en'])
for page in predictions:
for line in page.text_lines:
print(line.text)
Cloud: Mistral OCR (Recommended)
Mistral's OCR API, launched in late 2025, handles PDFs and images with support for tables, formulas, and multilingual text. The free tier offers 1000 pages per month.
import requests, base64
MISTRAL_KEY = "your-mistral-api-key"
# Method 1: Upload image
with open('page.png', 'rb') as f:
img_b64 = base64.b64encode(f.read()).decode()
resp = requests.post("https://api.mistral.ai/v1/ocr", headers={
"Authorization": f"Bearer {MISTRAL_KEY}",
"Content-Type": "application/json"
}, json={
"model": "mistral-ocr-latest",
"document": {
"type": "image_url",
"image_url": f"data:image/png;base64,{img_b64}"
}
})
result = resp.json()
markdown_text = result['pages'][0]['markdown']
print(markdown_text)
# Method 2: Pass PDF URL directly
resp = requests.post("https://api.mistral.ai/v1/ocr", headers={
"Authorization": f"Bearer {MISTRAL_KEY}",
"Content-Type": "application/json"
}, json={
"model": "mistral-ocr-latest",
"document": {
"type": "document_url",
"document_url": "https://example.com/paper.pdf"
}
})
for page in resp.json()['pages']:
print(page['markdown'])
Cloud: Google Vision API
from google.cloud import vision
import io
client = vision.ImageAnnotatorClient()
with io.open('receipt.jpg', 'rb') as f:
content = f.read()
image = vision.Image(content=content)
response = client.document_text_detection(image=image)
for page in response.full_text_annotation.pages:
for block in page.blocks:
text = ''.join(sym.text for par in block.paragraphs
for sym in par.symbols)
print(f"[{block.block_type}] {text}")
Selection Guidance
- Chinese-only / offline: PaddleOCR is the top choice; PP-Structure's table recognition is production-ready.
- Multilingual / academic docs: Surya supports 90+ languages with strong layout analysis.
- PDFs with formulas and tables: Mistral OCR outputs Markdown, directly usable for LLM-based RAG. 1000 free pages/month is sufficient for prototyping.
- Invoices / receipts: Google Vision's
document_text_detection is robust on complex layouts.
- Math formulas: Mathpix remains the gold standard for formula recognition, with 1000 free requests/month.
RAG Integration Example
# OCR → Markdown → Embedding → Vector DB
from mistral_ocr_pipeline import ocr_document # Mistral call above
from openai import OpenAI
client = OpenAI(base_url="https://openrouter.ai/api/v1", api_key="sk-or-...")
def build_rag_from_pdf(pdf_url):
# 1. OCR extract Markdown
pages = ocr_document(pdf_url)
# 2. Chunk
chunks = []
for i, page_md in enumerate(pages):
for para in page_md.split('\n\n'):
if len(para.strip()) > 50:
chunks.append({"page": i+1, "text": para.strip()})
# 3. Embed (using free Embedding API)
embeddings = []
for chunk in chunks:
resp = client.embeddings.create(
model="bge-m3:free",
input=chunk["text"]
)
embeddings.append(resp.data[0].embedding)
# 4. Store in vector DB (e.g., ChromaDB / Qdrant)
return chunks, embeddings
chunks, embs = build_rag_from_pdf("https://example.com/report.pdf")
print(f"Indexed {len(chunks)} chunks")
Caveats
- Image preprocessing: Deskew, denoise, and binarize scanned documents first — recognition accuracy can improve 10-30%.
- PDF splitting: Split large PDFs into pages before OCR to avoid timeouts and memory overflow.
- Table restoration: PaddleOCR's PP-Structure outputs HTML; Mistral outputs Markdown tables. Choose based on downstream needs.
- Privacy compliance: For documents with sensitive information, use local PaddleOCR/Surya instead of cloud APIs.
- Cost estimation: Mistral OCR free 1000 pages/month, then ~$0.01/page; Google Vision free 1000 requests/month, then $1.5/1000 requests.