oar_ocr/core/inference/
image_reader.rs

1//! Default implementation of the `ImageReader` trait used by inference builders.
2
3use crate::core::{errors::OCRError, traits::ImageReader};
4use image::RgbImage;
5use std::path::Path;
6
7/// Reads RGB images from disk with optional parallel batching.
8#[derive(Debug)]
9pub struct DefaultImageReader {
10    /// Optional threshold that switches the loader into parallel mode.
11    parallel_threshold: Option<usize>,
12}
13
14impl DefaultImageReader {
15    /// Creates a reader with no parallel threshold.
16    pub fn new() -> Self {
17        Self {
18            parallel_threshold: None,
19        }
20    }
21
22    /// Creates a reader that switches to parallel loading after the given threshold.
23    pub fn with_parallel_threshold(parallel_threshold: usize) -> Self {
24        Self {
25            parallel_threshold: Some(parallel_threshold),
26        }
27    }
28}
29
30impl Default for DefaultImageReader {
31    fn default() -> Self {
32        Self::new()
33    }
34}
35
36impl ImageReader for DefaultImageReader {
37    type Error = OCRError;
38
39    fn apply<P: AsRef<Path> + Send + Sync>(
40        &self,
41        imgs: impl IntoIterator<Item = P>,
42    ) -> Result<Vec<RgbImage>, Self::Error> {
43        use crate::utils::load_images_batch_with_threshold;
44
45        let img_paths: Vec<_> = imgs.into_iter().collect();
46        load_images_batch_with_threshold(&img_paths, self.parallel_threshold)
47    }
48}