ocr 0.1.1

A pure Rust CLI OCR tool for printed text extraction
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Configuration structures for OCR

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Main OCR configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OcrConfig {
    /// Recognition configuration
    pub recognition: RecognitionConfig,
    /// Image processing configuration
    pub image_processing: ImageProcessingConfig,
    /// Layout analysis configuration
    pub layout_analysis: LayoutAnalysisConfig,
    /// Language configuration
    pub language: LanguageConfig,
    /// Performance configuration
    pub performance: PerformanceConfig,
    /// Debug configuration
    pub debug: DebugConfig,
}

impl Default for OcrConfig {
    fn default() -> Self {
        Self {
            recognition: RecognitionConfig::default(),
            image_processing: ImageProcessingConfig::default(),
            layout_analysis: LayoutAnalysisConfig::default(),
            language: LanguageConfig::default(),
            performance: PerformanceConfig::default(),
            debug: DebugConfig::default(),
        }
    }
}

/// Recognition configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecognitionConfig {
    /// Recognition engine
    pub engine: RecognitionEngine,
    /// Language code
    pub language: String,
    /// Confidence threshold
    pub confidence_threshold: f32,
    /// Character whitelist
    pub character_whitelist: Option<Vec<char>>,
    /// Character blacklist
    pub character_blacklist: Option<Vec<char>>,
    /// Enable dictionary correction
    pub enable_dictionary_correction: bool,
    /// Enable language model
    pub enable_language_model: bool,
    /// Enable font attribute detection (bold, italic, monospace)
    pub enable_font_attribute_detection: bool,
    /// Additional parameters
    pub parameters: HashMap<String, String>,
}

impl Default for RecognitionConfig {
    fn default() -> Self {
        Self {
            engine: RecognitionEngine::PatternMatching,
            language: "en".to_string(),
            confidence_threshold: 0.5,
            character_whitelist: None,
            character_blacklist: None,
            enable_dictionary_correction: true,
            enable_language_model: true,
            enable_font_attribute_detection: true,
            parameters: HashMap::new(),
        }
    }
}

/// Recognition engine enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RecognitionEngine {
    /// LSTM-based recognition
    LSTM,
    /// Traditional pattern matching
    PatternMatching,
    /// Hybrid approach
    Hybrid,
}

/// Image processing configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImageProcessingConfig {
    /// Enable image preprocessing
    pub enable_preprocessing: bool,
    /// Enable image enhancement
    pub enable_enhancement: bool,
    /// Enable noise reduction
    pub enable_noise_reduction: bool,
    /// Enable contrast enhancement
    pub enable_contrast_enhancement: bool,
    /// Enable sharpening
    pub enable_sharpening: bool,
    /// Enable deskewing
    pub enable_deskewing: bool,
    /// Enable binarization
    pub enable_binarization: bool,
    /// Binarization threshold
    pub binarization_threshold: f32,
    /// Binarization method
    pub binarization_method: BinarizationMethod,
    /// Additional parameters
    pub parameters: HashMap<String, String>,
}

impl Default for ImageProcessingConfig {
    fn default() -> Self {
        Self {
            enable_preprocessing: true,
            enable_enhancement: true,
            enable_noise_reduction: true,
            enable_contrast_enhancement: true,
            enable_sharpening: false,
            enable_deskewing: true,
            enable_binarization: true,
            binarization_threshold: 0.5,
            binarization_method: BinarizationMethod::Otsu,
            parameters: HashMap::new(),
        }
    }
}

/// Binarization method enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BinarizationMethod {
    /// Otsu's method
    Otsu,
    /// Adaptive thresholding
    Adaptive,
    /// Fixed threshold
    Fixed,
    /// Sauvola's method
    Sauvola,
}

