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//! .with_formula_recognition(
85//! "models/formula_recognition.onnx",
86//! "models/tokenizer.json",
87//! "pp_formulanet"
88//! )
89//! .build()?;
90//!
91//! // Analyze document structure
92//! let result = structure.predict("document.jpg")?;
93//!
94//! println!("Layout elements: {}", result.layout_elements.len());
95//! println!("Tables: {}", result.tables.len());
96//! println!("Formulas: {}", result.formulas.len());
97//! # Ok(())
98//! # }
99//! ```
100
101// Core modules
102pub mod core;
103pub mod domain;
104pub mod models;
105
106pub mod oarocr;
107pub mod predictors;
108pub mod processors;
109pub mod utils;
110
111/// Prelude module for convenient imports.
112///
113/// Bring the essentials into scope with a single use statement:
114///
115/// ```rust
116/// use oar_ocr::prelude::*;
117/// ```
118///
119/// Included items focus on the most common tasks:
120/// - Builder APIs (`OAROCRBuilder`, `OARStructureBuilder`)
121/// - Edge processors (`EdgeProcessorConfig`)
122/// - Results (`OAROCRResult`, `TextRegion`)
123/// - Essential error and result types (`OCRError`, `OcrResult`)
124/// - Basic image loading (`load_image`, `load_images`)
125///
126/// For advanced customization (model adapters, traits),
127/// import directly from the respective modules (e.g., `oar_ocr::models`, `oar_ocr::core::traits`).
128pub mod prelude {
129 // High-level builder APIs
130 pub use crate::oarocr::{
131 EdgeProcessorConfig, OAROCR, OAROCRBuilder, OAROCRResult, OARStructure,
132 OARStructureBuilder, TextRegion,
133 };
134
135 // Error Handling
136 pub use crate::core::{OCRError, OcrResult};
137
138 // Image Utilities
139 pub use crate::utils::{load_image, load_images};
140
141 // Predictors (high-level API)
142 pub use crate::predictors::*;
143}