Run a single resolution test with live preview window. 运行单次分辨率测试,带实时预览窗口。 Captures frames for ~5 seconds (or until ESC), returns average FPS. 采集约 5 秒的帧(或按 ESC 退出),返回平均 FPS。
(
test: &ResolutionTest,
)
| 83 | /// Captures frames for ~5 seconds (or until ESC), returns average FPS. |
| 84 | /// 采集约 5 秒的帧(或按 ESC 退出),返回平均 FPS。 |
| 85 | fn run_resolution_test( |
| 86 | test: &ResolutionTest, |
| 87 | ) -> std::result::Result<(f64, String), Box<dyn std::error::Error>> { |
| 88 | let config = CameraConfig::new() |
| 89 | .resolution(test.width, test.height) |
| 90 | .fps(30); |
| 91 | |
| 92 | let mut cap = VideoCapture::open_with(0, config)?; |
| 93 | let actual_w = cap.width() as usize; |
| 94 | let actual_h = cap.height() as usize; |
| 95 | let format = cap.camera().pixel_format().to_string(); |
| 96 | |
| 97 | println!( |
| 98 | " Opened: {}x{} {} (requested {}x{})", |
| 99 | actual_w, actual_h, format, test.width, test.height |
| 100 | ); |
| 101 | |
| 102 | // Create window sized to actual resolution. |
| 103 | // 创建与实际分辨率匹配的窗口。 |
| 104 | let title = format!("rustcv-camera {} ({}x{})", test.label, actual_w, actual_h); |
| 105 | let mut window = Window::new(&title, actual_w, actual_h, WindowOptions::default())?; |
| 106 | |
| 107 | let mut frame = Mat::new(); |
| 108 | let mut display_buf: Vec<u32> = vec![0; actual_w * actual_h]; |
| 109 | |
| 110 | let start = Instant::now(); |
| 111 | let mut fps_timer = Instant::now(); |
| 112 | let mut fps_count: u32 = 0; |
| 113 | let mut fps_display: f64 = 0.0; |
| 114 | let mut total_frames: u64 = 0; |
| 115 | |
| 116 | // Run for ~5 seconds or until ESC. |
| 117 | // 运行约 5 秒或按 ESC 退出。 |
| 118 | while window.is_open() && !window.is_key_down(Key::Escape) && start.elapsed().as_secs() < 5 { |
| 119 | if !cap.read(&mut frame)? { |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | bgr_to_u32(frame.data(), &mut display_buf); |
| 124 | draw_fps_overlay(&mut display_buf, actual_w, fps_display, test.label); |
| 125 | window.update_with_buffer(&display_buf, actual_w, actual_h)?; |
| 126 | |
| 127 | total_frames += 1; |
| 128 | fps_count += 1; |
| 129 | let elapsed = fps_timer.elapsed().as_secs_f64(); |
| 130 | if elapsed >= 1.0 { |
| 131 | fps_display = fps_count as f64 / elapsed; |
| 132 | fps_count = 0; |
| 133 | fps_timer = Instant::now(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | let avg_fps = total_frames as f64 / start.elapsed().as_secs_f64(); |
| 138 | Ok((avg_fps, format)) |
| 139 | } |
| 140 | |
| 141 | /// Convert BGR byte slice to u32 display buffer (0x00RRGGBB). |
| 142 | /// 将 BGR 字节切片转换为 u32 显示缓冲区(0x00RRGGBB)。 |
no test coverage detected