oar-ocr 0.6.3

An Optical Character Recognition (OCR) and Document Layout Analysis library written in Rust.
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! Data processors for task graph edges.
//!
//! This module provides processors that transform data between task nodes in the graph.
//! For example, cropping and perspective transformation between detection and recognition.

use image::{Rgb, RgbImage};
use imageproc::geometric_transformations::{Interpolation, rotate_about_center};
use oar_ocr_core::core::OCRError;
use oar_ocr_core::processors::BoundingBox;
use oar_ocr_core::utils::BBoxCrop;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::sync::Arc;

/// Trait for processors that transform data between task nodes.
pub trait EdgeProcessor: Debug + Send + Sync {
    /// Input type for this processor
    type Input;

    /// Output type for this processor
    type Output;

    /// Process the input data and produce output
    fn process(&self, input: Self::Input) -> Result<Self::Output, OCRError>;

    /// Get the processor name for debugging
    fn name(&self) -> &str;
}

/// Configuration for edge processors in the task graph.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum EdgeProcessorConfig {
    /// Crop text regions from image based on bounding boxes
    TextCropping {
        /// Whether to handle rotated bounding boxes
        #[serde(default = "default_true")]
        handle_rotation: bool,
    },

    /// Apply perspective transformation to correct text orientation
    PerspectiveTransform {
        /// Target width for transformed images
        target_width: Option<u32>,
        /// Target height for transformed images
        target_height: Option<u32>,
    },

    /// Rotate images based on orientation angles
    ImageRotation {
        /// Whether to rotate based on detected angles
        #[serde(default = "default_true")]
        auto_rotate: bool,
    },

    /// Resize images to specific dimensions
    ImageResize {
        /// Target width
        width: u32,
        /// Target height
        height: u32,
        /// Whether to maintain aspect ratio
        #[serde(default)]
        maintain_aspect_ratio: bool,
    },

    /// Chain multiple processors
    Chain {
        /// List of processors to apply in sequence
        processors: Vec<EdgeProcessorConfig>,
    },
}

fn default_true() -> bool {
    true
}

/// Processor that crops text regions from an image based on bounding boxes.
#[derive(Debug)]
pub struct TextCroppingProcessor {
    pub(crate) handle_rotation: bool,
}

impl TextCroppingProcessor {
    pub fn new(handle_rotation: bool) -> Self {
        Self { handle_rotation }
    }

    /// Crop a single bounding box from an image
    fn crop_single(&self, image: &RgbImage, bbox: &BoundingBox) -> Result<RgbImage, OCRError> {
        if self.handle_rotation && bbox.points.len() == 4 {
            // Rotated bounding box (quadrilateral) - use perspective transform
            BBoxCrop::crop_rotated_bounding_box(image, bbox)
        } else {
            // Regular axis-aligned bounding box
            BBoxCrop::crop_bounding_box(image, bbox)
        }
    }
}

impl EdgeProcessor for TextCroppingProcessor {
    type Input = (Arc<RgbImage>, Vec<BoundingBox>);
    type Output = Vec<Option<Arc<RgbImage>>>;

    fn process(&self, input: Self::Input) -> Result<Self::Output, OCRError> {
        let (image, bboxes) = input;

        let cropped_images: Vec<Option<Arc<RgbImage>>> = bboxes
            .iter()
            .map(|bbox| {
                self.crop_single(&image, bbox)
                    .map(|img| Some(Arc::new(img)))
                    .unwrap_or_else(|_e| {
                        // Failed to crop, return None
                        None
                    })
            })
            .collect();

        Ok(cropped_images)
    }

    fn name(&self) -> &str {
        "TextCropping"
    }
}

/// Processor that rotates images based on orientation angles.
#[derive(Debug)]
pub struct ImageRotationProcessor {
    auto_rotate: bool,
}

impl ImageRotationProcessor {
    pub fn new(auto_rotate: bool) -> Self {
        Self { auto_rotate }
    }
}

impl EdgeProcessor for ImageRotationProcessor {
    type Input = (Vec<Option<Arc<RgbImage>>>, Vec<Option<f32>>);
    type Output = Vec<Option<Arc<RgbImage>>>;

    fn process(&self, input: Self::Input) -> Result<Self::Output, OCRError> {
        let (images, angles) = input;

        if !self.auto_rotate {
            return Ok(images);
        }

        let rotated_images: Vec<Option<Arc<RgbImage>>> = images
            .into_iter()
            .zip(angles.iter())
            .map(|(img_opt, angle_opt)| {
                match (img_opt, angle_opt) {
                    (Some(img), Some(angle)) if angle.abs() > 0.1 => {
                        // Rotate image by the detected angle
                        // Convert angle from degrees to radians (imageproc expects radians)
                        let angle_radians = -angle.to_radians(); // Negative for clockwise rotation

                        // Use bilinear interpolation for smooth rotation
                        let rotated = rotate_about_center(
                            &img,
                            angle_radians,
                            Interpolation::Bilinear,
                            Rgb([255u8, 255u8, 255u8]), // White background for padding
                        );

                        Some(Arc::new(rotated))
                    }
                    (img_opt, _) => img_opt,
                }
            })
            .collect();

        Ok(rotated_images)
    }

