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
107pub mod domain {
108 pub use oar_ocr_core::domain::*;
109}
110
111pub mod models {
112 pub use oar_ocr_core::models::*;
113}
114
115pub mod processors {
116 pub use oar_ocr_core::processors::*;
117}
118
119pub mod predictors {
120 pub use oar_ocr_core::predictors::*;
121}
122
123// Utils module with re-exports from core and OCR-specific visualization
124pub mod utils;
125
126// High-level OCR API (remains in main crate)
127pub mod oarocr;
128
129// Re-export derive macros for convenient use
130pub use oar_ocr_derive::{ConfigValidator, TaskPredictorBuilder};
131
132/// Prelude module for convenient imports.
133///
134/// Bring the essentials into scope with a single use statement:
135///
136/// ```rust
137/// use oar_ocr::prelude::*;
138/// ```
139///
140/// Included items focus on the most common tasks:
141/// - Builder APIs (`OAROCRBuilder`, `OARStructureBuilder`)
142/// - Edge processors (`EdgeProcessorConfig`)
143/// - Results (`OAROCRResult`, `TextRegion`)
144/// - Essential error and result types (`OCRError`, `OcrResult`)
145/// - Basic image loading (`load_image`, `load_images`)
146///
147/// For advanced customization (model adapters, traits),
148/// import directly from the respective modules (e.g., `oar_ocr::models`, `oar_ocr::core::traits`).
149pub mod prelude {
150 // High-level builder APIs
151 pub use crate::oarocr::{
152 EdgeProcessorConfig, OAROCR, OAROCRBuilder, OAROCRResult, OARStructure,
153 OARStructureBuilder, TextRegion,
154 };
155
156 // Error Handling
157 pub use oar_ocr_core::core::{OCRError, OcrResult};
158
159 // Image Utilities
160 pub use oar_ocr_core::utils::{load_image, load_images};
161
162 // Predictors (high-level API)
163 pub use oar_ocr_core::predictors::*;
164}