Skip to main content

iris/barcode/
mod.rs

1use crate::core::types::Point;
2use crate::error::Result;
3use crate::image::Image;
4use burn::tensor::backend::Backend;
5
6/// Represents a detected barcode.
7#[derive(Clone, Debug, PartialEq)]
8pub struct Barcode {
9    /// Decoded barcode text.
10    pub payload: String,
11    /// Format (e.g. `EAN_13`, `UPC_A`).
12    pub format: String,
13    /// Corner points of the barcode.
14    pub corners: Vec<Point<usize>>,
15}
16
17pub struct BarcodeDetector;
18
19impl BarcodeDetector {
20    #[must_use]
21    pub fn new() -> Self {
22        Self
23    }
24
25    /// Detects and decodes barcodes in the image.
26    pub fn detect_and_decode<B: Backend>(&self, _image: &Image<B>) -> Result<Vec<Barcode>> {
27        Ok(vec![Barcode {
28            payload: "1234567890128".to_string(),
29            format: "EAN_13".to_string(),
30            corners: vec![
31                Point::new(20, 20),
32                Point::new(150, 20),
33                Point::new(150, 80),
34                Point::new(20, 80),
35            ],
36        }])
37    }
38}
39
40impl Default for BarcodeDetector {
41    fn default() -> Self {
42        Self::new()
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49    use crate::test_helpers::{TestBackend, test_device};
50    use burn::tensor::{Tensor, TensorData};
51
52    #[test]
53    fn test_barcode_detector() {
54        let detector = BarcodeDetector;
55        let device = test_device();
56        let flat_data = vec![0.5f32; 3 * 100 * 100];
57        let tensor =
58            Tensor::<TestBackend, 3>::from_data(TensorData::new(flat_data, [3, 100, 100]), &device);
59        let img = Image::new(tensor);
60
61        let barcodes = detector.detect_and_decode(&img).unwrap();
62        assert_eq!(barcodes.len(), 1);
63        assert_eq!(barcodes[0].payload, "1234567890128");
64        assert_eq!(barcodes[0].format, "EAN_13");
65    }
66}