Expand description
§Rust PaddleOCR
A high-performance OCR library based on PaddleOCR models, using the MNN inference framework.
§Version 2.0 New Features
- New API Design: Complete layered API from low-level models to high-level pipeline
- Flexible Model Loading: Support loading models from file paths or memory bytes
- Configurable Detection Parameters: Support custom detection thresholds, resolution, etc.
- GPU Acceleration: Support multiple GPU backends including Metal, OpenCL, Vulkan
- Batch Processing: Support batch text recognition for improved throughput
§Quick Start
§Simple Usage - Using High-Level API (Recommended)
ⓘ
use ocr_rs::{OcrEngine, OcrEngineConfig};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create OCR engine
let engine = OcrEngine::new(
"models/det_model.mnn",
"models/rec_model.mnn",
"models/ppocr_keys.txt",
None, // Use default config
)?;
// Open and recognize image
let image = image::open("test.jpg")?;
let results = engine.recognize(&image)?;
for result in results {
println!("Text: {}, Confidence: {:.2}%", result.text, result.confidence * 100.0);
println!("Position: ({}, {})", result.bbox.rect.left(), result.bbox.rect.top());
}
Ok(())
}§Advanced Usage - Using Low-Level API
ⓘ
use ocr_rs::{DetModel, RecModel, DetOptions, DetPrecisionMode};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create detection model
let det = DetModel::from_file("models/det_model.mnn", None)?
.with_options(DetOptions::fast());
// Create recognition model
let rec = RecModel::from_file("models/rec_model.mnn", "models/ppocr_keys.txt", None)?;
// Load image
let image = image::open("test.jpg")?;
// Detect and crop text regions
let detections = det.detect_and_crop(&image)?;
// Recognize each text region
for (cropped_img, bbox) in detections {
let result = rec.recognize(&cropped_img)?;
println!("Position: ({}, {}), Text: {}",
bbox.rect.left(), bbox.rect.top(), result.text);
}
Ok(())
}§GPU Acceleration
ⓘ
use ocr_rs::{OcrEngine, OcrEngineConfig, Backend};
let config = OcrEngineConfig::new()
.with_backend(Backend::Metal); // macOS/iOS
// .with_backend(Backend::OpenCL); // Cross-platform
let engine = OcrEngine::new(det_path, rec_path, charset_path, Some(config))?;§Module Structure
mnn: MNN inference engine wrapper, provides low-level inference capabilitiesdet: Text detection module (DetModel), detects text regions in imagesrec: Text recognition module (RecModel), recognizes text contentengine: High-level OCR pipeline (OcrEngine), all-in-one OCR solutionpreprocess: Image preprocessing utilities, including normalization, scaling, etc.postprocess: Post-processing utilities, including NMS, box merging, sorting, etc.error: Error typesOcrError
§API Hierarchy
┌─────────────────────────────────────────┐
│ OcrEngine (High-Level API) │
│ Complete detection and recognition │
├─────────────────────────────────────────┤
│ DetModel │ RecModel │
│ Detection Model │ Recognition Model │
├─────────────────────────────────────────┤
│ InferenceEngine (MNN) │
│ Low-level inference engine │
└─────────────────────────────────────────┘§Supported Models
- PP-OCRv4: Stable version, good compatibility
- PP-OCRv5: Recommended version, supports multiple languages, higher accuracy
- PP-OCRv5 FP16: Efficient version, faster inference, lower memory usage
Re-exports§
pub use det::DetModel;pub use det::DetOptions;pub use det::DetPrecisionMode;pub use engine::ocr_file;pub use engine::DetOnlyEngine;pub use engine::OcrEngine;pub use engine::OcrEngineConfig;pub use engine::OcrResult_;pub use engine::RecOnlyEngine;pub use error::OcrError;pub use error::OcrResult;pub use mnn::Backend;pub use mnn::InferenceConfig;pub use mnn::InferenceEngine;pub use mnn::PrecisionMode;pub use postprocess::TextBox;pub use rec::RecModel;pub use rec::RecOptions;pub use rec::RecognitionResult;
Modules§
- det
- Text Detection Model
- engine
- OCR Engine
- error
- OCR error type definitions
- mnn
- MNN Inference Engine FFI Binding Layer
- postprocess
- Postprocessing Utilities
- preprocess
- Image Preprocessing Utilities
- rec
- Text Recognition Model
Functions§
- mnn_
version - Get MNN version
- version
- Get library version