/// Layout analysis configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LayoutAnalysisConfig {
    /// Enable layout analysis
    pub enable_layout_analysis: bool,
    /// Enable text region detection
    pub enable_text_region_detection: bool,
    /// Enable image region detection
    pub enable_image_region_detection: bool,
    /// Enable table detection
    pub enable_table_detection: bool,
    /// Enable reading order detection
    pub enable_reading_order_detection: bool,
    /// Enable orientation detection
    pub enable_orientation_detection: bool,
    /// Enable vertical text detection (CJK)
    pub enable_vertical_text_detection: bool,
    /// Page segmentation mode (Tesseract PSM)
    pub page_seg_mode: PageSegMode,
    /// Minimum text region size
    pub min_text_region_size: (u32, u32),
    /// Maximum text region size
    pub max_text_region_size: (u32, u32),
    /// Additional parameters
    pub parameters: HashMap<String, String>,
}

impl Default for LayoutAnalysisConfig {
    fn default() -> Self {
        Self {
            enable_layout_analysis: true,
            enable_text_region_detection: true,
            enable_image_region_detection: true,
            enable_table_detection: true,
            enable_reading_order_detection: true,
            enable_orientation_detection: true,
            enable_vertical_text_detection: true,
            page_seg_mode: PageSegMode::Auto,
            min_text_region_size: (10, 10),
            max_text_region_size: (10000, 10000),
            parameters: HashMap::new(),
        }
    }
}

/// Page segmentation mode (Tesseract --psm equivalent)
///
/// Controls how the page is segmented into text regions before recognition.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PageSegMode {
    /// PSM 3: Fully automatic page segmentation (default)
    Auto,
    /// PSM 6: Assume a single uniform block of text
    SingleBlock,
    /// PSM 4: Assume a single column of text of variable sizes
    SingleColumn,
    /// PSM 11: Sparse text — find as much text as possible with no ordering
    SparseText,
    /// PSM 12: Sparse text with orientation and script detection
    SparseTextWithOsd,
    /// PSM 13: Raw line mode — treat input as a single text line
    SingleLine,
    /// PSM 7: Treat the image as a single text line (skip segmentation)
    SingleLineRaw,
    /// PSM 8: Treat the image as a single word
    SingleWord,
    /// PSM 10: Treat the image as a single character
    SingleChar,
}

/// Language configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LanguageConfig {
    /// Primary language
    pub primary_language: String,
    /// Secondary languages
    pub secondary_languages: Vec<String>,
    /// Enable language detection
    pub enable_language_detection: bool,
    /// Language detection confidence threshold
    pub language_detection_threshold: f32,
    /// Character set
    pub character_set: CharacterSet,
    /// Text direction
    pub text_direction: TextDirection,
    /// Additional parameters
    pub parameters: HashMap<String, String>,
}

impl Default for LanguageConfig {
    fn default() -> Self {
        Self {
            primary_language: "en".to_string(),
            secondary_languages: Vec::new(),
            enable_language_detection: true,
            language_detection_threshold: 0.7,
            character_set: CharacterSet::Unicode,
            text_direction: TextDirection::LeftToRight,
            parameters: HashMap::new(),
        }
    }
}

/// Character set enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CharacterSet {
    /// ASCII character set
    Ascii,
    /// Latin-1 character set
    Latin1,
    /// Unicode character set
    Unicode,
    /// Custom character set
    Custom,
}

/// Text direction enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TextDirection {
    /// Left to right
    LeftToRight,
    /// Right to left
    RightToLeft,
    /// Top to bottom
    TopToBottom,
    /// Bottom to top
    BottomToTop,
}

/// Performance configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceConfig {
    /// Maximum number of threads
    pub max_threads: usize,
    /// Enable SIMD optimizations
    pub enable_simd: bool,
    /// Enable GPU acceleration
    pub enable_gpu: bool,
    /// Memory limit in MB
    pub memory_limit_mb: usize,
    /// Cache size in MB
    pub cache_size_mb: usize,
    /// Enable parallel processing
    pub enable_parallel_processing: bool,
    /// Compute device: cpu, gpu, auto
    pub device: String,
    /// Additional parameters
    pub parameters: HashMap<String, String>,
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            max_threads: num_cpus::get(),
            enable_simd: true,
            enable_gpu: false,
            memory_limit_mb: 1024,
            cache_size_mb: 256,
            enable_parallel_processing: true,
            device: "auto".to_string(),
            parameters: HashMap::new(),
        }
    }
}

