这是一个基于 PaddleOCR 和 Flask 的 OCR(光学字符识别)微服务,支持图片和 PDF 文件的文字识别功能。
- 支持图片和 PDF 文件的文字识别
- 支持指定区域识别(ROI)
- 支持多页 PDF 处理
- 提供异步处理提高性能
- 提供 RESTful API 接口
- 支持缩放处理
- Python 3.8+
- 操作系统:Linux/Windows/macOS
git clone [项目地址]
cd ocr-micro-servicepython3 -m venv venv
source venv/bin/activate # Linux/macOS
# 或
venv\Scripts\activate # Windows建议使用国内源,例如百度源:
pip config set global.index-url https://mirror.baidu.com/pypi/simple/pip install -r requirements.txt如果需要手动安装核心依赖,请按以下顺序执行:
# CPU 版本
pip install paddlepaddle paddleocr
# GPU 版本(如果有 NVIDIA GPU)
# pip install paddlepaddle-gpu paddleocr
# 版本选择参考: https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/windows-pip.html
pip install flask
pip install aiofiles
pip install gunicorn geventpython main.pygunicorn main:app -c gunicorn.conf.py或使用提供的脚本:
bash run.sh- URL:
/ - 方法:
GET - 描述: 获取简单的测试页面
-
URL:
/ocr -
方法:
POST -
描述: 上传图片或 PDF 文件进行文字识别
-
参数:
file: 上传的文件(图片或 PDF)zoom(可选): 缩放比例,默认 100.0rects(可选): 指定识别区域的坐标,格式为x0,y0,x1,y1;x0,y0,x1,y1;...
-
响应: XML 格式的识别结果
curl -X POST -F "[email protected]" -F "zoom=100" -F "rects=10,10,200,200;210,10,400,200" http://localhost:5000/ocr<result>
<number>1</number>
<zoom>100.0</zoom>
<pages>
<page index="0">
<content index="0" x0="10" y0="10" x1="200" y1="200" credibility="0.98,0.97,0.99">识别的文字内容</content>
<content index="1" x0="210" y0="10" x1="400" y1="200" credibility="0.96,0.95">另一个区域的文字</content>
</page>
</pages>
</result>docker build -t ocr-micro-service .docker run -p 5001:5001 ocr-micro-servicedocker pull izerui/ocr- GPU 加速: 如果有 NVIDIA GPU,安装
paddlepaddle-gpu以提高识别速度 - 并发设置: 在
gunicorn.conf.py中根据服务器资源调整workers数量 - 区域指定: 对于大图片,指定
rects参数只识别必要区域,提高处理速度
ocr-micro-service/
├── main.py # Flask 应用入口
├── ocr.py # OCR 处理核心逻辑
├── model.py # 数据模型定义
├── requirements.txt # 依赖列表
├── gunicorn.conf.py # Gunicorn 配置
├── run.sh # 启动脚本
├── Dockerfile # Docker 构建文件
├── templates/ # HTML 模板
├── static/ # 静态资源
└── whl/ # 本地模型文件(可选)
确保系统满足要求,并尝试使用国内镜像源:
pip config set global.index-url https://mirror.baidu.com/pypi/simple/
pip install paddlepaddle- 尝试使用 GPU 版本 paddlepaddle
- 指定识别区域减少处理面积
- 调整 Gunicorn workers 数量
- 减少 Gunicorn workers 数量
- 对于大文件,考虑分片处理
欢迎提交 Issue 和 Pull Request 来改进此项目。
[请根据您的项目添加适当的许可证信息]
这个更新后的 README.md 文件提供了详细的项目初始化帮助说明,包括环境要求、安装步骤、API 使用方法、部署选项和常见问题解答。内容全面且结构清晰,可以帮助新用户快速上手这个 OCR 微服务项目。