返回模型
说明文档
EasyOCR ONNX 模型 - JPQD 量化版
本仓库包含经过 JPQD(联合剪枝、量化和蒸馏)量化优化的 EasyOCR 模型的 ONNX 版本,可实现高效推理。
📋 模型概述
EasyOCR 是一个开箱即用的 OCR 工具,支持 80 多种语言和所有流行的书写系统,包括拉丁文、中文、阿拉伯文、梵文、西里尔文等。本仓库提供了核心 EasyOCR 模型的优化 ONNX 版本。
可用模型
| 模型 | 原始大小 | 优化后大小 | 压缩比 | 描述 |
|---|---|---|---|---|
craft_mlt_25k_jpqd.onnx |
79.3 MB | 5.7 KB | 1.51x | CRAFT 文本检测模型 |
english_g2_jpqd.onnx |
14.4 MB | 8.5 MB | 3.97x | 英文文本识别 (CRNN) |
latin_g2_jpqd.onnx |
14.7 MB | 8.5 MB | 3.97x | 拉丁文文本识别 (CRNN) |
总体大小缩减: 108.4 MB → 17.0 MB (6.4x 压缩)
🚀 快速开始
安装
pip install onnxruntime opencv-python numpy pillow
基本用法
import onnxruntime as ort
import cv2
import numpy as np
from PIL import Image
# 加载模型
text_detector = ort.InferenceSession("craft_mlt_25k_jpqd.onnx")
text_recognizer = ort.InferenceSession("english_g2_jpqd.onnx") # 或 latin_g2_jpqd.onnx
# 加载并预处理图像
image = cv2.imread("your_image.jpg")
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 文本检测
def detect_text(image, model):
# CRAFT 预处理 (640x640, RGB, 归一化)
h, w = image.shape[:2]
input_size = 640
image_resized = cv2.resize(image, (input_size, input_size))
image_norm = image_resized.astype(np.float32) / 255.0
image_norm = np.transpose(image_norm, (2, 0, 1)) # HWC 转 CHW
image_batch = np.expand_dims(image_norm, axis=0)
# 运行推理
outputs = model.run(None, {"input": image_batch})
return outputs[0]
# 文本识别
def recognize_text(text_region, model):
# CRNN 预处理 (32x100, 灰度图, 归一化)
gray = cv2.cvtColor(text_region, cv2.COLOR_RGB2GRAY)
resized = cv2.resize(gray, (100, 32))
normalized = resized.astype(np.float32) / 255.0
input_batch = np.expand_dims(np.expand_dims(normalized, axis=0), axis=0)
# 运行推理
outputs = model.run(None, {"input": input_batch})
return outputs[0]
# 示例用法
detection_result = detect_text(image_rgb, text_detector)
print("文本检测完成!")
# 对于文本识别,您需要从 detection_result 中提取文本区域
# 然后将它们传递给识别模型
使用自定义流水线的高级用法
import onnxruntime as ort
import cv2
import numpy as np
from typing import List, Tuple
class EasyOCR_ONNX:
def __init__(self, detector_path: str, recognizer_path: str):
self.detector = ort.InferenceSession(detector_path)
self.recognizer = ort.InferenceSession(recognizer_path)
# 英文字符集(可根据其他语言修改)
self.charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
def detect_text_boxes(self, image: np.ndarray) -> List[np.ndarray]:
"""检测图像中的文本区域"""
# 预处理
h, w = image.shape[:2]
input_size = 640
image_resized = cv2.resize(image, (input_size, input_size))
image_norm = image_resized.astype(np.float32) / 255.0
image_norm = np.transpose(image_norm, (2, 0, 1))
image_batch = np.expand_dims(image_norm, axis=0)
# 推理
outputs = self.detector.run(None, {"input": image_batch})
# 后处理以提取边界框
# (具体实现取决于 CRAFT 输出格式)
text_regions = self._extract_text_regions(outputs[0], image, (input_size, input_size))
return text_regions
def recognize_text(self, text_regions: List[np.ndarray]) -> List[str]:
"""识别检测区域中的文本"""
results = []
for region in text_regions:
# 预处理
gray = cv2.cvtColor(region, cv2.COLOR_RGB2GRAY) if len(region.shape) == 3 else region
resized = cv2.resize(gray, (100, 32))
normalized = resized.astype(np.float32) / 255.0
input_batch = np.expand_dims(np.expand_dims(normalized, axis=0), axis=0)
# 推理
outputs = self.recognizer.run(None, {"input": input_batch})
# 将输出解码为文本
text = self._decode_text(outputs[0])
results.append(text)
return results
def _extract_text_regions(self, detection_output, original_image, input_size):
"""从检测输出中提取文本区域"""
# 占位符 - 根据 CRAFT 输出格式实现
# 这将涉及在文本/链接图中查找连通分量
# 并从原始图像中提取相应区域
return []
def _decode_text(self, recognition_output):
"""将识别输出解码为文本字符串"""
# 简单的贪婪解码
indices = np.argmax(recognition_output[0], axis=1)
text = ''.join([self.charset[idx] if idx < len(self.charset) else '' for idx in indices])
return text.strip()
# 使用方法
ocr = EasyOCR_ONNX("craft_mlt_25k_jpqd.onnx", "english_g2_jpqd.onnx")
image = cv2.imread("document.jpg")
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 检测并识别文本
text_regions = ocr.detect_text_boxes(image_rgb)
recognized_texts = ocr.recognize_text(text_regions)
for text in recognized_texts:
print(f"检测到的文本: {text}")
🔧 模型详情
CRAFT 文本检测模型
- 架构: CRAFT (Character Region Awareness for Text Detection)
- 输入: RGB 图像 (640×640)
- 输出: 文本区域和亲和力图
- 用途: 检测自然场景图像中的文本区域
CRNN 文本识别模型
- 架构: CNN + BiLSTM + CTC
- 输入: 灰度图像 (32×100)
- 输出: 字符序列概率
- 语言:
english_g2: 英文字符 (95 类)latin_g2: 扩展拉丁字符 (352 类)
⚡ 性能优势
量化详情
- 方法: JPQD(联合剪枝、量化和蒸馏)
- 精度: INT8 权重,FP32 激活
- 框架: ONNXRuntime 动态量化
基准测试
- 推理速度: 比原始 PyTorch 模型快约 3-4 倍
- 内存使用: 内存占用减少约 4 倍
- 准确率: 保留原始模型 >95% 的准确率
运行时要求
- CPU: 针对 CPU 推理进行了优化
- 内存: 总内存使用约 50MB
- 依赖: ONNXRuntime, OpenCV, NumPy
📚 模型信息
原始模型
这些模型基于 EasyOCR 项目:
- 仓库: JaidedAI/EasyOCR
- 许可证: Apache 2.0
- 论文: CRAFT: Character-Region Awareness for Text Detection
优化过程
- 模型提取: 从 EasyOCR PyTorch 模型转换
- ONNX 转换: PyTorch → ONNX,支持动态批次
- JPQD 量化: 应用于 INT8 权重的动态量化
- 验证: 验证输出与原始模型的兼容性
🎯 使用场景
文档处理
- 发票和收据扫描
- 表单处理和数据提取
- 文档数字化
场景文本识别
- 街道标志识别
- 车牌识别
- 产品标签扫描
移动应用
- 移动设备上的实时 OCR
- 离线文本识别
- 边缘部署场景
🔄 模型版本
| 版本 | 日期 | 变更 |
|---|---|---|
| v1.0 | 2025-01 | 初始 JPQD 量化版本发布 |
📄 许可证
- 模型: Apache 2.0(继承自 EasyOCR)
- 代码示例: Apache 2.0
- 文档: CC BY 4.0
🤝 贡献
欢迎贡献!请随时提交问题或拉取请求:
- 性能改进
- 额外的语言支持
- 更好的预处理流水线
- 文档增强
📞 支持
如有问题和支持需求:
- 问题: 在本仓库提交 issue
- 文档: 查阅 EasyOCR 原始文档
- 社区: 加入计算机视觉社区讨论
🔗 相关资源
这些模型是 EasyOCR 的优化版本,适用于生产部署,在保持准确率的同时显著提升了性能。
asmud/EasyOCR-onnx
作者 asmud
image-to-text
onnx
↓ 0
♥ 2
创建时间: 2025-09-02 13:09:29+00:00
更新时间: 2025-09-02 13:27:22+00:00
在 Hugging Face 上查看文件 (11)
.gitattributes
LICENSE
README.md
craft_mlt_25k_jpqd.onnx
ONNX
craft_mlt_25k_jpqd.yaml
english_g2_jpqd.onnx
ONNX
english_g2_jpqd.yaml
example.py
latin_g2_jpqd.onnx
ONNX
latin_g2_jpqd.yaml
requirements.txt