/// Debug configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DebugConfig {
    /// Enable debug logging
    pub enable_debug_logging: bool,
    /// Enable performance profiling
    pub enable_profiling: bool,
    /// Enable intermediate image saving
    pub enable_intermediate_saving: bool,
    /// Debug output directory
    pub debug_output_dir: Option<String>,
    /// Log level
    pub log_level: LogLevel,
    /// Additional parameters
    pub parameters: HashMap<String, String>,
}

impl Default for DebugConfig {
    fn default() -> Self {
        Self {
            enable_debug_logging: false,
            enable_profiling: false,
            enable_intermediate_saving: false,
            debug_output_dir: None,
            log_level: LogLevel::Info,
            parameters: HashMap::new(),
        }
    }
}

/// Log level enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LogLevel {
    /// Error level
    Error,
    /// Warning level
    Warning,
    /// Info level
    Info,
    /// Debug level
    Debug,
    /// Trace level
    Trace,
}

impl OcrConfig {
    /// Create a new OCR configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Load configuration from file
    pub fn from_file<P: AsRef<std::path::Path>>(path: P) -> Result<Self> {
        let content = std::fs::read_to_string(path)?;
        let config: Self = serde_json::from_str(&content)?;
        Ok(config)
    }

    /// Save configuration to file
    pub fn to_file<P: AsRef<std::path::Path>>(&self, path: P) -> Result<()> {
        let content = serde_json::to_string_pretty(self)?;
        std::fs::write(path, content)?;
        Ok(())
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<()> {
        // Validate recognition config
        if self.recognition.confidence_threshold < 0.0
            || self.recognition.confidence_threshold > 1.0
        {
            return Err(anyhow::anyhow!(
                "Confidence threshold must be between 0.0 and 1.0"
            ));
        }

        // Validate image processing config
        if self.image_processing.binarization_threshold < 0.0
            || self.image_processing.binarization_threshold > 1.0
        {
            return Err(anyhow::anyhow!(
                "Binarization threshold must be between 0.0 and 1.0"
            ));
        }

        // Validate layout analysis config
        if self.layout_analysis.min_text_region_size.0
            >= self.layout_analysis.max_text_region_size.0
            || self.layout_analysis.min_text_region_size.1
                >= self.layout_analysis.max_text_region_size.1
        {
            return Err(anyhow::anyhow!(
                "Minimum text region size must be smaller than maximum"
            ));
        }

        // Validate language config
        if self.language.language_detection_threshold < 0.0
            || self.language.language_detection_threshold > 1.0
        {
            return Err(anyhow::anyhow!(
                "Language detection threshold must be between 0.0 and 1.0"
            ));
        }

        // Validate performance config
        if self.performance.max_threads == 0 {
            return Err(anyhow::anyhow!("Maximum threads must be greater than 0"));
        }

        if self.performance.memory_limit_mb == 0 {
            return Err(anyhow::anyhow!("Memory limit must be greater than 0"));
        }

        Ok(())
    }

    /// Get a configuration parameter
    pub fn get_parameter(&self, key: &str) -> Option<&String> {
        // Check recognition parameters
        if let Some(value) = self.recognition.parameters.get(key) {
            return Some(value);
        }

        // Check image processing parameters
        if let Some(value) = self.image_processing.parameters.get(key) {
            return Some(value);
        }

        // Check layout analysis parameters
        if let Some(value) = self.layout_analysis.parameters.get(key) {
            return Some(value);
        }

        // Check language parameters
        if let Some(value) = self.language.parameters.get(key) {
            return Some(value);
        }

        // Check performance parameters
        if let Some(value) = self.performance.parameters.get(key) {
            return Some(value);
        }

        // Check debug parameters
        if let Some(value) = self.debug.parameters.get(key) {
            return Some(value);
        }

        None
    }

    /// Set a configuration parameter
    pub fn set_parameter(&mut self, key: &str, value: String) {
        // Set in recognition parameters by default
        self.recognition.parameters.insert(key.to_string(), value);
    }
}