我写了一个OCR测试工具:DeepSeekOCR、PaddleOCR 和 混元OCR

大家好,我是 Ai 学习的老章

最近的 OCR 大模型我都做了本地部署和测试,还写了一个 API 统一对接这三个模型

腾讯混元 OCR 大模型,本地部署,实测

本地部署 PaddleOCR,消费级显卡轻松跑,支持本地图片和 PDF 文件

DeepSeek-OCR 本地部署(上):CUDA 升级 12.9,vLLM 升级至最新稳定版

DeepSeek-OCR 本地部署(下):vLLM 离线推理,API 重写,支持本地图片、PDF 解析

很多同学问选哪个?

成年人怎么还在做选择呢,必须全都要啊

我用 FastAPI 框架撸了一个简单的 OCR 模型对比工具 (OCR Comparison Tool),可以实现同样的提示词 + 图片/PDF,利用 Python 多线程并行调用 DeepSeek、Paddle 和 混元这三个模型的 API 进行解析,并将结果并排展示。

前端其实纯 HTML+CSS+js 实现,为了内网部署,不依赖任何 CND。

使用也很简单,图片/PDF 上传之后,输入提示词,没有特殊要求,使用默认就行。

点击 Run OCR Comparison 即可

三者都很快,内置了轻量级 Markdown 解析其,自动渲染结果。

也可以切换到识别后的原始 Markdown,支持一键 copy

核心代码如下(完整代码接近 600 行,大多是 HTML 相关):

感兴趣的同学可以试试,OCR 模型 API 部分替换成官方/第三方的 API

这段代码稍作修改就可以在线部署运行了

python 复制代码
#!/usr/bin/env python3
"""
OCR Comparison Web App - 美化版,不依赖外部 CDN
"""
import os
import re
import shutil
import tempfile
import requests
from concurrent.futures import ThreadPoolExecutor

import uvicorn
from fastapi import FastAPI, File, Form, UploadFile
from fastapi.responses import HTMLResponse

app = FastAPI(title="OCR Comparison")

# --- Configuration ---
MODELS = {
    "DeepSeek-OCR": "http://",
    "PaddleOCR": "http://",
    "HunyuanOCR": "http://",
}


def call_api(model_name, api_url, file_path, prompt):
    """调用单个 OCR API"""
    print(f"[INFO] Calling {model_name}: {api_url}")
    try:
        with open(file_path, 'rb') as f:
            resp = requests.post(
                api_url,
                files={'file': (os.path.basename(file_path), f)},
                data={'prompt': prompt},
                timeout=300
            )
        print(f"[INFO] {model_name} status: {resp.status_code}")
        if resp.status_code == 200:
            data = resp.json()
            result = data.get("result", str(data))
            print(f"[INFO] {model_name} result length: {len(result)}")
            return result
        return f"HTTP Error: {resp.status_code}"
    except Exception as e:
        print(f"[ERROR] {model_name}: {e}")
        return f"Error: {e}"


HTML_PAGE = """
<!DOCTYPE html>
省略
</html>
"""


@app.get("/", response_class=HTMLResponse)
async def index():
    return HTML_PAGE


@app.post("/api/compare")
async def compare(
    file: UploadFile = File(...),
    prompt: str = Form("Convert the document to markdown.")
):
    print(f"\n{'='*60}")
    print(f"[INFO] Received request: {file.filename}")
    print(f"[INFO] Prompt: {prompt[:50]}...")
    print(f"{'='*60}")
    
    temp_dir = tempfile.mkdtemp()
    temp_path = os.path.join(temp_dir, file.filename)
    
    try:
        with open(temp_path, "wb") as f:
            content = await file.read()
            f.write(content)
        
        print(f"[INFO] Saved to: {temp_path}, size: {len(content)} bytes")
        
        # 并行调用三个 API
        results = {}
        with ThreadPoolExecutor(max_workers=3) as executor:
            futures = {
                "deepseek": executor.submit(call_api, "DeepSeek-OCR", MODELS["DeepSeek-OCR"], temp_path, prompt),
                "paddle": executor.submit(call_api, "PaddleOCR", MODELS["PaddleOCR"], temp_path, prompt),
                "hunyuan": executor.submit(call_api, "HunyuanOCR", MODELS["HunyuanOCR"], temp_path, prompt),
            }
            
            for name, future in futures.items():
                try:
                    result = future.result(timeout=310)
                    results[name] = result
                    print(f"[INFO] {name} done, length: {len(result)}")
                except Exception as e:
                    results[name] = f"Error: {e}"
                    print(f"[ERROR] {name}: {e}")
        
        print(f"[INFO] All done. Returning results.")
        print(f"[DEBUG] Results keys: {list(results.keys())}")
        
        return results
        
    finally:
        shutil.rmtree(temp_dir, ignore_errors=True)


if __name__ == "__main__":
    print("\n" + "="*60)
    print("OCR Comparison Server")
    print("URL: http://0.0.0.0:8080")
    print("="*60 + "\n")
    
    uvicorn.run(app, host="0.0.0.0", port=8080)
相关推荐
梦想的颜色2 天前
OCR 识别原理与 Python 识图全实战:从文字提取到图像内容理解
python·计算机视觉·ocr·图像识别·python 识图
楚识科技3 天前
信创 OCR 四关:芯片/OS/加速卡/合规全适配实践
ocr
天远Date Lab3 天前
零信任架构实战:基于天远车信盟出险构建自动化汽车消费贷合规网关
人工智能·机器学习·计算机视觉·ocr
AI人工智能+3 天前
证件阅读机,通过光学扫描、AI大脑、芯片感应三双“眼睛”协同工作,借助深度学习OCR引擎完成证卡识别与结构化输出
深度学习·ocr·智能硬件·证件阅读机
天远数科4 天前
零信任架构实战:基于天远身份证OCR构建自动化高并发移动支付网关
人工智能·架构·自动化·ocr
聚美智数4 天前
机动车登记证书 OCR 识别-绿本信息结构化提取-车辆登记证书识别-机动车证件 OCR 解析 API 接口介绍
ocr
yychen_java4 天前
从像素到汉字:基于深度学习的中文OCR系统六步全链路剖析
人工智能·深度学习·ocr
天远Date Lab4 天前
计算机视觉管道构建:基于天远身份证OCR实现机器学习身份信息提取
人工智能·机器学习·计算机视觉·ocr
黄一一9112434 天前
告别手动敲字,本地截图 OCR,识别完直接复制!开源截图识别工具
windows·ocr·开源软件·贴图