ocr 0.1.2

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
//! Data loading and preprocessing for training

use crate::core::geometry::TBox;
use crate::core::image::OcrImage;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
use tokio::fs;

/// Training dataset entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrainingSample {
    /// Image data
    pub image: OcrImage,
    /// Ground truth text
    pub text: String,
    /// Bounding boxes for text regions
    pub bounding_boxes: Vec<TBox>,
    /// Language of the text
    pub language: Option<String>,
    /// Additional metadata
    pub metadata: HashMap<String, String>,
}

/// Dataset configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatasetConfig {
    /// Path to dataset directory
    pub dataset_path: String,
    /// Dataset format (synthetic, real, mixed)
    pub format: DatasetFormat,
    /// Training split ratio (0.0 to 1.0)
    pub train_split: f32,
    /// Validation split ratio (0.0 to 1.0)
    pub val_split: f32,
    /// Test split ratio (0.0 to 1.0)
    pub test_split: f32,
    /// Maximum number of samples to load
    pub max_samples: Option<usize>,
    /// Whether to shuffle the dataset
    pub shuffle: bool,
    /// Random seed for reproducibility
    pub seed: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DatasetFormat {
    /// Synthetic dataset with generated text
    Synthetic,
    /// Real-world dataset with actual images
    Real,
    /// Mixed dataset combining both
    Mixed,
    /// Custom format with specific loader
    Custom(String),
}

impl DatasetFormat {
    pub fn from_string(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "synthetic" => DatasetFormat::Synthetic,
            "real" => DatasetFormat::Real,
            "mixed" => DatasetFormat::Mixed,
            _ => DatasetFormat::Custom(s.to_string()),
        }
    }
}

/// Dataset splits
#[derive(Debug, Clone)]
pub struct DatasetSplits {
    pub train: Vec<TrainingSample>,
    pub validation: Vec<TrainingSample>,
    pub test: Vec<TrainingSample>,
}

/// Data loader for training datasets
pub struct DataLoader {
    config: DatasetConfig,
    splits: Option<DatasetSplits>,
}

impl DataLoader {
    pub fn new(config: DatasetConfig) -> Self {
        Self {
            config,
            splits: None,
        }
    }

    /// Load dataset from disk
    pub async fn load_dataset(&mut self) -> Result<()> {
        tracing::info!("Loading dataset from: {}", self.config.dataset_path);

        match self.config.format {
            DatasetFormat::Synthetic => self.load_synthetic_dataset().await,
            DatasetFormat::Real => self.load_real_dataset().await,
            DatasetFormat::Mixed => self.load_mixed_dataset().await,
            DatasetFormat::Custom(ref loader_name) => {
                let loader_name = loader_name.clone();
                self.load_custom_dataset(&loader_name).await
            }
        }
    }

    /// Get dataset splits
    pub fn get_splits(&self) -> Option<&DatasetSplits> {
        self.splits.as_ref()
    }

    /// Get training samples
    pub fn get_train_samples(&self) -> Option<&[TrainingSample]> {
        self.splits.as_ref().map(|s| s.train.as_slice())
    }

    /// Get validation samples
    pub fn get_val_samples(&self) -> Option<&[TrainingSample]> {
        self.splits.as_ref().map(|s| s.validation.as_slice())
    }

    /// Get test samples
    pub fn get_test_samples(&self) -> Option<&[TrainingSample]> {
        self.splits.as_ref().map(|s| s.test.as_slice())
    }

    async fn load_synthetic_dataset(&mut self) -> Result<()> {
        tracing::info!("Loading synthetic dataset");

        // For now, create dummy data
        // In a real implementation, this would load from synthetic data generators
        let samples = self.generate_synthetic_samples(1000).await?;
        self.splits = Some(self.split_dataset(samples));

        Ok(())
    }

