基于昇腾 Ascend NPU 的 PP-OCRv4/v5 ONNX 推理引擎,支持文本检测、识别与方向分类的端到端 OCR 推理。
| 属性 | 值 |
|---|---|
| 模型名称 | OnnxOCR (PP-OCRv4 / PP-OCRv5) |
| 任务类型 | 文字检测与识别 (OCR) |
| 框架 | ONNX → PyTorch (onnx2torch) on Ascend NPU |
| 原始模型 | PaddleOCR / OnnxOCR |
| 模型来源 | supersong/onnxocr_model (ModelScope) |
| 硬件平台 | Huawei Ascend 910 |
| CANN 版本 | 8.5.1 |
| 适配方式 | ONNX 标准化 + onnx2torch 转换 + torch_npu 部署 |
OnnxOCR 是基于 ONNX Runtime 的高性能多语言文字检测与识别项目。本项目将其 ONNX 模型适配到昇腾 Ascend NPU 上,通过以下技术路线实现:
| 模型 | 用途 | 输入形状 | 输出 |
|---|---|---|---|
det.onnx | 文字检测 (PicoDet/DB) | [N, 3, H, W] | [N, 1, H, W] 概率图 |
cls.onnx | 文字方向分类 (180°) | [N, 3, 48, 192] | [N, 2] 方向概率 |
rec.onnx | 文字识别 (CRNN/SVTR) | [N, 3, 48, W] | [N, T, V] CTC 分布 |
# 安装依赖
pip install torch_npu==2.9.0
pip install onnx onnxruntime onnx2torch
pip install opencv-python numpy shapely pyclipper
pip install modelscope # 下载模型# 下载模型
modelscope download --model supersong/onnxocr_modelfrom inference import NPUOCR
ocr = NPUOCR(version="ppocrv5", use_npu=True)
results = ocr.ocr("test.jpg")
for r in results:
print(f"[{r['confidence']:.3f}] {r['text']}")# 单张图片推理
python inference.py --image test.jpg --output result_img
# 性能基准测试
python inference.py --benchmark
# 精度验证 (NPU vs CPU)
python inference.py --accuracy
# CPU 模式 (对比测试)
python inference.py --image test.jpg --cpu测试环境: Ascend 910 @ CANN 8.5.1, torch_npu 2.9.0
| 模型 | NPU (ms) | CPU (ms) | 加速比 | NPU-CPU 误差 |
|---|---|---|---|---|
| det (PP-OCRv5) | 41.13 | 337.06 | 8.20x | < 0.0001% |
| cls | 10.81 | 8.54 | ~1.00x* | 0.0529% |
| rec (PP-OCRv5) | 27.10 | 61.50 | 2.27x | 0.3231% |
*注: cls 模型极小 (582KB),NPU 调度开销相对较大,性能与 CPU 持平。 实际端到端 OCR 中 cls 耗时占比 < 10%,整体加速显著。
| 测试图片 | 分辨率 | 检测文本数 | NPU 端到端 (ms) |
|---|---|---|---|
| 00006737.jpg | 896×528 | 31 | 1,331 |
| 00018069.jpg | 1,438×879 | 69 | 2,083 |
| 00059985.jpg | 800×566 | 22 | 677 |
| korean_1.jpg | 610×46 | 1 | 217 |
NPU vs CPU 推理输出最大差异:
| 模型 | 最大误差 | 阈值 | 结果 |
|---|---|---|---|
| det | 0.0000% | 1% | ✅ PASS |
| cls | 0.0529% | 1% | ✅ PASS |
| rec | 0.3231% | 1% | ✅ PASS |
结论: 所有模型 NPU 推理精度与 CPU 一致,误差 < 1%,满足精度要求。
原始 OnnxOCR 使用 onnxruntime.InferenceSession (CPU/CUDA)。本项目实现了 NPU 推理引擎 NPUInferenceEngine:
class NPUInferenceEngine:
"""将 ONNX 模型转换为 PyTorch 并在 NPU 上执行。"""
def _initialize(self):
# Step 1: ONNX 标准化 (Constants → Initializers)
std_path = standardize_onnx(model_path)
# Step 2: ONNX → PyTorch 转换
self.torch_model = onnx2torch.convert(std_path)
# Step 3: 部署到 NPU
self.torch_model = self.torch_model.npu()为确保精度一致,预处理完全对齐原始 OnnxOCR/PaddleOCR:
当 onnx2torch 转换失败时 (如 ppocrv4_rec 的 BatchNorm 不支持), 自动回退到 onnxruntime CPU:
[FALLBACK] onnx2torch conversion failed → onnxruntime CPU inferenceonnxocr_npu/
├── inference.py # 主推理脚本 & OCR Pipeline
├── npu_engine.py # NPU 推理引擎 (ONNX→Torch→NPU)
├── onnx_standardize.py # ONNX 模型标准化工具
├── accuracy_log.txt # 精度验证日志
├── benchmark_log.txt # 性能基准日志
├── result_img/ # 推理结果与可视化
├── README.md # 本文档
└── test_images/ # 测试图片#NPU #Ascend910 #OCR #PP-OCR #ONNX #torch_npu #TextDetection #TextRecognition #PaddleOCR #CANN #昇腾 #ModelScope
本项目基于 OnnxOCR 原始项目适配,遵循原始项目许可证。