oar_ocr/core/batch/dynamic/
config.rs

1//! Configuration types for dynamic batching
2
3use serde::{Deserialize, Serialize};
4
5/// Strategy for determining shape compatibility
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7pub enum ShapeCompatibilityStrategy {
8    /// Exact shape matching - images must have identical dimensions
9    Exact,
10    /// Aspect ratio bucketing - images are grouped by aspect ratio ranges
11    AspectRatio {
12        /// Tolerance for aspect ratio matching (e.g., 0.1 means ±10%)
13        tolerance: f32,
14    },
15    /// Maximum dimension bucketing - images are grouped by maximum dimension ranges
16    MaxDimension {
17        /// Size of each dimension bucket
18        bucket_size: u32,
19    },
20    /// Custom bucketing with predefined dimension targets
21    Custom {
22        /// List of target dimensions (height, width)
23        targets: Vec<(u32, u32)>,
24        /// Tolerance for matching to targets
25        tolerance: f32,
26    },
27}
28
29impl Default for ShapeCompatibilityStrategy {
30    fn default() -> Self {
31        Self::AspectRatio { tolerance: 0.1 }
32    }
33}
34
35/// Strategy for padding images to uniform size
36#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
37pub enum PaddingStrategy {
38    /// Zero padding (fill with zeros)
39    Zero,
40    /// Center padding (center image in padded area)
41    Center {
42        /// RGB color to use for padding
43        fill_color: [u8; 3],
44    },
45    /// Edge padding (repeat edge pixels)
46    Edge,
47    /// Smart padding (content-aware padding)
48    Smart,
49}
50
51impl Default for PaddingStrategy {
52    fn default() -> Self {
53        Self::Center {
54            fill_color: [0, 0, 0],
55        }
56    }
57}
58
59/// Configuration for dynamic batching
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct DynamicBatchConfig {
62    /// Maximum batch size for detection
63    pub max_detection_batch_size: usize,
64    /// Maximum batch size for recognition
65    pub max_recognition_batch_size: usize,
66    /// Minimum batch size (smaller batches are processed individually)
67    pub min_batch_size: usize,
68    /// Shape compatibility strategy
69    pub shape_compatibility: ShapeCompatibilityStrategy,
70    /// Padding strategy for uniform batch sizes
71    pub padding_strategy: PaddingStrategy,
72}
73
74impl Default for DynamicBatchConfig {
75    fn default() -> Self {
76        Self {
77            max_detection_batch_size: 8,
78            max_recognition_batch_size: 16,
79            min_batch_size: 2,
80            shape_compatibility: ShapeCompatibilityStrategy::default(),
81            padding_strategy: PaddingStrategy::default(),
82        }
83    }
84}
85
86impl DynamicBatchConfig {
87    /// Creates a new configuration with default values
88    pub fn new() -> Self {
89        Self::default()
90    }
91
92    /// Sets the maximum detection batch size
93    pub fn with_max_detection_batch_size(mut self, size: usize) -> Self {
94        self.max_detection_batch_size = size;
95        self
96    }
97
98    /// Sets the maximum recognition batch size
99    pub fn with_max_recognition_batch_size(mut self, size: usize) -> Self {
100        self.max_recognition_batch_size = size;
101        self
102    }
103
104    /// Sets the minimum batch size
105    pub fn with_min_batch_size(mut self, size: usize) -> Self {
106        self.min_batch_size = size;
107        self
108    }
109
110    /// Sets the shape compatibility strategy
111    pub fn with_shape_compatibility(mut self, strategy: ShapeCompatibilityStrategy) -> Self {
112        self.shape_compatibility = strategy;
113        self
114    }
115
116    /// Sets the padding strategy
117    pub fn with_padding_strategy(mut self, strategy: PaddingStrategy) -> Self {
118        self.padding_strategy = strategy;
119        self
120    }
121}