|  | 
|  | 1 | +//! Offers the ability to receive camera input. | 
|  | 2 | +
 | 
|  | 3 | +use anyhow::bail; | 
|  | 4 | +use opencv::{ | 
|  | 5 | +    core::{Mat, MatTraitConst, MatTraitConstManual, Size_}, | 
|  | 6 | +    videoio::{VideoCapture, VideoCaptureTrait, VideoCaptureTraitConst, CAP_ANY}, | 
|  | 7 | +}; | 
|  | 8 | + | 
|  | 9 | +//Reinport important functions | 
|  | 10 | +pub use opencv::imgproc::{cvt_color_def, COLOR_BGR2RGB}; | 
|  | 11 | + | 
|  | 12 | +/// Webcam struct definition | 
|  | 13 | +/// The struct wraps the ```VideoCapture``` type, and has custom functions for it. | 
|  | 14 | +/// You can create a new instance with the ```new``` functions. | 
|  | 15 | +#[derive(Debug)] | 
|  | 16 | +pub struct Webcam(VideoCapture); | 
|  | 17 | + | 
|  | 18 | +impl Webcam { | 
|  | 19 | +    /// Create new ```Webcam``` instance with api preference and camera index | 
|  | 20 | +    /// If you want to use the default api_preference you should use ```new_def(i32)``` instead | 
|  | 21 | +    /// API preference consts are available at the [opencv documentation](https://docs.rs/opencv/latest/opencv/index.html). Some exmaples for this const are: ```CAP_MSMF```, ```CAP_V4L```. | 
|  | 22 | +    pub fn new(camera_idx: i32, api_preference: i32) -> anyhow::Result<Self> { | 
|  | 23 | +        let video_capture_handle = VideoCapture::new(camera_idx, api_preference)?; | 
|  | 24 | + | 
|  | 25 | +        if !video_capture_handle.is_opened()? { | 
|  | 26 | +            bail!("Failed to open capture device.") | 
|  | 27 | +        } | 
|  | 28 | + | 
|  | 29 | +        Ok(Self(video_capture_handle)) | 
|  | 30 | +    } | 
|  | 31 | + | 
|  | 32 | +    /// Create new ```Webcam``` instance with auto camera detection. | 
|  | 33 | +    /// Please note that this function tries to auto detect the camera. | 
|  | 34 | +    /// If you have more than one camera you should use the ```new_def(i32)``` function to define which camera you are wanting to use. | 
|  | 35 | +    pub fn new_def_auto_detect() -> anyhow::Result<Self> { | 
|  | 36 | +        let video_capture_handle = VideoCapture::new_def(CAP_ANY)?; | 
|  | 37 | + | 
|  | 38 | +        if !video_capture_handle.is_opened()? { | 
|  | 39 | +            bail!("Failed to open capture device.") | 
|  | 40 | +        } | 
|  | 41 | + | 
|  | 42 | +        Ok(Self(video_capture_handle)) | 
|  | 43 | +    } | 
|  | 44 | + | 
|  | 45 | +    /// Create new ```Webcam``` instance from the camera index. | 
|  | 46 | +    /// The passed in argument defines which camera this function creates a new instance from | 
|  | 47 | +    pub fn new_def(camera_idx: i32) -> anyhow::Result<Self> { | 
|  | 48 | +        let video_capture_handle = VideoCapture::new_def(camera_idx)?; | 
|  | 49 | + | 
|  | 50 | +        if !video_capture_handle.is_opened()? { | 
|  | 51 | +            bail!("Failed to open capture device.") | 
|  | 52 | +        } | 
|  | 53 | + | 
|  | 54 | +        Ok(Self(video_capture_handle)) | 
|  | 55 | +    } | 
|  | 56 | + | 
|  | 57 | +    /// Reads an image out of the ```VideoCapture``` buffer, this removes the bytes of the image from the buffer. | 
|  | 58 | +    /// Returns a tuple of the raw image bytes and the size of the image. | 
|  | 59 | +    /// Please note the image's bytes returned by this function are automaticly converted from [BRG8](https://learn.microsoft.com/en-us/windows/win32/wic/-wic-codec-native-pixel-formats#rgbbgr-color-model) (Which is returned by opencv by default) to RGB8 | 
|  | 60 | +    pub fn get_frame(&mut self) -> anyhow::Result<(Vec<u8>, Size_<i32>)> { | 
|  | 61 | +        //Create frame which will be overwritten | 
|  | 62 | +        let mut frame = Mat::default(); | 
|  | 63 | + | 
|  | 64 | +        //Read frame | 
|  | 65 | +        self.0.read(&mut frame)?; | 
|  | 66 | + | 
|  | 67 | +        //Create corrected_frame | 
|  | 68 | +        let mut corrected_frame = Mat::default(); | 
|  | 69 | + | 
|  | 70 | +        //Color correction | 
|  | 71 | +        cvt_color_def(&frame, &mut corrected_frame, COLOR_BGR2RGB)?; | 
|  | 72 | + | 
|  | 73 | +        //Return captured frame | 
|  | 74 | +        Ok(( | 
|  | 75 | +            corrected_frame.data_bytes()?.to_vec(), | 
|  | 76 | +            corrected_frame.size()?, | 
|  | 77 | +        )) | 
|  | 78 | +    } | 
|  | 79 | + | 
|  | 80 | +    /// Get the backend api's name | 
|  | 81 | +    pub fn get_backend_name(&self) -> anyhow::Result<String> { | 
|  | 82 | +        Ok(self.0.get_backend_name()?) | 
|  | 83 | +    } | 
|  | 84 | + | 
|  | 85 | +    /// This function drops the inner ```VideoCapture``` instance. | 
|  | 86 | +    /// If this function is called the instance wont be able to capture any frames, you will need to create a new instance. | 
|  | 87 | +    pub fn release(&mut self) -> anyhow::Result<()> { | 
|  | 88 | +        Ok(self.0.release()?) | 
|  | 89 | +    } | 
|  | 90 | +} | 
0 commit comments