    fn name(&self) -> &str {
        "ImageRotation"
    }
}

/// Processor that chains multiple processors together.
///
/// All processors in the chain must have the same input and output types,
/// allowing the output of each processor to be fed as input to the next.
#[derive(Debug)]
pub struct ChainProcessor<T> {
    processors: Vec<Box<dyn EdgeProcessor<Input = T, Output = T>>>,
}

impl<T> ChainProcessor<T> {
    /// Creates a new chain processor with the given processors.
    pub fn new(processors: Vec<Box<dyn EdgeProcessor<Input = T, Output = T>>>) -> Self {
        Self { processors }
    }
}

impl<T> EdgeProcessor for ChainProcessor<T>
where
    T: Debug + Send + Sync,
{
    type Input = T;
    type Output = T;

    fn process(&self, input: Self::Input) -> Result<Self::Output, OCRError> {
        if self.processors.is_empty() {
            return Err(OCRError::ConfigError {
                message: "Empty processor chain".to_string(),
            });
        }

        // Apply all processors in sequence, threading the output of each as input to the next
        let mut current = input;

        for processor in &self.processors {
            current = processor.process(current)?;
        }

        Ok(current)
    }

    fn name(&self) -> &str {
        "Chain"
    }
}

/// Type alias for text cropping processor output
type TextCroppingOutput = Box<
    dyn EdgeProcessor<
            Input = (Arc<RgbImage>, Vec<BoundingBox>),
            Output = Vec<Option<Arc<RgbImage>>>,
        >,
>;

/// Type alias for image rotation processor output
type ImageRotationOutput = Box<
    dyn EdgeProcessor<
            Input = (Vec<Option<Arc<RgbImage>>>, Vec<Option<f32>>),
            Output = Vec<Option<Arc<RgbImage>>>,
        >,
>;

/// Factory for creating edge processors from configuration.
pub struct EdgeProcessorFactory;

impl EdgeProcessorFactory {
    /// Create a text cropping processor
    pub fn create_text_cropping(handle_rotation: bool) -> TextCroppingOutput {
        Box::new(TextCroppingProcessor::new(handle_rotation))
    }

