OpenOCR is an open-source OCR (Optical Character Recognition) model developed by Fudan University's FVL Lab, based on the RepSVTR architecture. This repository provides the NPU-adapted version for Huawei Ascend hardware, enabling high-performance Chinese text detection and recognition on Ascend NPU devices.
| Model | Backend | Size | Description |
|---|---|---|---|
openocr_det_model.onnx | ONNX | 11.85 MB | Text detection (DBNet + RepSVTR) |
openocr_rec_model.onnx | ONNX | 23.97 MB | Text recognition (SVTRv2-CTC) |
openocr_det_repvit_ch.pth | PyTorch | 12.14 MB | Detection weights (mobile) |
openocr_repsvtr_ch.pth | PyTorch | 62.26 MB | Recognition weights (mobile) |
openocr_svtrv2_ch.pth | PyTorch | 118.20 MB | Recognition weights (server) |
| Component | Version |
|---|---|
| Python | >= 3.8 |
| CANN | 8.5.1 |
| torch_npu | 2.9.0.post1 |
| torch | 2.x (NPU-compatible) |
| onnxruntime | >= 1.20.0 |
| numpy | >= 1.21.0 |
| opencv-python | >= 4.5.0 |
| Ascend NPU | Ascend910_9362 |
The OpenOCR model uses standard PyTorch operations (Conv2d, BatchNorm, GELU, Linear), which are fully supported by torch_npu on Ascend hardware. The adaptation follows an ONNX-first approach:
| Operator Type | Ascend Compatibility | Status |
|---|---|---|
| Conv2d | Fully supported | ✅ |
| BatchNorm2d | Fully supported | ✅ |
| GELU (Erf) | Fully supported | ✅ |
| Linear | Fully supported | ✅ |
| Depthwise Conv | Fully supported | ✅ |
| SE Block | Fully supported | ✅ |
| Element-wise Add | Fully supported | ✅ |
# 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']}")| Metric | Value |
|---|---|
| Avg Total Latency | 1159.94 ms |
| Detection Latency | 460.14 ms |
| Recognition Latency | 562.44 ms |
| Throughput | 0.86 images/s |
| Peak Memory | 16.73 MB |
| Detection Model Size | 11.85 MB |
| Recognition Model Size | 23.97 MB |
| Model | Input Shape | Output Shape |
|---|---|---|
| Detection | [B, 3, H, W] | [B, 1, H, W] |
| Recognition | [B, 3, 48, W] | [B, T, 6625] |
The model achieves high precision on standard Chinese OCR benchmarks:
# 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.jsonThe evaluation generates a JSON file containing:
model_info: Model I/O shapes and sizesperformance: Latency, throughput metricsprecision: Text accuracy vs ground truthmemory: Peak memory usage during inferenceOpenOCR_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)This work is based on OpenOCR which is licensed under the 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}},
}NPU adapted for Huawei Ascend 910 Series (Ascend910_9362) CANN 8.5.1 | torch_npu 2.9.0 | ONNX Runtime 1.26.0