oar-ocr-core 0.6.3

Core types and predictors for oar-ocr
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
//! Document transformation post-processing functionality.

use crate::core::OcrResult;
use crate::core::errors::OCRError;
use std::str::FromStr;

/// Post-processor for document transformation results.
///
/// The `UVDocPostProcess` struct handles the post-processing of document
/// transformation model outputs, converting normalized coordinates back
/// to pixel coordinates and applying various transformations.
#[derive(Debug)]
pub struct UVDocPostProcess {
    /// Scale factor to convert normalized values back to pixel values.
    pub scale: f32,
}

impl UVDocPostProcess {
    /// Creates a new UVDocPostProcess instance.
    ///
    /// # Arguments
    ///
    /// * `scale` - Scale factor for converting normalized coordinates to pixels.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use oar_ocr_core::processors::UVDocPostProcess;
    ///
    /// let postprocessor = UVDocPostProcess::new(1.0);
    /// ```
    pub fn new(scale: f32) -> Self {
        Self { scale }
    }

    /// Gets the current scale factor.
    ///
    /// # Returns
    ///
    /// The scale factor used for coordinate conversion.
    pub fn scale(&self) -> f32 {
        self.scale
    }

    /// Sets a new scale factor.
    ///
    /// # Arguments
    ///
    /// * `scale` - New scale factor.
    pub fn set_scale(&mut self, scale: f32) {
        self.scale = scale;
    }

    /// Converts normalized coordinates to pixel coordinates.
    ///
    /// # Arguments
    ///
    /// * `normalized_coords` - Vector of normalized coordinates (0.0 to 1.0).
    ///
    /// # Returns
    ///
    /// * `Vec<f32>` - Vector of pixel coordinates.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use oar_ocr_core::processors::UVDocPostProcess;
    ///
    /// let postprocessor = UVDocPostProcess::new(100.0);
    /// let normalized = vec![0.1, 0.2, 0.8, 0.9];
    /// let pixels = postprocessor.denormalize_coordinates(&normalized);
    /// assert_eq!(pixels, vec![10.0, 20.0, 80.0, 90.0]);
    /// ```
    pub fn denormalize_coordinates(&self, normalized_coords: &[f32]) -> Vec<f32> {
        normalized_coords
            .iter()
            .map(|&coord| coord * self.scale)
            .collect()
    }

    /// Converts pixel coordinates to normalized coordinates.
    ///
    /// # Arguments
    ///
    /// * `pixel_coords` - Vector of pixel coordinates.
    ///
    /// # Returns
    ///
    /// * `Vec<f32>` - Vector of normalized coordinates (0.0 to 1.0).
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use oar_ocr_core::processors::UVDocPostProcess;
    ///
    /// let postprocessor = UVDocPostProcess::new(100.0);
    /// let pixels = vec![10.0, 20.0, 80.0, 90.0];
    /// let normalized = postprocessor.normalize_coordinates(&pixels);
    /// assert_eq!(normalized, vec![0.1, 0.2, 0.8, 0.9]);
    /// ```
    pub fn normalize_coordinates(&self, pixel_coords: &[f32]) -> Vec<f32> {
        if self.scale == 0.0 {
            return vec![0.0; pixel_coords.len()];
        }
        pixel_coords
            .iter()
            .map(|&coord| coord / self.scale)
            .collect()
    }

    /// Processes a bounding box from normalized to pixel coordinates.
    ///
    /// # Arguments
    ///
    /// * `bbox` - Bounding box as [x1, y1, x2, y2] in normalized coordinates.
    ///
    /// # Returns
    ///
    /// * `[f32; 4]` - Bounding box in pixel coordinates.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use oar_ocr_core::processors::UVDocPostProcess;
    ///
    /// let postprocessor = UVDocPostProcess::new(100.0);
    /// let normalized_bbox = [0.1, 0.2, 0.8, 0.9];
    /// let pixel_bbox = postprocessor.process_bbox(&normalized_bbox);
    /// assert_eq!(pixel_bbox, [10.0, 20.0, 80.0, 90.0]);
    /// ```
    pub fn process_bbox(&self, bbox: &[f32; 4]) -> [f32; 4] {
        [
            bbox[0] * self.scale,
            bbox[1] * self.scale,
            bbox[2] * self.scale,
            bbox[3] * self.scale,
        ]
    }

    /// Processes multiple bounding boxes.
    ///
    /// # Arguments
    ///
    /// * `bboxes` - Vector of bounding boxes in normalized coordinates.
    ///
    /// # Returns
    ///
    /// * `Vec<[f32; 4]>` - Vector of bounding boxes in pixel coordinates.
    pub fn process_bboxes(&self, bboxes: &[[f32; 4]]) -> Vec<[f32; 4]> {
        bboxes.iter().map(|bbox| self.process_bbox(bbox)).collect()
    }

