fix: 树莓派gstreamer pipeline报错

This commit is contained in:
grabbit 2025-04-05 16:43:10 +08:00
parent 90f757a524
commit 94cf574161

View File

@ -63,13 +63,9 @@ impl OpenCVCamera {
// For Linux device files like /dev/video0 // For Linux device files like /dev/video0
if let Some(num_str) = path_str.strip_prefix("/dev/video") { if let Some(num_str) = path_str.strip_prefix("/dev/video") {
if let Ok(device_index) = num_str.parse::<i32>() { if let Ok(device_index) = num_str.parse::<i32>() {
// 在Linux下使用GStreamer pipeline // 在Linux下使用V4L2直接访问摄像头
let pipeline = format!( info!("Opening camera on Linux using V4L2: {}", path_str);
"v4l2src device={} ! videoconvert ! appsink", return Ok(videoio::VideoCapture::new(device_index, videoio::CAP_V4L2)?);
path_str
);
info!("Using GStreamer pipeline: {}", pipeline);
return Ok(videoio::VideoCapture::from_file(path_str, videoio::CAP_V4L2)?);
} else { } else {
return Err(anyhow!("Invalid device number in path: {}", path_str)); return Err(anyhow!("Invalid device number in path: {}", path_str));
} }
@ -201,28 +197,35 @@ impl OpenCVCamera {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
// 在Linux下创建一个使用GStreamer的流 // For Linux device files like /dev/video0
if self.device.starts_with("/dev/video") { if self.device.starts_with("/dev/video") {
let pipeline = format!( if let Some(num_str) = self.device.strip_prefix("/dev/video") {
"v4l2src device={} ! videoconvert ! video/x-raw,width={},height={},format=BGR,framerate={}/1 ! appsink", if let Ok(device_index) = num_str.parse::<i32>() {
self.device, self.width, self.height, 30 // 使用固定的30fps或者从设置获取 // 使用V4L2创建摄像头流
); info!("Starting stream using V4L2: device index {}", device_index);
info!("Starting stream with GStreamer pipeline: {}", pipeline); let mut stream_capture = videoio::VideoCapture::new(device_index, videoio::CAP_V4L2)?;
let stream_capture = videoio::VideoCapture::from_file(&pipeline, videoio::CAP_GSTREAMER)?;
// 应用相同的设置
stream_capture.set(videoio::CAP_PROP_FRAME_WIDTH, self.width as f64)?;
stream_capture.set(videoio::CAP_PROP_FRAME_HEIGHT, self.height as f64)?;
stream_capture.set(videoio::CAP_PROP_FPS, 30.0)?; // 使用固定30fps或从设置获取
if !stream_capture.is_opened()? { if !stream_capture.is_opened()? {
return Err(anyhow!("Failed to open GStreamer camera stream")); return Err(anyhow!("Failed to open V4L2 camera stream"));
} }
self.is_streaming = true; self.is_streaming = true;
info!("Started camera streaming with GStreamer"); info!("Started camera streaming with V4L2");
return Ok(OpenCVCaptureStream { return Ok(OpenCVCaptureStream {
capture: stream_capture, capture: stream_capture,
}); });
} }
} }
}
}
// For non-Linux platforms or if GStreamer approach fails, use regular approach // For non-Linux platforms, use regular approach
// Create a separate VideoCapture for the stream to avoid concurrent access issues // Create a separate VideoCapture for the stream to avoid concurrent access issues
let device = self.device.clone(); let device = self.device.clone();
let mut stream_capture = Self::create_capture_from_path(&device)?; let mut stream_capture = Self::create_capture_from_path(&device)?;