hs_hackathon_drone/
lib.rs

1use eyre::Context;
2use image::{codecs::jpeg::JpegDecoder, DynamicImage};
3
4/// A connection to the camera of the drone and abstraction to access the drones camera
5pub struct Camera(reqwest::Client);
6
7/// A videoframe recieved from the drones camera
8#[derive(Clone)]
9pub struct Frame(pub DynamicImage);
10
11impl Camera {
12    pub async fn connect() -> color_eyre::Result<Self> {
13        Ok(Self(reqwest::Client::new()))
14    }
15
16    pub async fn snapshot(&self) -> color_eyre::Result<Frame> {
17        let res = self
18            .0
19            .get("http://127.0.0.1:3000/camera?clean=true")
20            .send()
21            .await
22            .wrap_err("request image")?;
23        if !res.status().is_success() {
24            let status = res.status();
25            let body = res.text().await.wrap_err("fetch image error text")?;
26            return Err(eyre::eyre!(body)).wrap_err(format!("image grab gave {status:?}"));
27        };
28        let bytes = res.bytes().await.wrap_err("read bytes")?;
29        let decoder = JpegDecoder::new(&*bytes).wrap_err("launch decoder")?;
30        let img = DynamicImage::from_decoder(decoder).wrap_err("decode frame")?;
31        Ok(Frame(img))
32    }
33}