fix: wait for camera readiness during startup
This commit is contained in:
25
src/main.rs
25
src/main.rs
@@ -11,11 +11,12 @@ use std::{
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use opencv::{core::Mat, imgproc, prelude::*, videoio};
|
||||
use opencv::{core::Mat, imgproc, prelude::*};
|
||||
use slint::{Rgb8Pixel, SharedPixelBuffer};
|
||||
|
||||
slint::include_modules!();
|
||||
|
||||
mod camera;
|
||||
mod capture;
|
||||
mod diagnostics;
|
||||
mod hand_pose;
|
||||
@@ -31,32 +32,12 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
if let Err(error) = diagnostics::log_runtime() {
|
||||
eprintln!("Failed to read OpenCV runtime diagnostics: {error}");
|
||||
}
|
||||
let mut cam =
|
||||
videoio::VideoCapture::new(0, videoio::CAP_V4L2).expect("Failed to get video capture");
|
||||
let mut frame = Mat::default();
|
||||
|
||||
let codec = videoio::VideoWriter::fourcc('M', 'J', 'P', 'G')?;
|
||||
|
||||
for (property, value) in [
|
||||
(videoio::CAP_PROP_FOURCC, codec as f64),
|
||||
(videoio::CAP_PROP_FRAME_WIDTH, 640.0),
|
||||
(videoio::CAP_PROP_FRAME_HEIGHT, 480.0),
|
||||
] {
|
||||
if !cam.set(property, value)? {
|
||||
eprintln!("Camera rejected property {property}");
|
||||
}
|
||||
}
|
||||
|
||||
let mut camera_log = diagnostics::SlowLog::new(
|
||||
"camera read",
|
||||
Duration::from_millis(100),
|
||||
"Check camera passthrough and camera frame rate.",
|
||||
);
|
||||
for _ in 0..30 {
|
||||
if !camera_log.run(|| cam.read(&mut frame))? || frame.empty() {
|
||||
return Err("Failed to capture a frame".into());
|
||||
}
|
||||
}
|
||||
let (mut cam, mut frame) = camera::wait_until_ready(&mut camera_log)?;
|
||||
|
||||
let mut detector = palm::Detector::new()?;
|
||||
let ui = AppWindow::new()?;
|
||||
|
||||
Reference in New Issue
Block a user