Skip to content

liuzihua699/onnxocr_cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

简体中文

OnnxOCR C++

Based on OnnxOCR

Requirements

  • C++17 or higher
  • OpenCV 4.x
  • ONNX Runtime 1.11.0+

CentOS 7 Environment Setup

Installing ONNX Runtime

# Build from source
git clone --recursive https://github.com/microsoft/onnxruntime.git
cd onnxruntime
# ./build.sh --config Release --build_shared_lib --parallel
./build.sh --config Release --build_shared_lib --parallel --skip_submodule_sync --allow_running_as_root
cd build/Linux/Release
sudo make install

Build

cd cpp
mkdir build && cd build

# Basic build
cmake ..
make -j$(nproc)

# With GPU support
cmake -DUSE_CUDA=ON ..
make -j$(nproc)
sudo make install

Usage

# Prepare model files
mkdir -p models
# Copy det.onnx, rec.onnx, ppocr_keys.txt to models directory

# Run demo
./ocr_demo test.jpg --det_model ../models/det.onnx --rec_model ../models/rec.onnx --dict ../models/ppocr_keys.txt

demo.png

Project Structure

cpp/
├── include/
│   └── onnxocr/
│       ├── onnxocr.hpp        # Main entry point (include this)
│       ├── types.hpp          # Type definitions
│       ├── config.hpp         # Configuration
│       ├── utils.hpp          # Utility functions
│       ├── preprocess.hpp     # Preprocessing
│       ├── db_postprocess.hpp # Detection postprocessing
│       ├── ctc_postprocess.hpp# Recognition postprocessing
│       ├── cls_postprocess.hpp# Classification postprocessing
│       ├── onnx_session.hpp   # ONNX session management
│       ├── detector.hpp       # Text detector
│       ├── recognizer.hpp     # Text recognizer
│       ├── classifier.hpp     # Text classifier
│       └── text_system.hpp    # OCR system
├── third_party/
│   ├── clipper.hpp            # Clipper polygon library
│   └── clipper.cpp
├── src/
│   └── main.cpp               # Example program
└── CMakeLists.txt

API Usage Examples

CMake Integration

  1. With installation
find_package(onnxocr REQUIRED)
target_link_libraries(your_app onnxocr)

Header include:

#include <onnxocr/onnxocr_api.h>
  1. Without installation
find_package(onnxruntime REQUIRED)
set(ONNXOCR_DIR "/path/to/OnnxOCR/cpp")
target_include_directories(your_app PRIVATE
        ${ONNXOCR_DIR}/onnxocr
        ${OpenCV_INCLUDE_DIRS}
)
target_link_libraries(your_app
        ${ONNXOCR_DIR}/build/libonnxocr.so
        ${OpenCV_LIBS}
        onnxruntime
)

Basic Usage

#include "onnxocr/onnxocr.hpp"

// Configuration
onnxocr::Config config;
config.det_model_path = "models/det.onnx";
config.rec_model_path = "models/rec.onnx";
config.rec_char_dict_path = "models/ppocr_keys.txt";
config.use_gpu = false;

// Initialize
onnxocr::OCR ocr(config);

// Recognize
cv::Mat img = cv::imread("test.jpg");
auto results = ocr(img);

// Print results
for (const auto& line : results) {
    std::cout << line.text << " [" << line.score << "]" << std::endl;
}

Detection Only

auto boxes = ocr.detect(img);
for (const auto& box : boxes) {
    // box[0], box[1], box[2], box[3] are the four corner points
}

Recognition Only

std::vector<cv::Mat> crops = {...};  // Cropped text images
auto rec_results = ocr.recognize(crops);

Enable Angle Classification

config.use_angle_cls = true;
config.cls_model_path = "models/cls.onnx";

Model Conversion

Convert PaddleOCR models to ONNX format:

# Using paddle2onnx
paddlex --install paddle2onnx
            
# Convert detection model
paddlex --paddle2onnx --paddle_model_dir ./PP-OCRv5_server_det_infer --onnx_model_dir ./det_onnx --opset_version 11

# Convert recognition model
paddlex --paddle2onnx --paddle_model_dir ./PP-OCRv5_server_rec_infer --onnx_model_dir ./rec_onnx --opset_version 11

Notes

  1. Dictionary file (ppocr_keys.txt): Must match the dictionary used during training
  2. Image format: Input should be BGR format cv::Mat
  3. Memory: Pay attention to memory usage when processing large batches
  4. Performance: GPU version requires ONNX Runtime built with corresponding CUDA version

About

Lightweight C++ OCR inference using ONNX Runtime for PaddleOCR models. Based on OnnxOCR.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors