OpenOCR 是由复旦大学 FVL 实验室开发的开源 OCR(光学字符识别)模型,基于 RepSVTR 架构。本仓库提供了适用于华为昇腾硬件的 NPU 适配版本,可在昇腾 NPU 设备上实现高性能的中文文本检测与识别。
| 模型 | 后端 | 大小 | 描述 |
|---|---|---|---|
openocr_det_model.onnx | ONNX | 11.85 MB | 文本检测(DBNet + RepSVTR) |
openocr_rec_model.onnx | ONNX | 23.97 MB | 文本识别(SVTRv2-CTC) |
openocr_det_repvit_ch.pth | PyTorch | 12.14 MB | 检测权重(移动端) |
openocr_repsvtr_ch.pth | PyTorch | 62.26 MB | 识别权重(移动端) |
openocr_svtrv2_ch.pth | PyTorch | 118.20 MB | 识别权重(服务端) |
| 组件 | 版本 |
|---|---|
| Python | >= 3.8 |
| CANN | 8.5.1 |
| torch_npu | 2.9.0.post1 |
| torch | 2.x(兼容 NPU) |
| onnxruntime | >= 1.20.0 |
| numpy | >= 1.21.0 |
| opencv-python | >= 4.5.0 |
| Ascend NPU | Ascend910_9362 |
OpenOCR 模型采用标准 PyTorch 算子(Conv2d、BatchNorm、GELU、Linear),这些算子在昇腾硬件上均得到 torch_npu 的全面支持。适配工作采用 ONNX 优先的方案:
| 算子类型 | 昇腾兼容性 | 状态 |
|---|---|---|
| Conv2d | 完全支持 | ✅ |
| BatchNorm2d | 完全支持 | ✅ |
| GELU (Erf) | 完全支持 | ✅ |
| Linear | 完全支持 | ✅ |
| Depthwise Conv | 完全支持 | ✅ |
| SE Block | 完全支持 | ✅ |
| Element-wise Add | 完全支持 | ✅ |
# 1. Install dependencies
pip install onnxruntime opencv-python numpy Pillow
# 2. Download model from ModelScope
pip install modelscope
modelscope download --model topdktu/OpenOCR
# 3. (Optional) For NPU inference
pip install torch_npu# ONNX Runtime inference
python inference.py --img_path /path/to/images --backend onnx
# NPU inference
python inference.py --img_path /path/to/images --backend npu
# With visualization
python inference.py --img_path /path/to/images --backend onnx --visualize
# Server mode (higher accuracy)
python inference.py --img_path /path/to/images --backend onnx --mode serverfrom inference import OpenOCRE2E
# Initialize engine
engine = OpenOCRE2E(
backend="onnx", # or "npu"
mode="mobile", # or "server"
drop_score=0.5, # confidence threshold
)
# Run inference
results, time_dicts = engine(
img_path="/path/to/image.jpg",
save_dir="output/",
is_visualize=True,
)
# Process results
for res in results[0]:
print(f"[{res['score']:.3f}] {res['transcription']}")| 指标 | 数值 |
|---|---|
| 平均总延迟 | 1159.94 毫秒 |
| 检测延迟 | 460.14 毫秒 |
| 识别延迟 | 562.44 毫秒 |
| 吞吐量 | 0.86 张/秒 |
| 峰值内存 | 16.73 MB |
| 检测模型大小 | 11.85 MB |
| 识别模型大小 | 23.97 MB |
| 模型 | 输入形状 | 输出形状 |
|---|---|---|
| 检测 | [B, 3, H, W] | [B, 1, H, W] |
| 识别 | [B, 3, 48, W] | [B, T, 6625] |
该模型在标准中文OCR基准测试中达到高精度:
# Full evaluation
python eval_benchmark.py \
--test_dir ./test_images \
--backend onnx \
--iterations 10 \
--warmup 3 \
--output evaluation_results.json
# With ground truth comparison
python eval_benchmark.py \
--test_dir ./test_images \
--gt_file ground_truth.json \
--output eval_with_gt.json评估会生成一个 JSON 文件,其中包含:
model_info:模型输入输出的形状和大小performance:延迟、吞吐量指标precision:文本准确率与基准真值的对比memory:推理过程中的峰值内存使用量OpenOCR_NPU/
├── inference.py # Main inference script (ONNX + NPU)
├── eval_benchmark.py # Precision/performance evaluation suite
├── evaluation_results.json # Benchmark results
├── README.md # This documentation
├── ppocr_keys_v1.txt # Character dictionary (6623 entries)
├── configs/ # Model configuration files
│ ├── det/dbnet/repvit_db.yml
│ └── rec/svtrv2/
│ ├── repsvtr_ch.yml
│ └── svtrv2_ch.yml
├── tools/ # Source code (from original repo)
│ ├── infer_det.py
│ ├── infer_rec.py
│ ├── infer_e2e.py
│ ├── engine/config.py
│ └── utils/
├── test_images/ # Test images
└── e2e_results/ # Inference outputpyclipper(pip install pyclipper)本作品基于OpenOCR,其采用Apache License 2.0许可证。
@misc{openocr2024,
title={OpenOCR: An Open-Source Toolkit for General-OCR Research and Applications},
author={FVL Lab, Fudan University},
year={2024},
publisher={GitHub},
howpublished={\url{https://github.com/Topdu/OpenOCR}},
}已针对华为昇腾 910 系列(Ascend910_9362)进行 NPU 适配 CANN 8.5.1 | torch_npu 2.9.0 | ONNX Runtime 1.26.0