Skip to content

About

ocr rest api 服务

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

OCR 微服务项目

这是一个基于 PaddleOCR 和 Flask 的 OCR(光学字符识别)微服务,支持图片和 PDF 文件的文字识别功能。

功能特点

  • 支持图片和 PDF 文件的文字识别
  • 支持指定区域识别(ROI)
  • 支持多页 PDF 处理
  • 提供异步处理提高性能
  • 提供 RESTful API 接口
  • 支持缩放处理

环境要求

  • Python 3.8+
  • 操作系统:Linux/Windows/macOS

快速开始

1. 克隆项目

git clone [项目地址]
cd ocr-micro-service

2. 创建虚拟环境(推荐)

python3 -m venv venv
source venv/bin/activate  # Linux/macOS
# 或
venv\Scripts\activate  # Windows

3. 配置 pip 源(可选,加速下载)

建议使用国内源,例如百度源:

pip config set global.index-url https://mirror.baidu.com/pypi/simple/

4. 安装依赖

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 gevent

5. 运行服务

开发模式

python main.py

服务将在 http://localhost:5000 启动

生产模式

gunicorn main:app -c gunicorn.conf.py

或使用提供的脚本:

bash run.sh

API 接口

1. 首页

  • URL: /
  • 方法: GET
  • 描述: 获取简单的测试页面

2. OCR 识别

  • URL: /ocr

  • 方法: POST

  • 描述: 上传图片或 PDF 文件进行文字识别

  • 参数:

    • file: 上传的文件(图片或 PDF)
    • zoom (可选): 缩放比例,默认 100.0
    • rects (可选): 指定识别区域的坐标,格式为 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 部署

1. 构建镜像

docker build -t ocr-micro-service .

2. 运行容器

docker run -p 5001:5001 ocr-micro-service

3. 使用预构建镜像

docker pull izerui/ocr

性能优化建议

  1. GPU 加速: 如果有 NVIDIA GPU,安装 paddlepaddle-gpu 以提高识别速度
  2. 并发设置: 在 gunicorn.conf.py 中根据服务器资源调整 workers 数量
  3. 区域指定: 对于大图片,指定 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/                 # 本地模型文件(可选)

常见问题

1. 安装 paddlepaddle 失败

确保系统满足要求,并尝试使用国内镜像源:

pip config set global.index-url https://mirror.baidu.com/pypi/simple/
pip install paddlepaddle

2. OCR 识别速度慢

  • 尝试使用 GPU 版本 paddlepaddle
  • 指定识别区域减少处理面积
  • 调整 Gunicorn workers 数量

3. 内存不足

  • 减少 Gunicorn workers 数量
  • 对于大文件,考虑分片处理

贡献指南

欢迎提交 Issue 和 Pull Request 来改进此项目。

许可证

[请根据您的项目添加适当的许可证信息]


这个更新后的 README.md 文件提供了详细的项目初始化帮助说明,包括环境要求、安装步骤、API 使用方法、部署选项和常见问题解答。内容全面且结构清晰,可以帮助新用户快速上手这个 OCR 微服务项目。

About

ocr rest api 服务

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages