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