This repository provides a C++ implementation of PaddleOCR optimized for RISC-V architecture, using the ncnn inference framework for efficient OCR processing on Milk-V platforms.
- OpenCV
- ncnn inference framework
- risc-toolchain
For x86 platform:
# Run OpenCV build script
./tools/build_opencv.shFor RISC-V platform:
# Run RISC-V OpenCV build script
./tools/build_opencv_risc.shAfter compilation, OpenCV will be installed in:
- x86:
lib/opencv/opencv4/ - RISC-V:
lib/opencv/opencv4_riscv/
For x86 platform:
# Enter ncnn directory
cd lib/ncnn
# Run build script
./build.sh
# After completion, lib/cmake/ncnn directory will be generated
# You need to change lib PATHFor RISC-V platform:
# Run RISC-V ncnn build script
./tool/build_ncnn_risc.shAfter compilation, ncnn will be installed in:
- x86:
lib/ncnn/lib/cmake/ncnn/ - RISC-V:
lib/ncnn/ncnn_riscv/lib/cmake/ncnn/
Model files should be placed in the models/ directory, including:
models/
├── det.bin # Detection model binary file
├── det.param # Detection model parameter file
├── cls.bin # Classification model binary file
├── cls.param # Classification model parameter file
├── rec.bin # Recognition model binary file
├── rec.param # Recognition model parameter file
└── keys.txt # Character dictionary file
Recommended Models:
- PP-OCRv3 series models
- PP-OCRv5 series models (mobile/server versions)
# Create build directory
mkdir -p build
cd build
# Configure CMake
cmake ..
# Build
make -j$(nproc)# Use provided build script
./tools/build.shOr manual build:
# Create build directory
mkdir -p build
cd build
# Configure CMake (enable RISC-V cross-compilation)
cmake .. -DBUILD_FOR_RISCV=ON
# Build
make -j$(nproc)# Syntax
./build/main <config_file> <image_path>
# Example
./build/main config.json images/test.jpgEdit config.json to adjust OCR parameters:
{
"save": true,
"det": {
"infer_threads": -1, // Inference thread count (-1 for auto)
"model_path": "../models/det", // Detection model path
"padding": 0, // Padding size
"max_side_len": 768, // Maximum side length
"box_thres": 0.5, // Box threshold
"bitmap_thres": 0.3, // Binarization threshold
"unclip_ratio": 2.0, // Expansion ratio
"fp16": false // Whether to use FP16
},
"cls": {
"infer_threads": 1, // Inference thread count
"reco_threads": -1, // Recognition thread count
"model_path": "../models/cls", // Classification model path
"enable": true, // Whether to enable classification
"most_angle": true, // Whether to use most common angle
"fp16": false // Whether to use FP16
},
"rec": {
"infer_threads": 1, // Inference thread count
"reco_threads": -1, // Recognition thread count
"model_path": "../models/rec", // Recognition model path
"keys_path": "../models/keys.txt", // Dictionary file path
"fp16": false // Whether to use FP16
}
}The program will output recognized text content, including:
- Text region coordinates
- Confidence scores
- Recognized text content
PaddleOCR-ncnn-CPP/
├── src/ # Source code
│ ├── main.cpp # Main program entry
│ ├── ocr_engine.cpp # OCR engine implementation
│ ├── db_net.cpp # DB detection network
│ ├── angle_net.cpp # Angle classification network
│ ├── crnn_net.cpp # CRNN recognition network
│ ├── utils.cpp # Utility functions
│ └── 3rdparty/ # Third-party libraries
├── models/ # Model files directory
├── images/ # Test images
├── lib/ # Dependencies
│ ├── opencv/ # OpenCV library
│ └── ncnn/ # ncnn library
├── tools/ # Build tools
│ ├── build.sh # RISC-V build script
│ ├── build_opencv.sh # OpenCV build script
│ ├── build_opencv_risc.sh # RISC-V OpenCV build script
│ └── build_ncnn_risc.sh # RISC-V ncnn build script
├── config.json # Configuration file
└── CMakeLists.txt # CMake configuration
infer_threads: Inference thread count, -1 means using all available coresreco_threads: Recognition thread count for parallel processing of multiple text regions
- Mobile Models: Smaller and faster, suitable for resource-constrained devices
- Server Models: Larger and more accurate, suitable for high-performance requirements
- Enable
"fp16": trueon supported hardware to improve inference speed
-
OpenCV not found
Solution: Ensure OpenCV is correctly compiled and installed, check path settings in CMakeLists.txt -
ncnn not found
Solution: Ensure ncnn is correctly compiled, check ncnn_DIR path settings -
Model loading failed
Solution: Check model file paths, ensure both .bin and .param files exist -
RISC-V cross-compilation failed
Solution: Ensure RISC-V toolchain is correctly installed, check tools/toolchain/ directory
Modify log level in src/main.cpp:
// Detailed output
plog::init(plog::debug, &console_appender);
// Basic output
plog::init(plog::info, &console_appender);This project follows the license terms in the LICENSE file.
Issues and Pull Requests are welcome to improve this project!