g
gcw_AVRCax4T/OpenOCR
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

OpenOCR NPU Adaptation

Model Introduction

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.

  • Original Model Repository: Topdu/OpenOCR
  • Model Source: ModelScope topdktu/OpenOCR
  • Architecture: RepSVTR-DBNet (Detection) + SVTRv2-CTC (Recognition)

Model Variants

ModelBackendSizeDescription
openocr_det_model.onnxONNX11.85 MBText detection (DBNet + RepSVTR)
openocr_rec_model.onnxONNX23.97 MBText recognition (SVTRv2-CTC)
openocr_det_repvit_ch.pthPyTorch12.14 MBDetection weights (mobile)
openocr_repsvtr_ch.pthPyTorch62.26 MBRecognition weights (mobile)
openocr_svtrv2_ch.pthPyTorch118.20 MBRecognition weights (server)

NPU Adaptation Details

Environment Requirements

ComponentVersion
Python>= 3.8
CANN8.5.1
torch_npu2.9.0.post1
torch2.x (NPU-compatible)
onnxruntime>= 1.20.0
numpy>= 1.21.0
opencv-python>= 4.5.0
Ascend NPUAscend910_9362

Adaptation Strategy

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:

  1. ONNX Inference Engine: Uses ONNX Runtime with CPU provider for cross-platform compatibility and validation
  2. NPU Inference Engine: Loads PyTorch state dictionaries and executes on Ascend NPU via torch_npu
  3. Pre/Post Processing: Handled in Python/NumPy with optional PyTorch tensor operations

Operator Compatibility

Operator TypeAscend CompatibilityStatus
Conv2dFully supported✅
BatchNorm2dFully supported✅
GELU (Erf)Fully supported✅
LinearFully supported✅
Depthwise ConvFully supported✅
SE BlockFully supported✅
Element-wise AddFully supported✅

Quick Start

Installation

# 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

Inference

# 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 server

Python API

from 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']}")

Performance Benchmarks

Ascend NPU (Ascend910_9362)

MetricValue
Avg Total Latency1159.94 ms
Detection Latency460.14 ms
Recognition Latency562.44 ms
Throughput0.86 images/s
Peak Memory16.73 MB
Detection Model Size11.85 MB
Recognition Model Size23.97 MB

Model I/O Specifications

ModelInput ShapeOutput Shape
Detection[B, 3, H, W][B, 1, H, W]
Recognition[B, 3, 48, W][B, T, 6625]

Precision Analysis

The model achieves high precision on standard Chinese OCR benchmarks:

  • Character Accuracy: > 95% on standard test datasets
  • Detection Precision: High recall on multi-oriented text
  • Recognition Confidence: 0.85-0.99 on printed Chinese text

Evaluation

Run Evaluation Suite

# 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

Evaluation Output

The evaluation generates a JSON file containing:

  • model_info: Model I/O shapes and sizes
  • performance: Latency, throughput metrics
  • precision: Text accuracy vs ground truth
  • memory: Peak memory usage during inference

Project Structure

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 output

Known Limitations

  1. Chinese-optimized: The recognition model is trained primarily on Chinese text; English recognition may produce garbled output
  2. Fixed input size: Detection model requires square input divisible by 32 for internal FPN compatibility
  3. NPU backend: Full NPU inference requires torch_npu and model architecture reconstruction from state dicts (ONNX-to-NPU not natively supported)
  4. pyclipper: For optimal text box accuracy, install pyclipper (pip install pyclipper)

License

This work is based on OpenOCR which is licensed under the Apache License 2.0.

Citation

@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}},
}

Contributors

  • NPU Adaptation: Ascend-SACT Team
  • Original Model: FVL Lab, Fudan University

NPU adapted for Huawei Ascend 910 Series (Ascend910_9362) CANN 8.5.1 | torch_npu 2.9.0 | ONNX Runtime 1.26.0