rvideo 0.5.3

Real-time video server for embedded apps
Documentation
use std::{thread, time::Duration};

use image::{ImageBuffer, Rgb};
use imageproc::drawing::draw_text_mut;
use rusttype::{Font, Scale};
use rvideo::{BoundingBox, Format, Frame};
use serde::Serialize;

const FONT: &[u8] = include_bytes!("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf");

#[derive(Serialize)]
struct FrameInfo {
    source: String,
    frame_number: u64,
    #[serde(rename = ".bboxes")]
    bounding_boxes: Vec<BoundingBox>,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let width = 640;
    let height = 480;
    let stream = rvideo::add_stream(Format::Rgb8, width, height)?;
    thread::spawn(move || {
        let mut frame_number = 0;
        let font = Font::try_from_bytes(FONT).unwrap();
        loop {
            let mut imgbuf =
                ImageBuffer::<Rgb<u8>, Vec<u8>>::from_fn(width.into(), height.into(), |_, _| {
                    Rgb([0, 0, 0])
                });
            draw_text_mut(
                &mut imgbuf,
                Rgb::from([255, 255, 255]),
                0,
                0,
                Scale { x: 100.0, y: 100.0 },
                &font,
                &frame_number.to_string(),
            );
            dbg!(frame_number);
            let metadata = FrameInfo {
                source: "test".to_string(),
                frame_number,
                bounding_boxes: vec![
                    BoundingBox::new(100, 300, 100, 100).with_color([255, 0, 0]),
                    BoundingBox::new(220, 220, 50, 50),
                ],
            };
            stream
                .send_frame(Frame::new_with_metadata(
                    rmp_serde::to_vec_named(&metadata).unwrap().into(),
                    imgbuf.to_vec().into(),
                ))
                .unwrap();
            thread::sleep(Duration::from_millis(100));
            frame_number += 1;
        }
    });
    rvideo::serve("127.0.0.1:3001")?;
    Ok(())
}