Skip to content

Commit

Permalink
🔧 load webcam params from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
chriamue committed Jan 30, 2022
1 parent 10c1960 commit 1e99220
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ornithology-pi"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
authors = ["Christian <[email protected]>"]
description = "Capture birds in your garden, running on raspberry pi."
Expand Down Expand Up @@ -33,8 +33,8 @@ base64 = { version = "0.13.0", optional = true }
bluer = { version = "0.13", optional = true }
bytes = "1.1.0"
chrono = "0.4"
figment = "0.10.6"
format-bytes = "0.1"

futures = "0.3"

image = { version = "0.23.14", default-features = false, features = [
Expand Down
5 changes: 5 additions & 0 deletions Rocket.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
ctrlc = false
signals = ["term", "hup"]

[default.camera]
width = 640
height = 480
fps = 30

[release]
port = 8000
address = "0.0.0.0"
5 changes: 5 additions & 0 deletions ornithology-pi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
ctrlc = false
signals = ["term", "hup"]

[default.camera]
width = 640
height = 480
fps = 30

[release]
port = 8000
address = "0.0.0.0"
22 changes: 16 additions & 6 deletions src/capture/webcam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@ use std::time::Duration;
use super::Capture;

pub struct WebCam {
width: u32,
height: u32,
fps: u32,
frame: Arc<Mutex<ImageBuffer<Rgb<u8>, Vec<u8>>>>,
running: Arc<Mutex<bool>>,
}

impl WebCam {
pub fn new(width: u32, height: u32) -> Result<Self, Box<dyn Error>> {
pub fn new(width: u32, height: u32, fps: u32) -> Result<Self, Box<dyn Error>> {
let frame = Arc::new(Mutex::new(ImageBuffer::new(width, height)));
let running = Arc::new(Mutex::new(false));

let mut webcam = Self { frame, running };
let mut webcam = Self {
width,
height,
fps,
frame,
running,
};
webcam.start();
Ok(webcam)
}
Expand All @@ -38,8 +47,9 @@ impl WebCam {
}

pub fn start(&mut self) {
let width = self.frame.lock().unwrap().width();
let height = self.frame.lock().unwrap().height();
let width = self.width;
let height = self.height;
let fps = self.fps;
let running = self.running.clone();
let frame = self.frame.clone();

Expand All @@ -50,7 +60,7 @@ impl WebCam {
width,
height,
FrameFormat::MJPEG,
30,
fps,
)),
)
.unwrap();
Expand All @@ -72,7 +82,7 @@ impl WebCam {

impl Default for WebCam {
fn default() -> Self {
Self::new(1920, 1080).unwrap()
Self::new(1920, 1080, 30).unwrap()
}
}

Expand Down
35 changes: 35 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use figment::{
providers::{Env, Format, Toml},
Figment,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
pub struct Camera {
pub width: u32,
pub height: u32,
pub fps: u32,
}

impl Default for Camera {
fn default() -> Self {
Camera {
width: 640,
height: 480,
fps: 30,
}
}
}

#[derive(Default, Debug, Deserialize, Serialize)]
pub struct Config {
pub camera: Camera,
}

pub fn load_config() -> Config {
Figment::new()
.merge(Toml::file(Env::var_or("ROCKET_CONFIG", "Rocket.toml")).nested())
.merge(Env::prefixed("ROCKET_").ignore(&["PROFILE"]).global())
.extract()
.unwrap_or_default()
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ pub use detector::BirdDetector;
pub mod capture;
pub use capture::Capture;
pub use capture::WebCam;

pub mod config;
pub use config::Config;

pub mod mjpeg;
pub use mjpeg::MJpeg;

Expand Down
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(feature = "bluetooth")]
use ornithology_pi::bluetooth::run_bluetooth;
use ornithology_pi::config;
#[cfg(feature = "hotspot")]
use ornithology_pi::hotspot::Hotspot;
#[cfg(feature = "server")]
Expand Down Expand Up @@ -59,8 +60,15 @@ async fn run_detector(sightings: Arc<Mutex<Vec<Sighting>>>, capture: Arc<Mutex<W

#[tokio::main]
async fn main() {
let config = config::load_config();
let sightings: Arc<Mutex<Vec<Sighting>>> = Arc::new(Mutex::new(Vec::new()));
let capture: Arc<Mutex<WebCam>> = Arc::new(Mutex::new(WebCam::default()));
let capture: Arc<Mutex<WebCam>> = Arc::new(Mutex::new(WebCam::new(
config.camera.width.clone(),
config.camera.height.clone(),
config.camera.fps.clone(),
).unwrap()));

println!("Loaded Config: {:?}", config);

#[cfg(feature = "bluetooth")]
let bluetooth_thread = tokio::spawn(run_bluetooth(sightings.clone()));
Expand Down

0 comments on commit 1e99220

Please sign in to comment.