    /// Processes a polygon from normalized to pixel coordinates.
    ///
    /// # Arguments
    ///
    /// * `polygon` - Vector of points as [x, y] pairs in normalized coordinates.
    ///
    /// # Returns
    ///
    /// * `Vec<[f32; 2]>` - Vector of points in pixel coordinates.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use oar_ocr_core::processors::UVDocPostProcess;
    ///
    /// let postprocessor = UVDocPostProcess::new(100.0);
    /// let normalized_polygon = vec![[0.1, 0.2], [0.8, 0.2], [0.8, 0.9], [0.1, 0.9]];
    /// let pixel_polygon = postprocessor.process_polygon(&normalized_polygon);
    /// assert_eq!(pixel_polygon[0], [10.0, 20.0]);
    /// ```
    pub fn process_polygon(&self, polygon: &[[f32; 2]]) -> Vec<[f32; 2]> {
        polygon
            .iter()
            .map(|&[x, y]| [x * self.scale, y * self.scale])
            .collect()
    }

    /// Clamps coordinates to valid ranges.
    ///
    /// # Arguments
    ///
    /// * `coords` - Vector of coordinates to clamp.
    /// * `min_val` - Minimum allowed value.
    /// * `max_val` - Maximum allowed value.
    ///
    /// # Returns
    ///
    /// * `Vec<f32>` - Vector of clamped coordinates.
    pub fn clamp_coordinates(&self, coords: &[f32], min_val: f32, max_val: f32) -> Vec<f32> {
        coords
            .iter()
            .map(|&coord| coord.clamp(min_val, max_val))
            .collect()
    }

    /// Validates that coordinates are within expected ranges.
    ///
    /// # Arguments
    ///
    /// * `coords` - Vector of coordinates to validate.
    /// * `min_val` - Minimum expected value.
    /// * `max_val` - Maximum expected value.
    ///
    /// # Returns
    ///
    /// * `true` - If all coordinates are within range.
    /// * `false` - If any coordinate is out of range.
    pub fn validate_coordinates(&self, coords: &[f32], min_val: f32, max_val: f32) -> bool {
        coords
            .iter()
            .all(|&coord| coord >= min_val && coord <= max_val)
    }

    /// Rounds coordinates to integer values.
    ///
    /// # Arguments
    ///
    /// * `coords` - Vector of coordinates to round.
    ///
    /// # Returns
    ///
    /// * `Vec<i32>` - Vector of rounded integer coordinates.
    pub fn round_coordinates(&self, coords: &[f32]) -> Vec<i32> {
        coords.iter().map(|&coord| coord.round() as i32).collect()
    }

    /// Processes transformation matrix values.
    ///
    /// # Arguments
    ///
    /// * `matrix` - 3x3 transformation matrix as a flat vector.
    ///
    /// # Returns
    ///
    /// * `Vec<f32>` - Processed transformation matrix.
    pub fn process_transformation_matrix(&self, matrix: &[f32; 9]) -> [f32; 9] {
        // Apply scale to translation components (indices 2 and 5)
        let mut processed = *matrix;
        processed[2] *= self.scale; // tx
        processed[5] *= self.scale; // ty
        processed
    }

    /// Applies inverse transformation to coordinates.
    ///
    /// # Arguments
    ///
    /// * `coords` - Vector of coordinates to transform.
    /// * `matrix` - 3x3 transformation matrix.
    ///
    /// # Returns
    ///
    /// * `OcrResult<Vec<[f32; 2]>>` - Transformed coordinates or error.
    pub fn apply_inverse_transform(
        &self,
        coords: &[[f32; 2]],
        matrix: &[f32; 9],
    ) -> OcrResult<Vec<[f32; 2]>> {
        // Calculate determinant for matrix inversion
        let det = matrix[0] * (matrix[4] * matrix[8] - matrix[5] * matrix[7])
            - matrix[1] * (matrix[3] * matrix[8] - matrix[5] * matrix[6])
            + matrix[2] * (matrix[3] * matrix[7] - matrix[4] * matrix[6]);

        if det.abs() < f32::EPSILON {
            return Err(OCRError::InvalidInput {
                message: "Matrix is not invertible (determinant is zero)".to_string(),
            });
        }

        // For simplicity, this is a basic implementation
        // In practice, you might want to use a proper matrix library
        let mut transformed = Vec::new();
        for &[x, y] in coords {
            // Apply inverse transformation (simplified)
            let new_x = (x - matrix[2]) / matrix[0];
            let new_y = (y - matrix[5]) / matrix[4];
            transformed.push([new_x, new_y]);
        }

        Ok(transformed)
    }

