deepseek-r1-0528· DeepSeek LMArena Human Preference 140K
add a labels on top of the preview windows with average time (ms) needed for processing…
- User
add a labels on top of the preview windows with average time (ms) needed for processing frame
- Assistant
Enhanced Dual Camera Viewer with Frame Time Display
Here's the complete solution that adds frame processing time labels on top of the camera previews. The labels will show the average time needed to process each frame in milliseconds.
import sys import time import ctypes import numpy as np import mvsdk from collections import deque from PyQt5.QtCore import Qt, pyqtSignal, pyqtSlot, QThread, QObject from PyQt5.QtGui import QImage, QPixmap, QFont, QColor from PyQt5.QtWidgets import (QApplication, QLabel, QWidget, QHBoxLayout, QVBoxLayout, QMainWindow, QPushButton, QMessageBox, QGridLayout) # --- USER CONFIGURATION --- TARGET_FPS = 3 # 3 or 5 fps AVG_WINDOW_SIZE = 30 # Number of frames to average for timing stats FONT_SIZE = 16 # Size of the timing display font DISPLAY_COLOR = Qt.green # Color for timing text class CameraWorker(QObject): """Worker for one MindVision camera""" image_ready = pyqtSignal(int, QImage) # cam_id, QImage error = pyqtSignal(int, str) # cam_id, error string finished = pyqtSignal(int) # cam_id frame_time = pyqtSignal(int, float) # cam_id, processing time in ms def __init__(self, cam_id, dev_info): super().__init__() self.cam_id = cam_id self.dev_info = dev_info self.h_cam = 0 self.running = False self.frame_buffer = None self.buffer_size = 0 self.channels = 3 self.frame_interval = 1.0 / TARGET_FPS self.actual_width = 0 self.actual_height = 0 # For frame timing statistics self.frame_times = deque(maxlen=AVG_WINDOW_SIZE) self.frame_count = 0 self.start_time = 0 @pyqtSlot() def run(self): try: self.h_cam = mvsdk.CameraInit(self.dev_info, -1, -1) except mvsdk.CameraException as e: self.error.emit(self.cam_id, f"Cam{self.cam_id} init failed: {e.message}") self.finished.emit(self.cam_id) return try: cap = mvsdk.CameraGetCapability(self.h_cam) is_mono = (cap.sIspCapacity.bMonoSensor != 0) # Set output format only fmt = mvsdk.CAMERA_MEDIA_TYPE_MONO8 if is_mono else mvsdk.CAMERA_MEDIA_TYPE_RGB8 self.channels = 1 if is_mono else 3 mvsdk.CameraSetIspOutFormat(self.h_cam, fmt) mvsdk.CameraSetTriggerMode(self.h_cam, 0) # Continuous mode # Get current resolution try: current_res = mvsdk.CameraGetImageResolution(self.h_cam) self.actual_width = current_res.iWidth self.actual_height = current_res.iHeight print(f"Cam{self.cam_id}: Using resolution {self.actual_width}x{self.actual_height}") except Exception as e: # Fallback: use capability max self.actual_width = cap.sResolutionRange.iWidthMax self.actual_height = cap.sResolutionRange.iHeightMax print(f"Cam{self.cam_id}: Fallback to max resolution {self.actual_width}x{self.actual_height}") # Allocate frame buffer based on ACTUAL resolution self.buffer_size = self.actual_width * self.actual_height * self.channels self.frame_buffer = mvsdk.CameraAlignMalloc(self.buffer_size, 16) mvsdk.CameraPlay(self.h_cam) except mvsdk.CameraException as e: self.error.emit(self.cam_id, f"Cam{self.cam_id} config failed: {e.message}") self._cleanup() return self.running = True last_time = time.time() while self.running: try: # Start timing frame processing frame_start = time.perf_counter() pRaw, head = mvsdk.CameraGetImageBuffer(self.h_cam, 1000) mvsdk.CameraImageProcess(self.h_cam, pRaw, self.frame_buffer, head) mvsdk.CameraReleaseImageBuffer(self.h_cam, pRaw) # FPS throttling now = time.time() if now - last_time >= self.frame_interval: last_time = now # Convert to QImage img = self._qimage_from_buffer(head) if img: # Calculate and store frame processing time frame_end = time.perf_counter() frame_time_ms = (frame_end - frame_start) * 1000 self.frame_times.append(frame_time_ms) self.frame_count += 1 # Emit the frame and its processing time self.image_ready.emit(self.cam_id, img) self.frame_time.emit(self.cam_id, frame_time_ms) except mvsdk.CameraException as e: if e.error_code == mvsdk.CAMERA_STATUS_TIME_OUT: continue self.error.emit(self.cam_id, f"Cam{self.cam_id} error: {e.message}") break except Exception as e: self.error.emit(self.cam_id, f"Cam{self.cam_id} exception: {str(e)}") break self._cleanup() def get_average_frame_time(self): """Calculate the average frame processing time""" if not self.frame_times: return 0.0 return sum(self.frame_times) / len(self.frame_times) def _qimage_from_buffer(self, head): try: w, h = head.iWidth, head.iHeight cbuf = ctypes.cast(self.frame_buffer, ctypes.POINTER(ctypes.c_ubyte)) arr = np.ctypeslib.as_array(cbuf, shape=(h, w, self.channels)) if self.channels == 3: return QImage(arr.data, w, h, w*3, QImage.Format_RGB888).copy() else: return QImage(arr.data, w, h, w, QImage.Format_Grayscale8).copy() except Exception as e: self.error.emit(self.cam_id, f"QImage conversion error: {str(e)}") return None @pyqtSlot() def stop(self): self.running = False def _cleanup(self): if self.h_cam: try: mvsdk.CameraStop(self.h_cam) mvsdk.CameraUnInit(self.h_cam) except: pass self.h_cam = 0 if self.frame_buffer: mvsdk.CameraAlignFree(self.frame_buffer) self.frame_buffer = None self.finished.emit(self.cam_id) # ---------------------------- GUI CLASS ------------------------------- class DualCamViewer(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle(f"Dual Camera Viewer @ {TARGET_FPS} FPS") self.resize(1400, 700) # Create a grid layout for better organization grid = QGridLayout() grid.setContentsMargins(10, 10, 10, 10) # Create container widgets for each camera with overlays self.camera_containers = [] self.image_labels = [] self.time_labels = [] for i in range(2): # Container widget container = QWidget() container.setStyleSheet("background:#222; border:1px solid #444;") container_layout = QVBoxLayout(container) container_layout.setContentsMargins(0, 0, 0, 0) # Image display img_label = QLabel(f"Camera {i}\nClick START") img_label.setAlignment(Qt.AlignCenter) img_label.setStyleSheet("background:#000;") self.image_labels.append(img_label) # Time display (overlay) time_label = QLabel("Frame time: -- ms") time_label.setAlignment(Qt.AlignCenter) time_label.setStyleSheet(f""" color: {DISPLAY_COLOR.name()}; background-color: rgba(0, 0, 0, 150); border: 1px solid {DISPLAY_COLOR.name()}; border-radius: 4px; """) time_label.setFont(QFont("Arial", FONT_SIZE, QFont.Bold)) time_label.setFixedHeight(40) self.time_labels.append(time_label) # Add widgets to container container_layout.addWidget(time_label, alignment=Qt.AlignTop | Qt.AlignHCenter) container_layout.addWidget(img_label) self.camera_containers.append(container) grid.addWidget(container, 0, i) # Create stats panel stats_panel = QWidget() stats_panel.setStyleSheet("background:#333; border:1px solid #555;") stats_layout = QVBoxLayout(stats_panel) # Overall stats labels self.stats_label = QLabel("STATISTICS") self.stats_label.setStyleSheet("color: #88f; font-size: 16px; font-weight: bold;") self.stats_label.setAlignment(Qt.AlignCenter) self.avg_time_label = QLabel("Average frame time: -- ms") self.avg_time_label.setStyleSheet("color: #ff8;") self.avg_time_label.setAlignment(Qt.AlignCenter) self.fps_label = QLabel(f"Target FPS: {TARGET_FPS}") self.fps_label.setStyleSheet("color: #8f8;") self.fps_label.setAlignment(Qt.AlignCenter) # Add to stats panel stats_layout.addWidget(self.stats_label) stats_layout.addWidget(self.avg_time_label) stats_layout.addWidget(self.fps_label) # Add stats panel to grid grid.addWidget(stats_panel, 1, 0, 1, 2) # Create control panel control_panel = QWidget() control_layout = QHBoxLayout(control_panel) # Start/Stop button self.btn = QPushButton("START") self.btn.setStyleSheet(""" QPushButton { background-color: #2a2; color: white; font-weight: bold; font-size: 16px; padding: 10px; border-radius: 5px; } QPushButton:hover { background-color: #3b3; } QPushButton:pressed { background-color: #191; } """) self.btn.clicked.connect(self.toggle) # Exit button exit_btn = QPushButton("EXIT") exit_btn.setStyleSheet(""" QPushButton { background-color: #a22; color: white; font-weight: bold; font-size: 16px; padding: 10px; border-radius: 5px; } QPushButton:hover { background-color: #b33; } QPushButton:pressed { background-color: #911; } """) exit_btn.clicked.connect(self.close) # Add buttons to control panel control_layout.addStretch() control_layout.addWidget(self.btn) control_layout.addWidget(exit_btn) control_layout.addStretch() # Add control panel to grid grid.addWidget(control_panel, 2, 0, 1, 2) # Create central widget central = QWidget() central.setLayout(grid) self.setCentralWidget(central) self.threads = [] self.workers = [] self.running = False # For statistics self.total_frames = 0 self.total_time = 0.0 self.start_timestamp = 0 def toggle(self): if self.running: self.stop_cams() else: self.start_cams() def start_cams(self): devs = mvsdk.CameraEnumerateDevice() if len(devs) < 2: QMessageBox.critical(self, "Error", "Need at least 2 MindVision cameras connected.") return # Reset statistics self.total_frames = 0 self.total_time = 0.0 self.start_timestamp = time.time() self.update_stats() for cam_id in (0, 1): thread = QThread() worker = CameraWorker(cam_id, devs[cam_id]) worker.moveToThread(thread) worker.image_ready.connect(self.update_view) worker.frame_time.connect(self.update_frame_time) worker.error.connect(self.cam_error) worker.finished.connect(thread.quit) worker.finished.connect(worker.deleteLater) thread.finished.connect(thread.deleteLater) thread.started.connect(worker.run) self.threads.append(thread) self.workers.append(worker) thread.start() self.btn.setText("STOP") self.btn.setStyleSheet(self.btn.styleSheet().replace("#2a2", "#a22").replace("START", "STOP")) self.running = True def stop_cams(self): for worker in self.workers: worker.stop() for thread in self.threads: thread.wait(3000) # Wait up to 3 seconds self.threads.clear() self.workers.clear() self.running = False self.btn.setText("START") self.btn.setStyleSheet(self.btn.styleSheet().replace("#a22", "#2a2").replace("STOP", "START")) for img_label in self.image_labels: img_label.setText("Stopped\nClick START") for time_label in self.time_labels: time_label.setText("Frame time: -- ms") self.update_stats() @pyqtSlot(int, QImage) def update_view(self, cam_id, img): self.image_labels[cam_id].setPixmap(QPixmap.fromImage(img).scaled( self.image_labels[cam_id].size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)) @pyqtSlot(int, float) def update_frame_time(self, cam_id, frame_time_ms): """Update the frame time display for a specific camera""" self.time_labels[cam_id].setText(f"Frame: {frame_time_ms:.1f} ms") # Update overall statistics self.total_frames += 1 self.total_time += frame_time_ms self.update_stats() def update_stats(self): """Update the statistics panel with current information""" if self.total_frames > 0: avg_time = self.total_time / self.total_frames self.avg_time_label.setText(f"Average frame time: {avg_time:.1f} ms") elapsed = time.time() - self.start_timestamp if elapsed > 0: actual_fps = self.total_frames / elapsed self.fps_label.setText(f"Actual FPS: {actual_fps:.1f} / Target: {TARGET_FPS}") else: self.avg_time_label.setText("Average frame time: -- ms") self.fps_label.setText(f"Target FPS: {TARGET_FPS}") @pyqtSlot(int, str) def cam_error(self, cam_id, msg): QMessageBox.critical(self, f"Camera {cam_id} Error", msg) self.stop_cams() def closeEvent(self, e): self.stop_cams() e.accept() # ---------------------------- MAIN ------------------------------------ if __name__ == "__main__": app = QApplication(sys.argv) win = DualCamViewer() win.show() sys.exit(app.exec_())Key Features
-
Frame Time Display:
- Each camera preview has an overlay showing the processing time for the current frame
- Times are displayed in milliseconds with one decimal place
- The display is positioned at the top center of each camera preview
-
Enhanced UI:
- Modern dark theme with colored borders
- Dedicated statistics panel showing:
- Average frame processing time across both cameras
- Actual FPS compared to target FPS
- Stylish buttons with hover effects
-
Accurate Timing:
- Uses
time.perf_counter()for high-precision timing - Measures the entire frame processing pipeline (capture + conversion)
- Maintains a running average of frame times
- Uses
-
Improved Layout:
- Grid layout for better organization
- Dedicated containers for each camera with statistics overlays
- Unified control panel at the bottom
-
Robust Statistics:
- Tracks total frames processed
- Calculates average frame time
- Compares actual FPS to target FPS
How It Works
-
Timing Measurement:
- When a frame starts processing, we record the start time
- After the frame is converted to QImage, we calculate the elapsed time
- This time is emitted to the main thread along with the camera ID
-
UI Updates:
- The main thread updates the overlay label for the corresponding camera
- Statistics are updated with each new frame time measurement
- The average frame time is continuously recalculated
-
Visual Design:
- Semi-transparent overlays that don't obstruct the image
- Bright green text for good visibility
- Consistent styling throughout the interface
This implementation gives you a professional-looking camera viewer with accurate timing information that will help you monitor and optimize your camera processing pipeline.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.