    async fn load_real_dataset(&mut self) -> Result<()> {
        tracing::info!("Loading real dataset");

        let dataset_path = Path::new(&self.config.dataset_path);
        if !dataset_path.exists() {
            return Err(anyhow::anyhow!(
                "Dataset path does not exist: {}",
                self.config.dataset_path
            ));
        }

        // Look for common dataset formats
        if dataset_path.join("annotations.json").exists() {
            self.load_json_dataset(dataset_path).await
        } else if dataset_path.join("labels.txt").exists() {
            self.load_text_dataset(dataset_path).await
        } else {
            Err(anyhow::anyhow!(
                "Unknown dataset format in: {}",
                self.config.dataset_path
            ))
        }
    }

    async fn load_mixed_dataset(&mut self) -> Result<()> {
        tracing::info!("Loading mixed dataset");

        // Load both synthetic and real data
        let mut synthetic_loader = DataLoader::new(DatasetConfig {
            dataset_path: self.config.dataset_path.clone(),
            format: DatasetFormat::Synthetic,
            train_split: 0.5,
            val_split: 0.25,
            test_split: 0.25,
            max_samples: self.config.max_samples.map(|n| n / 2),
            shuffle: self.config.shuffle,
            seed: self.config.seed,
        });

        let mut real_loader = DataLoader::new(DatasetConfig {
            dataset_path: self.config.dataset_path.clone(),
            format: DatasetFormat::Real,
            train_split: 0.5,
            val_split: 0.25,
            test_split: 0.25,
            max_samples: self.config.max_samples.map(|n| n / 2),
            shuffle: self.config.shuffle,
            seed: self.config.seed,
        });

        synthetic_loader.load_synthetic_dataset().await?;
        real_loader.load_real_dataset().await?;

        // Combine datasets
        let synthetic_splits = synthetic_loader.splits.unwrap();
        let real_splits = real_loader.splits.unwrap();

        self.splits = Some(DatasetSplits {
            train: [synthetic_splits.train, real_splits.train].concat(),
            validation: [synthetic_splits.validation, real_splits.validation].concat(),
            test: [synthetic_splits.test, real_splits.test].concat(),
        });

        Ok(())
    }

    async fn load_custom_dataset(&mut self, _loader_name: &str) -> Result<()> {
        tracing::info!("Loading custom dataset");

        // Placeholder for custom dataset loaders
        Err(anyhow::anyhow!(
            "Custom dataset loaders not implemented yet"
        ))
    }

    async fn load_json_dataset(&self, dataset_path: &Path) -> Result<()> {
        let annotations_path = dataset_path.join("annotations.json");
        let annotations_data = fs::read_to_string(annotations_path).await?;
        let annotations: serde_json::Value = serde_json::from_str(&annotations_data)?;

        // Parse JSON annotations and load images
        // This is a placeholder implementation
        tracing::info!(
            "Loaded JSON annotations with {} entries",
            annotations.as_object().unwrap().len()
        );

        Ok(())
    }

    async fn load_text_dataset(&self, dataset_path: &Path) -> Result<()> {
        let labels_path = dataset_path.join("labels.txt");
        let labels_data = fs::read_to_string(labels_path).await?;

        // Parse text labels and load images
        // This is a placeholder implementation
        let lines: Vec<&str> = labels_data.lines().collect();
        tracing::info!("Loaded text labels with {} entries", lines.len());

        Ok(())
    }

    async fn generate_synthetic_samples(&self, count: usize) -> Result<Vec<TrainingSample>> {
        let mut samples = Vec::with_capacity(count);

        for i in 0..count {
            // Generate synthetic image and text
            let image = self.generate_synthetic_image(i).await?;
            let text = self.generate_synthetic_text(i).await?;
            let bounding_boxes = self.generate_synthetic_boxes(&image, &text).await?;

            samples.push(TrainingSample {
                image,
                text,
                bounding_boxes,
                language: Some("en".to_string()),
                metadata: HashMap::new(),
            });
        }

        Ok(samples)
    }