    /// Applies batch processing to tensor output to produce rectified images.
    ///
    /// # Arguments
    ///
    /// * `output` - 4D tensor output from the model [batch, channels, height, width].
    ///
    /// # Returns
    ///
    /// * `OcrResult<Vec<image::RgbImage>>` - Vector of rectified images or error.
    pub fn apply_batch(&self, output: &ndarray::Array4<f32>) -> OcrResult<Vec<image::RgbImage>> {
        use image::{Rgb, RgbImage};

        let shape = output.shape();
        if shape.len() != 4 {
            return Err(OCRError::InvalidInput {
                message: "Expected 4D tensor [batch, channels, height, width]".to_string(),
            });
        }

        let batch_size = shape[0];
        let channels = shape[1];
        let height = shape[2];
        let width = shape[3];

        if channels != 3 {
            return Err(OCRError::InvalidInput {
                message: "Expected 3 channels (RGB)".to_string(),
            });
        }

        let mut images = Vec::with_capacity(batch_size);

        let scale = self.scale;

        for b in 0..batch_size {
            let mut img = RgbImage::new(width as u32, height as u32);

            for y in 0..height {
                for x in 0..width {
                    // Model outputs are in BGR order; convert back to RGB.
                    let b_val = (output[[b, 0, y, x]] * scale).clamp(0.0, 255.0) as u8;
                    let g_val = (output[[b, 1, y, x]] * scale).clamp(0.0, 255.0) as u8;
                    let r_val = (output[[b, 2, y, x]] * scale).clamp(0.0, 255.0) as u8;

                    img.put_pixel(x as u32, y as u32, Rgb([r_val, g_val, b_val]));
                }
            }

            images.push(img);
        }

        Ok(images)
    }
}

impl Default for UVDocPostProcess {
    /// Creates a default UVDocPostProcess with scale factor 1.0.
    fn default() -> Self {
        Self::new(1.0)
    }
}

impl FromStr for UVDocPostProcess {
    type Err = std::num::ParseFloatError;

    /// Creates a UVDocPostProcess from a string representation of the scale factor.
    ///
    /// # Arguments
    ///
    /// * `s` - String representation of the scale factor.
    ///
    /// # Returns
    ///
    /// * `Ok(UVDocPostProcess)` - If the string can be parsed as a float.
    /// * `Err(ParseFloatError)` - If the string cannot be parsed.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let scale = s.parse::<f32>()?;
        Ok(Self::new(scale))
    }
}

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

    #[test]
    fn test_denormalize_coordinates() {
        let postprocessor = UVDocPostProcess::new(100.0);
        let normalized = vec![0.1, 0.2, 0.8, 0.9];
        let pixels = postprocessor.denormalize_coordinates(&normalized);
        assert_eq!(pixels, vec![10.0, 20.0, 80.0, 90.0]);
    }

    #[test]
    fn test_normalize_coordinates() {
        let postprocessor = UVDocPostProcess::new(100.0);
        let pixels = vec![10.0, 20.0, 80.0, 90.0];
        let normalized = postprocessor.normalize_coordinates(&pixels);
        assert_eq!(normalized, vec![0.1, 0.2, 0.8, 0.9]);
    }

    #[test]
    fn test_process_bbox() {
        let postprocessor = UVDocPostProcess::new(100.0);
        let normalized_bbox = [0.1, 0.2, 0.8, 0.9];
        let pixel_bbox = postprocessor.process_bbox(&normalized_bbox);
        assert_eq!(pixel_bbox, [10.0, 20.0, 80.0, 90.0]);
    }

    #[test]
    fn test_process_polygon() {
        let postprocessor = UVDocPostProcess::new(100.0);
        let normalized_polygon = vec![[0.1, 0.2], [0.8, 0.2], [0.8, 0.9], [0.1, 0.9]];
        let pixel_polygon = postprocessor.process_polygon(&normalized_polygon);
        assert_eq!(pixel_polygon[0], [10.0, 20.0]);
        assert_eq!(pixel_polygon[1], [80.0, 20.0]);
    }

    #[test]
    fn test_clamp_coordinates() {
        let postprocessor = UVDocPostProcess::new(1.0);
        let coords = vec![-10.0, 50.0, 150.0];
        let clamped = postprocessor.clamp_coordinates(&coords, 0.0, 100.0);
        assert_eq!(clamped, vec![0.0, 50.0, 100.0]);
    }

    #[test]
    fn test_validate_coordinates() {
        let postprocessor = UVDocPostProcess::new(1.0);
        let valid_coords = vec![10.0, 50.0, 90.0];
        let invalid_coords = vec![10.0, 150.0, 90.0];

        assert!(postprocessor.validate_coordinates(&valid_coords, 0.0, 100.0));
        assert!(!postprocessor.validate_coordinates(&invalid_coords, 0.0, 100.0));
    }

    #[test]
    fn test_round_coordinates() {
        let postprocessor = UVDocPostProcess::new(1.0);
        let coords = vec![10.3, 20.7, 30.5];
        let rounded = postprocessor.round_coordinates(&coords);
        assert_eq!(rounded, vec![10, 21, 31]);
    }

    #[test]
    fn test_from_str() -> Result<(), std::num::ParseFloatError> {
        let postprocessor: UVDocPostProcess = "2.5".parse()?;
        assert_eq!(postprocessor.scale(), 2.5);

        assert!("invalid".parse::<UVDocPostProcess>().is_err());
        Ok(())
    }

    #[test]
    fn test_zero_scale_normalize() {
        let postprocessor = UVDocPostProcess::new(0.0);
        let pixels = vec![10.0, 20.0];
        let normalized = postprocessor.normalize_coordinates(&pixels);
        assert_eq!(normalized, vec![0.0, 0.0]);
    }
}