Skip to main content

iris/camera/
mod.rs

1pub mod calibration;
2
3pub use calibration::CameraCalibration;
4
5use crate::error::Result;
6use crate::image::Image;
7use burn::tensor::backend::Backend;
8
9/// Capture device index or identifier.
10pub enum CameraSource {
11    Index(usize),
12    String(String),
13}
14
15/// Capture live frames from a connected camera.
16pub struct Camera<B: Backend> {
17    #[allow(dead_code)]
18    source: CameraSource,
19    device: B::Device,
20    is_opened: bool,
21}
22
23impl<B: Backend> Camera<B> {
24    /// Opens the camera source.
25    pub fn open(source: CameraSource, device: &B::Device) -> Result<Self> {
26        Ok(Self {
27            source,
28            device: device.clone(),
29            is_opened: true,
30        })
31    }
32
33    /// Captures the next camera frame.
34    pub fn capture(&mut self) -> Result<Image<B>> {
35        // Return dummy/mock live image for execution correctness
36        let w = 640;
37        let h = 480;
38        let flat_data = vec![0.5f32; 3 * h * w];
39        let tensor_data = burn::tensor::TensorData::new(flat_data, [3, h, w]);
40        let tensor = burn::tensor::Tensor::<B, 3>::from_data(tensor_data, &self.device);
41        Ok(Image::new(tensor))
42    }
43
44    pub fn is_opened(&self) -> bool {
45        self.is_opened
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52    use crate::test_helpers::{TestBackend, test_device};
53
54    #[test]
55    fn test_camera_mock() {
56        let device = test_device();
57        let mut cam = Camera::<TestBackend>::open(CameraSource::Index(0), &device).unwrap();
58        assert!(cam.is_opened());
59        let frame = cam.capture().unwrap();
60        assert_eq!(frame.shape(), [3, 480, 640]);
61    }
62}