    async fn generate_synthetic_image(&self, _index: usize) -> Result<OcrImage> {
        // Generate a simple synthetic image
        // In a real implementation, this would use text rendering libraries
        use image::{ImageBuffer, Rgb, RgbImage};

        let width = 200;
        let height = 50;
        let mut img: RgbImage = ImageBuffer::new(width, height);

        // Fill with white background
        for pixel in img.pixels_mut() {
            *pixel = Rgb([255, 255, 255]);
        }

        // Add some random noise/text
        for y in 10..40 {
            for x in 10..190 {
                if (x + y) % 3 == 0 {
                    img.put_pixel(x, y, Rgb([0, 0, 0]));
                }
            }
        }

        Ok(OcrImage::new(img.into(), 300))
    }

    async fn generate_synthetic_text(&self, index: usize) -> Result<String> {
        // Generate synthetic text
        let texts = vec![
            "Hello World",
            "Sample Text",
            "OCR Training",
            "Neural Network",
            "Machine Learning",
            "Computer Vision",
            "Text Recognition",
            "Image Processing",
        ];

        Ok(texts[index % texts.len()].to_string())
    }

    async fn generate_synthetic_boxes(&self, _image: &OcrImage, _text: &str) -> Result<Vec<TBox>> {
        // Generate bounding boxes for text regions
        // This is a placeholder implementation
        Ok(vec![TBox::new(10, 10, 190, 40)])
    }

    fn split_dataset(&self, mut samples: Vec<TrainingSample>) -> DatasetSplits {
        if self.config.shuffle {
            use rand::seq::SliceRandom;
            use rand::SeedableRng;
            let mut rng = if let Some(seed) = self.config.seed {
                rand::rngs::StdRng::seed_from_u64(seed)
            } else {
                rand::rngs::StdRng::from_entropy()
            };
            samples.shuffle(&mut rng);
        }

        let total = samples.len();
        let train_end = (total as f32 * self.config.train_split) as usize;
        let val_end = train_end + (total as f32 * self.config.val_split) as usize;

        DatasetSplits {
            train: samples[..train_end].to_vec(),
            validation: samples[train_end..val_end].to_vec(),
            test: samples[val_end..].to_vec(),
        }
    }
}

/// Batch of training samples
#[derive(Debug, Clone)]
pub struct TrainingBatch {
    pub images: Vec<OcrImage>,
    pub texts: Vec<String>,
    pub bounding_boxes: Vec<Vec<TBox>>,
    pub languages: Vec<Option<String>>,
    pub metadata: Vec<HashMap<String, String>>,
}

impl TrainingBatch {
    pub fn new(batch_size: usize) -> Self {
        Self {
            images: Vec::with_capacity(batch_size),
            texts: Vec::with_capacity(batch_size),
            bounding_boxes: Vec::with_capacity(batch_size),
            languages: Vec::with_capacity(batch_size),
            metadata: Vec::with_capacity(batch_size),
        }
    }

    pub fn add_sample(&mut self, sample: TrainingSample) {
        self.images.push(sample.image);
        self.texts.push(sample.text);
        self.bounding_boxes.push(sample.bounding_boxes);
        self.languages.push(sample.language);
        self.metadata.push(sample.metadata);
    }

    pub fn len(&self) -> usize {
        self.images.len()
    }

    pub fn is_empty(&self) -> bool {
        self.images.is_empty()
    }
}

/// Iterator for batching training samples
pub struct BatchIterator {
    samples: Vec<TrainingSample>,
    batch_size: usize,
    current_index: usize,
}

impl BatchIterator {
    pub fn new(samples: Vec<TrainingSample>, batch_size: usize) -> Self {
        Self {
            samples,
            batch_size,
            current_index: 0,
        }
    }
}

impl Iterator for BatchIterator {
    type Item = TrainingBatch;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current_index >= self.samples.len() {
            return None;
        }

        let end_index = (self.current_index + self.batch_size).min(self.samples.len());
        let mut batch = TrainingBatch::new(self.batch_size);

        for sample in &self.samples[self.current_index..end_index] {
            batch.add_sample(sample.clone());
        }

        self.current_index = end_index;
        Some(batch)
    }
}