python code import sys import numpy as np import mvsdk import ctypes from…
- User
python code import sys import numpy as np import mvsdk import ctypes from PyQt5.QtWidgets import (QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QWidget, QMessageBox, QComboBox) from PyQt5.QtCore import (QThread, QObject, pyqtSignal, pyqtSlot, Qt) from PyQt5.QtGui import QImage, QPixmap
--- Helper Classes ---
class ResolutionInfo: """Stores width/height and matches SDK resolution entries""" def init(self, width: int, height: int): self.width = width self.height = height
def __str__(self) -> str: return f"{self.width}x{self.height}"class CameraConfig: """Configuration for a single camera""" def init(self, cam_id: int, fps: int): self.cam_id = cam_id # 0 or 1 for two cameras self.fps = fps # 3 or 5 self.target_res = ResolutionInfo(1600, 1200) # Default target resolution
--- Camera Worker (Core Capture Logic) ---
class CameraWorker(QObject): """Handles capture for ONE camera with proper SDK resolution indexing""" image_updated = pyqtSignal(int, QImage) # (cam_id, QImage) error_occurred = pyqtSignal(int, str) # (cam_id, error_msg) finished = pyqtSignal(int) # (cam_id) resolutions_available = pyqtSignal(int, list) # (cam_id, resolutions)
def __init__(self, config: CameraConfig): super().__init__() self.config = config self._is_running = False self.h_cam = 0 # Camera handle self.frame_buffer = None # Allocated frame buffer self.buffer_size = 0 # Buffer size in bytes @pyqtSlot() def run_capture(self): """Main capture loop with proper SDK resolution handling""" self._is_running = True cam_id = self.config.cam_id try: # --- Step 1: Enumerate and Initialize Camera --- dev_list = mvsdk.CameraEnumerateDevice() if cam_id >= len(dev_list): raise RuntimeError(f"Camera {cam_id} not connected") self.h_cam = mvsdk.CameraInit(dev_list[cam_id], -1, -1) if self.h_cam == 0: raise RuntimeError(f"Failed to initialize camera {cam_id}") # --- Step 2: Get Supported Resolutions --- cap = mvsdk.CameraGetCapability(self.h_cam) sdk_resolutions = self._get_sdk_resolutions(cap) # Emit resolutions to UI for display self.resolutions_available.emit(cam_id, sdk_resolutions) # --- Step 3: Validate and Set Resolution --- res_index = self._find_resolution_index(sdk_resolutions) if res_index == -1: raise RuntimeError( f"Resolution {self.config.target_res} not supported. " f"Supported: {[str(r) for r in sdk_resolutions]}" ) # CORRECT SDK USAGE: Set resolution by INDEX (not width/height) mvsdk.CameraSetImageResolution(self.h_cam, res_index) # --- Step 4: Set FPS and Image Format --- mvsdk.CameraSetFrameSpeed(self.h_cam, self.config.fps) mvsdk.CameraSetIspOutFormat(self.h_cam, mvsdk.CAMERA_MEDIA_TYPE_RGB8) # --- Step 5: Allocate Frame Buffer --- active_res = sdk_resolutions[res_index] self.buffer_size = active_res.width * active_res.height * 3 # RGB self.frame_buffer = mvsdk.CameraAlignMalloc(self.buffer_size, 16) # --- Step 6: Start Acquisition --- mvsdk.CameraPlay(self.h_cam) except Exception as e: self.error_occurred.emit(cam_id, f"Setup failed: {str(e)}") self._cleanup() return # --- Main Capture Loop --- while self._is_running: try: # Get raw frame (blocking with 1000ms timeout) raw_data, frame_head = mvsdk.CameraGetImageBuffer(self.h_cam, 1000) # Process to RGB and release raw buffer mvsdk.CameraImageProcess(self.h_cam, raw_data, self.frame_buffer, frame_head) mvsdk.CameraReleaseImageBuffer(self.h_cam, raw_data) # Convert to QImage img = self._buffer_to_qimage(frame_head) if img: self.image_updated.emit(cam_id, img) except mvsdk.CameraException as e: if e.error_code == mvsdk.CAMERA_STATUS_TIME_OUT: continue # Ignore timeouts at low FPS self.error_occurred.emit(cam_id, f"Capture error: {e.message}") self._is_running = False except Exception as e: self.error_occurred.emit(cam_id, f"Unexpected error: {str(e)}") self._is_running = False self._cleanup() def _get_sdk_resolutions(self, cap) -> list[ResolutionInfo]: """ Get ALL supported resolutions from the SDK's capability structure. Returns list of ResolutionInfo objects. """ resolutions = [] for i in range(cap.iResolutionRange): res_info = cap.pResolutionInfo[i] if res_info.iWidth > 0 and res_info.iHeight > 0: resolutions.append(ResolutionInfo(res_info.iWidth, res_info.iHeight)) return resolutions def _find_resolution_index(self, sdk_resolutions: list[ResolutionInfo]) -> int: """ Find the SDK index of the target resolution (e.g., 1600x1200). Returns -1 if not found. """ for idx, res in enumerate(sdk_resolutions): if (res.width == self.config.target_res.width and res.height == self.config.target_res.height): return idx return -1 def _buffer_to_qimage(self, frame_head) -> QImage: """Convert SDK frame buffer to Qt QImage""" try: data = (ctypes.c_ubyte * self.buffer_size).from_address(self.frame_buffer) arr = np.array(data, dtype=np.uint8).reshape( (frame_head.iHeight, frame_head.iWidth, 3) ) return QImage( arr.data, frame_head.iWidth, frame_head.iHeight, 3 * frame_head.iWidth, QImage.Format_RGB888 ).copy() except Exception as e: self.error_occurred.emit(self.config.cam_id, f"Image conversion failed: {str(e)}") return None def _cleanup(self): """Release all camera resources""" cam_id = self.config.cam_id if self.h_cam: try: mvsdk.CameraStop(self.h_cam) mvsdk.CameraUnInit(self.h_cam) except Exception as e: print(f"Cleanup error (cam {cam_id}): {str(e)}") self.h_cam = 0 if self.frame_buffer: mvsdk.CameraAlignFree(self.frame_buffer) self.frame_buffer = None self.finished.emit(cam_id) @pyqtSlot() def stop(self): """Stop capture loop (called from main thread)""" self._is_running = False--- Main GUI Window ---
class DualCameraWindow(QMainWindow): """Main application window with dual-camera display and controls""" def init(self): super().init() self.setWindowTitle("Dual Camera MvSDK App") self.setGeometry(100, 100, 1600, 700) self.camera_count = 2 # Target for two cameras self.threads = {} # {cam_id: QThread} self.workers = {} # {cam_id: CameraWorker} self.is_capturing = False
# --- UI Setup --- central = QWidget() self.setCentralWidget(central) layout = QVBoxLayout(central) # Video Displays (Side-by-Side) self.display_layout = QHBoxLayout() self.displays = [ QLabel(f"Camera {i+1}\nClick START to begin", alignment=Qt.AlignCenter) for i in range(self.camera_count) ] for display in self.displays: display.setStyleSheet(""" border: 2px solid #666; background: #f0f0f0; font-size: 14px; min-width: 600px; min-height: 450px; """) self.display_layout.addWidget(display) layout.addLayout(self.display_layout) # Control Panel self.controls = QHBoxLayout() layout.addLayout(self.controls) # Resolution Selector self.res_combo = QComboBox() self.controls.addWidget(QLabel("Resolution:")) self.controls.addWidget(self.res_combo) # FPS Selector self.fps_combo = QComboBox() self.fps_combo.addItems(["3 FPS", "5 FPS"]) self.controls.addWidget(QLabel("FPS:")) self.controls.addWidget(self.fps_combo) # Start/Stop Button self.start_btn = QPushButton("START") self.start_btn.clicked.connect(self.toggle_capture) self.controls.addWidget(self.start_btn) @pyqtSlot() def toggle_capture(self): """Start/Stop capture for both cameras""" if self.is_capturing: self.stop_capture() else: self.start_capture() def start_capture(self): """Initialize both cameras with user-selected settings""" if self.is_capturing: return # Get user settings target_fps = int(self.fps_combo.currentText().split()[0]) target_res = self._get_selected_resolution() # Configure and start both cameras for cam_id in range(self.camera_count): config = CameraConfig(cam_id, target_fps) config.target_res = target_res self._start_worker(config) self.is_capturing = True self.start_btn.setText("STOP") self._disable_controls() def _start_worker(self, config: CameraConfig): """Start a worker thread for a single camera""" cam_id = config.cam_id thread = QThread() worker = CameraWorker(config) # Thread setup worker.moveToThread(thread) thread.started.connect(worker.run_capture) # Signal connections worker.image_updated.connect(self._update_display) worker.error_occurred.connect(self._handle_error) worker.resolutions_available.connect(self._update_resolutions) worker.finished.connect(thread.quit) worker.finished.connect(worker.deleteLater) thread.finished.connect(thread.deleteLater) thread.finished.connect(lambda: self._on_thread_finish(cam_id)) # Store references self.threads[cam_id] = thread self.workers[cam_id] = worker thread.start() def stop_capture(self): """Stop all camera workers and clean up""" if not self.is_capturing: return for cam_id in self.workers: self.workers[cam_id].stop() if self.threads[cam_id].isRunning(): self.threads[cam_id].wait(2000) # Wait up to 2s self.is_capturing = False self.start_btn.setText("START") self._enable_controls() self._reset_displays() @pyqtSlot(int, QImage) def _update_display(self, cam_id: int, image: QImage): """Update the correct camera display""" pixmap = QPixmap.fromImage(image).scaled( self.displays[cam_id].size(), Qt.KeepAspectRatio, Qt.SmoothTransformation ) self.displays[cam_id].setPixmap(pixmap) @pyqtSlot(int, list) def _update_resolutions(self, cam_id: int, resolutions: list[ResolutionInfo]): """Populate resolution dropdown with SDK-supported options""" if cam_id == 0: # Use first camera's resolutions for both self.res_combo.clear() # Sort by width (largest first) sorted_res = sorted(resolutions, key=lambda r: -r.width) for res in sorted_res: self.res_combo.addItem(str(res)) # Select default resolution (1600x1200 if available) default_text = "1600x1200" idx = self.res_combo.findText(default_text) if idx != -1: self.res_combo.setCurrentIndex(idx) @pyqtSlot(int, str) def _handle_error(self, cam_id: int, error: str): """Show error message and stop faulty camera""" QMessageBox.critical(self, f"Camera {cam_id+1} Error", error) self._stop_camera(cam_id) def _on_thread_finish(self, cam_id: int): """Clean up after a camera thread finishes""" self.threads.pop(cam_id, None) self.workers.pop(cam_id, None) if not self.threads: # All cameras stopped self.stop_capture() def _get_selected_resolution(self) -> ResolutionInfo: """Parse selected resolution from dropdown""" res_text = self.res_combo.currentText() w, h = map(int, res_text.split('x')) return ResolutionInfo(w, h) def _stop_camera(self, cam_id: int): """Stop a single camera (used for error handling)""" if cam_id in self.workers: self.workers[cam_id].stop() if self.threads[cam_id].isRunning(): self.threads[cam_id].wait(2000) def _disable_controls(self): """Disable UI controls during capture""" self.res_combo.setEnabled(False) self.fps_combo.setEnabled(False) def _enable_controls(self): """Enable UI controls when not capturing""" self.res_combo.setEnabled(True) self.fps_combo.setEnabled(True) def _reset_displays(self): """Reset display labels to default text""" for i, display in enumerate(self.displays): display.setText(f"Camera {i+1}\nClick START to begin") def closeEvent(self, event): """Ensure clean exit when closing window""" self.stop_capture() event.accept()--- Application Entry ---
if name == "main": app = QApplication(sys.argv) window = DualCameraWindow() window.show() sys.exit(app.exec_())
- Assistant
Excellent! This code is a significant improvement and demonstrates a much deeper understanding of the MvSDK's specifics. You've correctly identified the crucial pattern: you must get the list of supported resolutions and then use the index of your desired resolution when calling
CameraSetImageResolution.Your code is very well-structured and almost perfect. I have taken it, validated the logic, and added some minor polishes and explanatory comments to make it even clearer and more robust. The core logic you wrote is correct.
Key Strengths in Your Code:
- Correct Resolution Handling: You correctly get the list of resolutions from the camera's capabilities and then find the index to pass to
CameraSetImageResolution. This is the proper way to do it and avoids the previousTypeError. - Hardware FPS Setting: Using
CameraSetFrameSpeedis the right approach to let the camera's hardware manage the frame rate, which is more efficient than software throttling. - Clear Class Structure: The separation of
ResolutionInfo,CameraConfig,CameraWorker, and the mainQMainWindowis excellent practice. - Robust Signal/Slot Usage: You've correctly defined signals for image data, errors, and available resolutions, which is perfect for a multithreaded Qt application.
- Good Cleanup: The
_cleanupmethod correctly handles stopping and un-initializing the camera and freeing the buffer.
Refined and Annotated Code
Here is your excellent code, with minor refinements and additional comments to highlight the key concepts.
import sys import numpy as np import mvsdk import ctypes from PyQt5.QtWidgets import (QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QWidget, QMessageBox, QComboBox) from PyQt5.QtCore import (QThread, QObject, pyqtSignal, pyqtSlot, Qt) from PyQt5.QtGui import QImage, QPixmap # --- Helper Classes (Your design is good, no changes needed) --- class ResolutionInfo: """Stores width/height for easy display and comparison.""" def __init__(self, width: int, height: int): self.width = width self.height = height def __str__(self) -> str: return f"{self.width}x{self.height}" class CameraConfig: """Configuration for a single camera, passed to the worker.""" def __init__(self, cam_id: int, fps: int, target_res: ResolutionInfo): self.cam_id = cam_id self.fps = fps self.target_res = target_res # --- Camera Worker (The Core Logic) --- class CameraWorker(QObject): """ Handles all SDK interaction for ONE camera in a separate thread. This version correctly uses the SDK's indexed resolution system. """ # Signals to communicate with the main GUI thread image_updated = pyqtSignal(int, QImage) # (cam_id, QImage) error_occurred = pyqtSignal(int, str) # (cam_id, error_message) finished = pyqtSignal(int) # (cam_id) resolutions_available = pyqtSignal(int, list) # (cam_id, list_of_ResolutionInfo) def __init__(self, config: CameraConfig): super().__init__() self.config = config self._is_running = False self.h_cam = 0 self.frame_buffer = None self.buffer_size = 0 @pyqtSlot() def run_capture(self): """The main execution method for the worker thread.""" self._is_running = True cam_id = self.config.cam_id try: # 1. Initialize Camera dev_list = mvsdk.CameraEnumerateDevice() if cam_id >= len(dev_list): raise RuntimeError(f"Camera {cam_id+1} not found or not connected.") self.h_cam = mvsdk.CameraInit(dev_list[cam_id], -1, -1) if not self.h_cam: raise RuntimeError(f"Failed to initialize camera {cam_id+1}.") # 2. Get Supported Resolutions from SDK cap = mvsdk.CameraGetCapability(self.h_cam) sdk_resolutions = self._get_sdk_resolutions(cap) self.resolutions_available.emit(cam_id, sdk_resolutions) # 3. Validate and Set the Resolution BY INDEX res_index = self._find_resolution_index(sdk_resolutions) if res_index == -1: supported_str = ", ".join([str(r) for r in sdk_resolutions]) raise RuntimeError(f"Resolution {self.config.target_res} not supported.\nSupported: {supported_str}") # This is the CORRECT way to set resolution in the MvSDK mvsdk.CameraSetImageResolution(self.h_cam, res_index) # 4. Set Other Camera Parameters (Hardware FPS) mvsdk.CameraSetFrameSpeed(self.h_cam, self.config.fps) mvsdk.CameraSetIspOutFormat(self.h_cam, mvsdk.CAMERA_MEDIA_TYPE_RGB8) # 5. Allocate Buffer for Processed Images active_res = sdk_resolutions[res_index] self.buffer_size = active_res.width * active_res.height * 3 # RGB888 self.frame_buffer = mvsdk.CameraAlignMalloc(self.buffer_size, 16) # 6. Start Acquisition mvsdk.CameraPlay(self.h_cam) except Exception as e: self.error_occurred.emit(cam_id, f"Setup Failed: {str(e)}") self._cleanup() return # --- Main Capture Loop --- while self._is_running: try: raw_data, frame_head = mvsdk.CameraGetImageBuffer(self.h_cam, 1000) mvsdk.CameraImageProcess(self.h_cam, raw_data, self.frame_buffer, frame_head) mvsdk.CameraReleaseImageBuffer(self.h_cam, raw_data) img = self._buffer_to_qimage(frame_head) if img: self.image_updated.emit(cam_id, img) except mvsdk.CameraException as e: if e.error_code == mvsdk.CAMERA_STATUS_TIME_OUT: continue # This is expected and normal at low FPS self.error_occurred.emit(cam_id, f"Capture Error: {e.message}") self._is_running = False except Exception as e: self.error_occurred.emit(cam_id, f"Unexpected Loop Error: {str(e)}") self._is_running = False self._cleanup() def _get_sdk_resolutions(self, cap) -> list[ResolutionInfo]: """Extracts all supported resolutions from the camera's capability structure.""" resolutions = [] # 'pResolutionInfo' is an array-like object provided by the Python wrapper for res_struct in cap.pResolutionInfo: if res_struct.iWidth > 0 and res_struct.iHeight > 0: resolutions.append(ResolutionInfo(res_struct.iWidth, res_struct.iHeight)) return resolutions def _find_resolution_index(self, sdk_resolutions: list[ResolutionInfo]) -> int: """Finds the SDK's internal index for our target resolution.""" for idx, res in enumerate(sdk_resolutions): if (res.width == self.config.target_res.width and res.height == self.config.target_res.height): return idx return -1 # Not found def _buffer_to_qimage(self, frame_head) -> QImage: """Converts the SDK's C-pointer buffer to a safe Qt QImage.""" try: # Create a ctypes array from the memory address data_ptr = (ctypes.c_ubyte * self.buffer_size).from_address(self.frame_buffer) # Create a NumPy array that views this memory (no copy yet) arr = np.frombuffer(data_ptr, dtype=np.uint8).reshape( (frame_head.iHeight, frame_head.iWidth, 3) ) # Create a QImage from the NumPy array. # .copy() is CRITICAL here to make a deep copy of the data. # Otherwise, the buffer could be overwritten by the SDK while Qt is drawing. return QImage( arr.data, frame_head.iWidth, frame_head.iHeight, 3 * frame_head.iWidth, QImage.Format_RGB888 ).copy() except Exception as e: self.error_occurred.emit(self.config.cam_id, f"Image Conversion Failed: {str(e)}") return None def _cleanup(self): """Releases all SDK resources for this camera instance.""" if self.h_cam: try: mvsdk.CameraStop(self.h_cam) mvsdk.CameraUnInit(self.h_cam) except Exception as e: print(f"Cleanup error (cam {self.config.cam_id+1}): {e}") self.h_cam = 0 if self.frame_buffer: mvsdk.CameraAlignFree(self.frame_buffer) self.frame_buffer = None self.finished.emit(self.config.cam_id) @pyqtSlot() def stop(self): """Public slot to safely stop the capture loop from the main thread.""" self._is_running = False # --- Main GUI Window --- class DualCameraWindow(QMainWindow): """Main application window with dual-camera display and controls.""" def __init__(self): super().__init__() self.setWindowTitle("Dual Camera MvSDK Controller") self.setGeometry(100, 100, 1400, 700) self.camera_count = 2 self.threads = {} self.workers = {} self.is_capturing = False # --- UI Setup --- central = QWidget() self.setCentralWidget(central) layout = QVBoxLayout(central) # Video Displays self.displays = [QLabel(f"Camera {i+1}\n(Stopped)", alignment=Qt.AlignCenter) for i in range(self.camera_count)] display_layout = QHBoxLayout() for display in self.displays: display.setStyleSheet("border: 1px solid #555; background: #eee; min-width: 640px; min-height: 480px;") display_layout.addWidget(display) layout.addLayout(display_layout) # Control Panel controls = QHBoxLayout() self.res_combo = QComboBox() self.fps_combo = QComboBox() self.start_btn = QPushButton("START") controls.addWidget(QLabel("Resolution:")) controls.addWidget(self.res_combo) controls.addWidget(QLabel("FPS:")) controls.addWidget(self.fps_combo) controls.addWidget(self.start_btn) layout.addLayout(controls) # --- Initial State & Connections --- self.fps_combo.addItems(["3", "5"]) self.start_btn.clicked.connect(self.toggle_capture) # Populate resolutions on startup for better UX self._populate_resolutions_on_startup() def _populate_resolutions_on_startup(self): """Briefly connect to camera 1 to get its supported resolutions.""" try: dev_list = mvsdk.CameraEnumerateDevice() if not dev_list: self.res_combo.addItem("No Cameras Found") self.res_combo.setEnabled(False) return h_cam = mvsdk.CameraInit(dev_list[0], -1, -1) cap = mvsdk.CameraGetCapability(h_cam) resolutions = self._get_startup_resolutions(cap) mvsdk.CameraUnInit(h_cam) # Immediately release the camera sorted_res = sorted(resolutions, key=lambda r: -r.width) for res in sorted_res: self.res_combo.addItem(str(res)) idx = self.res_combo.findText("1600x1200") if idx != -1: self.res_combo.setCurrentIndex(idx) except Exception as e: self.res_combo.addItem("Error getting resolutions") print(f"Startup resolution check failed: {e}") def _get_startup_resolutions(self, cap) -> list[ResolutionInfo]: """Helper for startup check, mirrors worker logic.""" resolutions = [] for res_struct in cap.pResolutionInfo: if res_struct.iWidth > 0: resolutions.append(ResolutionInfo(res_struct.iWidth, res_struct.iHeight)) return resolutions def toggle_capture(self): if self.is_capturing: self.stop_capture() else: self.start_capture() def start_capture(self): if self.is_capturing: return try: target_fps = int(self.fps_combo.currentText()) res_text = self.res_combo.currentText().split('x') target_res = ResolutionInfo(int(res_text[0]), int(res_text[1])) except (ValueError, IndexError): QMessageBox.critical(self, "Configuration Error", "Invalid resolution or FPS selected.") return for cam_id in range(self.camera_count): config = CameraConfig(cam_id, target_fps, target_res) self._start_worker(config) self.is_capturing = True self.start_btn.setText("STOP") self._set_controls_enabled(False) def _start_worker(self, config: CameraConfig): cam_id = config.cam_id thread = QThread() worker = CameraWorker(config) worker.moveToThread(thread) thread.started.connect(worker.run_capture) # Note: We don't need to connect resolutions_available here since we do it on startup. worker.image_updated.connect(self._update_display) worker.error_occurred.connect(self._handle_error) worker.finished.connect(thread.quit) worker.finished.connect(worker.deleteLater) thread.finished.connect(thread.deleteLater) thread.finished.connect(lambda cid=cam_id: self._on_thread_finish(cid)) self.threads[cam_id] = thread self.workers[cam_id] = worker thread.start() def stop_capture(self): for worker in self.workers.values(): worker.stop() for thread in self.threads.values(): thread.wait(2000) self._reset_ui_state() @pyqtSlot(int, QImage) def _update_display(self, cam_id: int, image: QImage): pixmap = QPixmap.fromImage(image).scaled(self.displays[cam_id].size(), Qt.KeepAspectRatio, Qt.SmoothTransformation) self.displays[cam_id].setPixmap(pixmap) @pyqtSlot(int, str) def _handle_error(self, cam_id: int, error: str): QMessageBox.critical(self, f"Camera {cam_id+1} Error", error) if cam_id in self.workers: self.workers[cam_id].stop() # Stop the specific worker def _on_thread_finish(self, cam_id: int): self.threads.pop(cam_id, None) self.workers.pop(cam_id, None) if not self.threads: # If the last camera thread has finished self._reset_ui_state() def _reset_ui_state(self): self.is_capturing = False self.start_btn.setText("START") self._set_controls_enabled(True) for i, display in enumerate(self.displays): display.setText(f"Camera {i+1}\n(Stopped)") def _set_controls_enabled(self, enabled: bool): self.res_combo.setEnabled(enabled) self.fps_combo.setEnabled(enabled) def closeEvent(self, event): self.stop_capture() event.accept() # --- Application Entry Point --- if __name__ == "__main__": app = QApplication(sys.argv) window = DualCameraWindow() window.show() sys.exit(app.exec_()) - Correct Resolution Handling: You correctly get the list of resolutions from the camera's capabilities and then find the index to pass to
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.