    /// Create an image rotation processor
    pub fn create_image_rotation(auto_rotate: bool) -> ImageRotationOutput {
        Box::new(ImageRotationProcessor::new(auto_rotate))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_text_cropping_processor_creation() {
        let processor = TextCroppingProcessor::new(true);
        assert_eq!(processor.name(), "TextCropping");
    }

    #[test]
    fn test_image_rotation_processor_creation() {
        let processor = ImageRotationProcessor::new(true);
        assert_eq!(processor.name(), "ImageRotation");
    }

    #[test]
    fn test_edge_processor_config_serialization() -> Result<(), Box<dyn std::error::Error>> {
        let config = EdgeProcessorConfig::TextCropping {
            handle_rotation: true,
        };

        let json = serde_json::to_string(&config)?;
        assert!(json.contains("TextCropping"));

        let deserialized: EdgeProcessorConfig = serde_json::from_str(&json)?;
        if let EdgeProcessorConfig::TextCropping { handle_rotation } = deserialized {
            assert!(handle_rotation);
        } else {
            panic!("Wrong variant");
        }
        Ok(())
    }

    #[test]
    fn test_image_rotation_processor_rotates_images() -> Result<(), OCRError> {
        let processor = ImageRotationProcessor::new(true);

        // Create a simple test image (10x10 white image)
        let img = Arc::new(RgbImage::from_pixel(10, 10, Rgb([255u8, 255u8, 255u8])));

        // Test with rotation angle
        let images = vec![Some(img.clone())];
        let angles = vec![Some(45.0)]; // 45 degree rotation

        let result = processor.process((images, angles))?;

        // Should have one rotated image
        assert_eq!(result.len(), 1);
        assert!(result[0].is_some());

        // The rotated image should have different dimensions due to rotation
        let Some(rotated) = result[0].as_ref() else {
            panic!("expected rotated image to be Some");
        };
        // After rotation, the image will be larger to accommodate the rotated content
        assert!(rotated.width() >= 10 || rotated.height() >= 10);
        Ok(())
    }

    #[test]
    fn test_image_rotation_processor_skips_small_angles() -> Result<(), OCRError> {
        let processor = ImageRotationProcessor::new(true);

        let img = Arc::new(RgbImage::from_pixel(10, 10, Rgb([255u8, 255u8, 255u8])));
        let images = vec![Some(img.clone())];
        let angles = vec![Some(0.05)]; // Very small angle, should be skipped

        let result = processor.process((images, angles))?;

        // Should return the original image unchanged
        assert_eq!(result.len(), 1);
        assert!(result[0].is_some());
        let Some(output) = result[0].as_ref() else {
            panic!("expected output image to be Some");
        };
        assert_eq!(output.dimensions(), img.dimensions());
        Ok(())
    }

    #[test]
    fn test_image_rotation_processor_disabled() -> Result<(), OCRError> {
        let processor = ImageRotationProcessor::new(false); // auto_rotate disabled

        let img = Arc::new(RgbImage::from_pixel(10, 10, Rgb([255u8, 255u8, 255u8])));
        let images = vec![Some(img.clone())];
        let angles = vec![Some(45.0)];

        let result = processor.process((images, angles))?;

        // Should return the original image unchanged
        assert_eq!(result.len(), 1);
        assert!(result[0].is_some());
        let Some(output) = result[0].as_ref() else {
            panic!("expected output image to be Some");
        };
        assert_eq!(output.dimensions(), img.dimensions());
        Ok(())
    }

    // Test processor that adds a value to an integer
    #[derive(Debug)]
    struct AddProcessor {
        value: i32,
    }

    impl EdgeProcessor for AddProcessor {
        type Input = i32;
        type Output = i32;

        fn process(&self, input: Self::Input) -> Result<Self::Output, OCRError> {
            Ok(input + self.value)
        }

        fn name(&self) -> &str {
            "Add"
        }
    }

    // Test processor that multiplies an integer by a value
    #[derive(Debug)]
    struct MultiplyProcessor {
        value: i32,
    }

    impl EdgeProcessor for MultiplyProcessor {
        type Input = i32;
        type Output = i32;

        fn process(&self, input: Self::Input) -> Result<Self::Output, OCRError> {
            Ok(input * self.value)
        }

        fn name(&self) -> &str {
            "Multiply"
        }
    }

    #[test]
    fn test_chain_processor_single_processor() -> Result<(), OCRError> {
        let processors: Vec<Box<dyn EdgeProcessor<Input = i32, Output = i32>>> =
            vec![Box::new(AddProcessor { value: 5 })];

        let chain = ChainProcessor::new(processors);
        let result = chain.process(10)?;

        // 10 + 5 = 15
        assert_eq!(result, 15);
        Ok(())
    }

    #[test]
    fn test_chain_processor_multiple_processors() -> Result<(), OCRError> {
        let processors: Vec<Box<dyn EdgeProcessor<Input = i32, Output = i32>>> = vec![
            Box::new(AddProcessor { value: 5 }),      // 10 + 5 = 15
            Box::new(MultiplyProcessor { value: 2 }), // 15 * 2 = 30
            Box::new(AddProcessor { value: 10 }),     // 30 + 10 = 40
        ];

        let chain = ChainProcessor::new(processors);
        let result = chain.process(10)?;

        // (10 + 5) * 2 + 10 = 40
        assert_eq!(result, 40);
        Ok(())
    }

    #[test]
    fn test_chain_processor_empty_chain() {
        let processors: Vec<Box<dyn EdgeProcessor<Input = i32, Output = i32>>> = vec![];

        let chain = ChainProcessor::new(processors);
        let result = chain.process(10);

        // Should return an error for empty chain
        assert!(result.is_err());
        if let Err(OCRError::ConfigError { message }) = result {
            assert_eq!(message, "Empty processor chain");
        } else {
            panic!("Expected ConfigError");
        }
    }

    #[test]
    fn test_chain_processor_name() {
        let processors: Vec<Box<dyn EdgeProcessor<Input = i32, Output = i32>>> =
            vec![Box::new(AddProcessor { value: 5 })];

        let chain = ChainProcessor::new(processors);
        assert_eq!(chain.name(), "Chain");
    }

    #[test]
    fn test_chain_processor_order_matters() -> Result<(), OCRError> {
        // Test that processors are applied in order
        let processors1: Vec<Box<dyn EdgeProcessor<Input = i32, Output = i32>>> = vec![
            Box::new(AddProcessor { value: 5 }),      // 10 + 5 = 15
            Box::new(MultiplyProcessor { value: 2 }), // 15 * 2 = 30
        ];

        let processors2: Vec<Box<dyn EdgeProcessor<Input = i32, Output = i32>>> = vec![
            Box::new(MultiplyProcessor { value: 2 }), // 10 * 2 = 20
            Box::new(AddProcessor { value: 5 }),      // 20 + 5 = 25
        ];

        let chain1 = ChainProcessor::new(processors1);
        let chain2 = ChainProcessor::new(processors2);

        let result1 = chain1.process(10)?;
        let result2 = chain2.process(10)?;

        // (10 + 5) * 2 = 30
        assert_eq!(result1, 30);
        // (10 * 2) + 5 = 25
        assert_eq!(result2, 25);
        // Results should be different
        assert_ne!(result1, result2);
        Ok(())
    }
}