oar_ocr/lib.rs
1//! # OAR OCR
2//!
3//! A Rust OCR library that extracts text from document images using ONNX models.
4//! Supports text detection, recognition, document orientation, and rectification.
5//!
6//! ## Features
7//!
8//! - Complete OCR pipeline from image to text
9//! - High-level builder APIs for easy pipeline configuration
10//! - Model adapter system for easy model swapping
11//! - Batch processing support
12//! - ONNX Runtime integration for fast inference
13//!
14//! ## Components
15//!
16//! - **Text Detection**: Find text regions in images
17//! - **Text Recognition**: Convert text regions to readable text
18//! - **Layout Detection**: Identify document structure elements (text blocks, titles, tables, figures)
19//! - **Document Orientation**: Detect document rotation (0°, 90°, 180°, 270°)
20//! - **Document Rectification**: Fix perspective distortion
21//! - **Text Line Classification**: Detect text line orientation
22//! - **Seal Text Detection**: Detect text in circular seals
23//! - **Formula Recognition**: Recognize mathematical formulas
24//!
25//! ## Modules
26//!
27//! * [`core`] - Core traits, error handling, and batch processing
28//! * [`domain`] - Domain types like orientation helpers and prediction models
29//! * [`models`] - Model adapters for different OCR tasks
30//! * [`oarocr`] - High-level OCR pipeline builders
31//! * [`processors`] - Image processing utilities
32//! * [`utils`] - Utility functions for images and tensors
33//! * [`predictors`] - Task-specific predictor interfaces
34//!
35//! ## Quick Start
36//!
37//! ### OCR Pipeline
38//!
39//! ```rust,no_run
40//! use oar_ocr::oarocr::{OAROCRBuilder, OAROCR};
41//! use oar_ocr::utils::load_image;
42//! use std::path::Path;
43//!
44//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
45//! // Create OCR pipeline with required components
46//! let ocr = OAROCRBuilder::new(
47//! "models/text_detection.onnx",
48//! "models/text_recognition.onnx",
49//! "models/character_dict.txt"
50//! )
51//! .with_document_image_orientation_classification("models/doc_orient.onnx")
52//! .with_text_line_orientation_classification("models/line_orient.onnx")
53//! .image_batch_size(4)
54//! .region_batch_size(32)
55//! .build()?;
56//!
57//! // Process images
58//! let image = load_image(Path::new("document.jpg"))?;
59//! let results = ocr.predict(vec![image])?;
60//!
61//! for result in results {
62//! for region in result.text_regions {
63//! if let Some(text) = region.text {
64//! println!("Text: {}", text);
65//! }
66//! }
67//! }
68//! # Ok(())
69//! # }
70//! ```
71//!
72//! ### Document Structure Analysis
73//!
74//! ```rust,no_run
75//! use oar_ocr::oarocr::{OARStructureBuilder, OARStructure};
76//! use std::path::Path;
77//!
78//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
79//! // Create structure analysis pipeline
80//! let structure = OARStructureBuilder::new("models/layout_detection.onnx")
81//! .with_table_classification("models/table_classification.onnx")
82//! .with_table_cell_detection("models/table_cell_detection.onnx", "wired")
83//! .with_table_structure_recognition("models/table_structure.onnx", "wired")
84//! .table_structure_dict_path("models/table_structure_dict.txt")
85//! .with_formula_recognition(
86//! "models/formula_recognition.onnx",
87//! "models/tokenizer.json",
88//! "pp_formulanet"
89//! )
90//! .build()?;
91//!
92//! // Analyze document structure
93//! let result = structure.predict("document.jpg")?;
94//!
95//! println!("Layout elements: {}", result.layout_elements.len());
96//! println!("Tables: {}", result.tables.len());
97//! println!("Formulas: {}", result.formulas.len());
98//! # Ok(())
99//! # }
100//! ```
101
102// Re-export core modules from oar-ocr-core
103pub mod core {
104 pub use oar_ocr_core::core::*;
105}
106
107/// Auto-download of model files from ModelScope.
108///
109/// Available only when the `auto-download` feature is enabled. See
110/// [`oar_ocr_core::core::download`] for details. When the feature is on,
111/// the high-level OCR builders accept either a filesystem path or a bare
112/// registered file name (e.g. `"pp-ocrv5_mobile_det.onnx"`) for any model
113/// path argument.
114#[cfg(feature = "auto-download")]
115pub mod download {
116 pub use oar_ocr_core::core::download::*;
117}
118
119pub mod domain {
120 pub use oar_ocr_core::domain::*;
121}
122
123pub mod models {
124 pub use oar_ocr_core::models::*;
125}
126
127pub mod processors {
128 pub use oar_ocr_core::processors::*;
129}
130
131pub mod predictors {
132 pub use oar_ocr_core::predictors::*;
133}
134
135// Utils module with re-exports from core and OCR-specific visualization
136pub mod utils;
137
138// High-level OCR API (remains in main crate)
139pub mod oarocr;
140
141// Re-export derive macros for convenient use
142pub use oar_ocr_derive::{ConfigValidator, TaskPredictorBuilder};
143
144/// Prelude module for convenient imports.
145///
146/// Bring the essentials into scope with a single use statement:
147///
148/// ```rust
149/// use oar_ocr::prelude::*;
150/// ```
151///
152/// Included items focus on the most common tasks:
153/// - Builder APIs (`OAROCRBuilder`, `OARStructureBuilder`)
154/// - Edge processors (`EdgeProcessorConfig`)
155/// - Results (`OAROCRResult`, `TextRegion`)
156/// - Essential error and result types (`OCRError`, `OcrResult`)
157/// - Basic image loading (`load_image`, `load_images`)
158///
159/// For advanced customization (model adapters, traits),
160/// import directly from the respective modules (e.g., `oar_ocr::models`, `oar_ocr::core::traits`).
161pub mod prelude {
162 // High-level builder APIs
163 pub use crate::oarocr::{
164 EdgeProcessorConfig, OAROCR, OAROCRBuilder, OAROCRResult, OARStructure,
165 OARStructureBuilder, TextRegion,
166 };
167
168 // Error Handling
169 pub use oar_ocr_core::core::{OCRError, OcrResult};
170
171 // Image Utilities
172 pub use oar_ocr_core::utils::{load_image, load_images};
173
174 // Predictors (high-level API)
175 pub use oar_ocr_core::predictors::*;
176}