Skip to main content

基于 PaddleOCR 的离线 OCR 识别工具,使用 ONNX 模型进行推理

Project description

YDOCR-ONNX

基于 PaddleOCR 的离线 OCR 识别工具,使用 ONNX 模型进行推理。

特点

  • 完全离线运行,无需联网
  • 基于 PaddleOCR 的 PP-OCRv5 模型
  • 使用 ONNX 进行模型推理,性能优异
  • 支持文本检测、文本识别和方向分类
  • 支持中英文识别
  • 支持自定义模型路径

安装

pip install ydocr-onnx

使用方法

基本使用

from onnxocr.onnx_paddleocr import ONNXPaddleOcr
import cv2

# 初始化 OCR 引擎
ocr = ONNXPaddleOcr()

# 读取图片
img = cv2.imread('test.jpg')

# 执行 OCR 识别
result = ocr.ocr(img)

# 打印识别结果
print(result)

使用自定义模型

from onnxocr.onnx_paddleocr import ONNXPaddleOcr

# 指定模型路径
ocr = ONNXPaddleOcr(
    det_model_path='path/to/det.onnx',
    rec_model_path='path/to/rec.onnx',
    cls_model_path='path/to/cls.onnx'
)

参数说明

  • det_model_path: 检测模型路径
  • rec_model_path: 识别模型路径
  • cls_model_path: 方向分类模型路径
  • use_gpu: 是否使用 GPU 推理
  • use_angle_cls: 是否使用方向分类
  • lang: 识别语言,支持 'ch' 和 'en'

依赖要求

  • Python >= 3.7
  • opencv-python-headless >= 4.7
  • onnxruntime >= 1.14
  • numpy
  • requests
  • shapely
  • pyclipper
  • scikit-image

许可证

Apache License 2.0

贡献

欢迎提交 Issue 和 Pull Request!

致谢

本项目基于 PaddleOCR 开发。

🛠️ 环境安装

python>=3.6  

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt  

注意

  • 默认使用 PP-OCRv5_mobile-ONNX 模型。
  • 由于PyPI上传限制(100MB),模型文件会在首次使用时自动下载到用户主目录下的 .onnxocr/models 目录。
  • 您也可以手动下载模型:
# 下载server模型(更准确但更大)
python download_models.py --mode server

# 下载mobile模型(更快但精度略低)
python download_models.py --mode mobile

模型下载地址:

🚀 一键运行

python test_ocr.py  

📡 API 服务(CPU 示例)

启动服务

python app-service.py  

测试示例

请求

curl -X POST http://localhost:5005/ocr \  
-H "Content-Type: application/json" \  
-d '{"image": "base64_encoded_image_data"}'  

响应

{  
  "processing_time": 0.456,  
  "results": [  
    {  
      "text": "名称",  
      "confidence": 0.9999361634254456,  
      "bounding_box": [[4.0, 8.0], [31.0, 8.0], [31.0, 24.0], [4.0, 24.0]]  
    },  
    {  
      "text": "标头",  
      "confidence": 0.9998759031295776,  
      "bounding_box": [[233.0, 7.0], [258.0, 7.0], [258.0, 23.0], [233.0, 23.0]]  
    }  
  ]  
}  

POST 请求

url: ip:5006/ocr  

返回值示例

{  
  "processing_time": 0.456,  
  "results": [  
    {  
      "text": "名称",  
      "confidence": 0.9999361634254456,  
      "bounding_box": [[4.0, 8.0], [31.0, 8.0], [31.0, 24.0], [4.0, 24.0]]  
    },  
    {  
      "text": "标头",  
      "confidence": 0.9998759031295776,  
      "bounding_box": [[233.0, 7.0], [258.0, 7.0], [258.0, 23.0], [233.0, 23.0]]  
    }  
  ]  
}  

🌟 效果展示

示例 1 示例 2
示例 3 示例 4
示例 5 示例 6

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ydocr-onnx-1.0.2.tar.gz (25.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ydocr_onnx-1.0.2-py3-none-any.whl (25.1 MB view details)

Uploaded Python 3

File details

Details for the file ydocr-onnx-1.0.2.tar.gz.

File metadata

  • Download URL: ydocr-onnx-1.0.2.tar.gz
  • Upload date:
  • Size: 25.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for ydocr-onnx-1.0.2.tar.gz
Algorithm Hash digest
SHA256 fcf3c49de6d5c665762ebc7986989c4459fd5975cb2344d69ac82a19f1a41750
MD5 d3200f2eb52c2d4f8771c277d9d1e9ca
BLAKE2b-256 2960e14c92d656c3a7380436957f9d32d9e387733865494530e0d3ac8deeeaaa

See more details on using hashes here.

File details

Details for the file ydocr_onnx-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: ydocr_onnx-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 25.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for ydocr_onnx-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 89da4a6e6ada2a426a2637d1f04fa3bad334e9a5f585ea27a35de38c4ec15b1d
MD5 663579bf4be7e63513dcdbe29f5de339
BLAKE2b-256 105a5aa4990cdb370b7d711e4b2facd7465e0285a9b79c0fe5338d5fd6d2f176

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page