Skip to main content

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