From 0d13a4c1f60b118316f84996990cc690e2a19cf9 Mon Sep 17 00:00:00 2001 From: Cyril Achard Date: Fri, 29 May 2026 15:08:22 +0200 Subject: [PATCH 1/5] Split multi-camera frame signals; throttle display Introduce a separate, throttled UI/display path for multi-camera frames. Add GUI_MAX_DISPLAY_FPS config and a new display_ready signal that is emitted at most at that rate, while frame_ready remains full-rate for recording/inference. Implement _should_emit_display_ready, wire the MultiCameraController to emit both signals, and reset throttling state when starting. Update the main window to handle processing and display paths via _on_multi_frame_processing_ready and _on_multi_frame_display_ready, and adjust tests accordingly. Also add GUI/debug timing config keys and a temporary timing-only early path in SingleCameraWorker (marked as FIXME). --- dlclivegui/config.py | 12 ++++- dlclivegui/gui/main_window.py | 15 ++++-- .../services/multi_camera_controller.py | 48 +++++++++++++++++-- tests/gui/test_pose_overlay.py | 6 +-- 4 files changed, 67 insertions(+), 14 deletions(-) diff --git a/dlclivegui/config.py b/dlclivegui/config.py index 2d17622..936a742 100644 --- a/dlclivegui/config.py +++ b/dlclivegui/config.py @@ -17,8 +17,16 @@ TriggerStrobePolarity = Literal["ActiveHigh", "ActiveLow"] TriggerStrobeOperation = Literal["Exposure", "FixedDuration"] -SINGLE_CAMERA_WORKER_DO_LOG_TIMING = False -MULTI_CAMERA_WORKER_DO_LOG_TIMING = True +# Global settings +## GUI +GUI_MAX_DISPLAY_FPS: float = 30.0 + + +## Debug +### Timing logs +SINGLE_CAMERA_WORKER_DO_LOG_TIMING: bool = True +MULTI_CAMERA_WORKER_DO_LOG_TIMING: bool = True +MAIN_WINDOW_DO_LOG_TIMING: bool = False class CameraSettings(BaseModel): diff --git a/dlclivegui/gui/main_window.py b/dlclivegui/gui/main_window.py index 6fddba6..69061e9 100644 --- a/dlclivegui/gui/main_window.py +++ b/dlclivegui/gui/main_window.py @@ -772,7 +772,8 @@ def _connect_signals(self) -> None: self.bbox_color_combo.currentIndexChanged.connect(self._on_bbox_color_changed) # Multi-camera controller signals (used for both single and multi-camera modes) - self.multi_camera_controller.frame_ready.connect(self._on_multi_frame_ready) + self.multi_camera_controller.frame_ready.connect(self._on_multi_frame_processing_ready) + self.multi_camera_controller.frame_ready.connect(self._on_multi_frame_display_ready) self.multi_camera_controller.all_started.connect(self._on_multi_camera_started) self.multi_camera_controller.all_stopped.connect(self._on_multi_camera_stopped) self.multi_camera_controller.camera_error.connect(self._on_multi_camera_error) @@ -1374,13 +1375,12 @@ def _render_overlays_for_recording(self, cam_id, frame): ) return output - def _on_multi_frame_ready(self, frame_data: MultiFrameData) -> None: + def _on_multi_frame_processing_ready(self, frame_data: MultiFrameData) -> None: """Handle frames from multiple cameras. - Priority order for performance: + Priority: 1. DLC processing (highest priority - enqueue immediately, only for DLC camera) 2. Recording (queued writes, non-blocking) - 3. Display (lowest priority - tiled and updated on separate timer) """ self._multi_camera_frames = frame_data.frames src_id = frame_data.source_camera_id @@ -1437,7 +1437,12 @@ def _on_multi_frame_ready(self, frame_data: MultiFrameData) -> None: ts = frame_data.timestamps.get(src_id, time.time()) self._rec_manager.write_frame(src_id, frame, ts) - # PRIORITY 3: Mark display dirty (tiling done in display timer) + def _on_multi_frame_display_ready(self, frame_data: MultiFrameData) -> None: + """Throttled UI/display path. + + Called at GUI_MAX_DISPLAY_FPS, not at camera capture FPS for performance reasons. + """ + self._multi_camera_frames = frame_data.frames self._display_dirty = True def _on_multi_camera_started(self) -> None: diff --git a/dlclivegui/services/multi_camera_controller.py b/dlclivegui/services/multi_camera_controller.py index c88fbda..dc4024b 100644 --- a/dlclivegui/services/multi_camera_controller.py +++ b/dlclivegui/services/multi_camera_controller.py @@ -4,6 +4,7 @@ import copy import logging +import time from dataclasses import dataclass from functools import partial from threading import Event, Lock @@ -18,7 +19,12 @@ from dlclivegui.cameras.factory import camera_identity_key # from dlclivegui.config import CameraSettings -from dlclivegui.config import MULTI_CAMERA_WORKER_DO_LOG_TIMING, SINGLE_CAMERA_WORKER_DO_LOG_TIMING, CameraSettings +from dlclivegui.config import ( + GUI_MAX_DISPLAY_FPS, + MULTI_CAMERA_WORKER_DO_LOG_TIMING, + SINGLE_CAMERA_WORKER_DO_LOG_TIMING, + CameraSettings, +) from dlclivegui.utils.stats import WorkerTimingStats LOGGER = logging.getLogger(__name__) @@ -110,6 +116,10 @@ def run(self) -> None: continue consecutive_errors = 0 + # if True: # TEMP FIXME REMOVE + # self._timing.note_frame() + # self._timing.maybe_log() + # continue with self._timing.measure("Single.emit.frame_captured"): self.frame_captured.emit(self._camera_id, frame, timestamp) @@ -235,7 +245,8 @@ class MultiCameraController(QObject): """Controller for managing multiple cameras simultaneously.""" # Signals - frame_ready = Signal(object) # MultiFrameData + frame_ready = Signal(object) # MultiFrameData (full cam FPS; recording and inference only) + display_ready = Signal(object) # MultiFrameData for GUI display (throttled to GUI_MAX_DISPLAY_FPS) camera_started = Signal(str, object) # camera_id, settings camera_stopped = Signal(str) # camera_id camera_error = Signal(str, str) # camera_id, error_message @@ -260,6 +271,9 @@ def __init__(self): self._failed_cameras: dict[str, str] = {} # camera_id -> error message self._expected_cameras: int = 0 # Number of cameras we're trying to start + # GUI display max FPS (for throttling display updates when many cameras are active) + self._gui_display_max_fps: float = GUI_MAX_DISPLAY_FPS + self._gui_display_last_emit: float = 0.0 # Performance logs self._timing_per_cam: dict[str, WorkerTimingStats] = {} @@ -272,8 +286,6 @@ def get_active_count(self) -> int: return len(self._started_cameras) def _timing_for_camera(self, camera_id: str) -> WorkerTimingStats: - if not MULTI_CAMERA_WORKER_DO_LOG_TIMING: - return WorkerTimingStats(camera_id, enabled=False) timing = self._timing_per_cam.get(camera_id) if timing is None: timing = WorkerTimingStats( @@ -285,6 +297,24 @@ def _timing_for_camera(self, camera_id: str) -> WorkerTimingStats: self._timing_per_cam[camera_id] = timing return timing + def _should_emit_display_ready(self) -> bool: + """Return True when the UI/display path should be updated. + + This only throttles display_ready. It must not throttle frame_ready, + because frame_ready is used for full-rate consumers such as recording. + """ + if self._gui_display_max_fps <= 0: + return True + + now = time.perf_counter() + min_interval = 1.0 / max(self._gui_display_max_fps, 1e-9) + + if now - self._gui_display_last_emit < min_interval: + return False + + self._gui_display_last_emit = now + return True + def start(self, camera_settings: list[CameraSettings]) -> None: """Start multiple cameras.""" if self._running: @@ -456,6 +486,11 @@ def stop(self, wait: bool = True) -> None: self.all_stopped.emit() return + self._timing_per_cam.clear() + self._gui_display_last_emit = 0.0 + self._workers.clear() + self._threads.clear() + self._settings.clear() self._started_cameras.clear() self._failed_cameras.clear() self._camera_display_order.clear() @@ -521,6 +556,11 @@ def _on_frame_captured(self, camera_id: str, frame: np.ndarray, timestamp: float with timing.measure("Multi.emit.frame_ready"): self.frame_ready.emit(frame_data) + # GUI-only path: throttled display updates + if self._should_emit_display_ready(): + with timing.measure("Multi.emit.display_ready"): + self.display_ready.emit(frame_data) + timing.note_frame() timing.maybe_log() diff --git a/tests/gui/test_pose_overlay.py b/tests/gui/test_pose_overlay.py index 369baf8..3af3530 100644 --- a/tests/gui/test_pose_overlay.py +++ b/tests/gui/test_pose_overlay.py @@ -65,7 +65,7 @@ def test_record_overlay_toggle_affects_frames_sent_to_recorder(window, recording # Provide a frame raw = np.zeros((100, 100, 3), dtype=np.uint8) - # Build minimal frame_data to call _on_multi_frame_ready + # Build minimal frame_data to call _on_multi_frame_processing_ready from dlclivegui.services.multi_camera_controller import MultiFrameData frame_data = MultiFrameData( @@ -76,7 +76,7 @@ def test_record_overlay_toggle_affects_frames_sent_to_recorder(window, recording # 1) toggle OFF: should record raw window.record_with_overlays_checkbox.setChecked(False) - window._on_multi_frame_ready(frame_data) + window._on_multi_frame_processing_ready(frame_data) assert cam_id in recording_frame_spy recorded_off = recording_frame_spy[cam_id] @@ -84,7 +84,7 @@ def test_record_overlay_toggle_affects_frames_sent_to_recorder(window, recording # 2) toggle ON: should record overlay frame (different) window.record_with_overlays_checkbox.setChecked(True) - window._on_multi_frame_ready(frame_data) + window._on_multi_frame_processing_ready(frame_data) recorded_on = recording_frame_spy[cam_id] assert not np.array_equal(recorded_on, raw) From 126c1c3de10508492ec352b410c949cadda88824 Mon Sep 17 00:00:00 2001 From: Cyril Achard Date: Fri, 29 May 2026 15:25:39 +0200 Subject: [PATCH 2/5] Add GenTL telemetry and frame-rate debugging Add helpers and telemetry to better read and debug GenTL/GenICam cameras. - New debug helper _debug_frame_rate_nodes to log many common frame-rate/exposure/throughput nodes. - Added robust node accessors: _node_value, _node_float, _node_str to safely read various GenICam node types and try multiple node names. - Enhance frame-rate configuration to log before/after values when enabling rate control and when setting AcquisitionFrameRate/AcquisitionFrameRateAbs; record any accepted frame-rate as _actual_fps. - After starting acquisition, attempt to read telemetry and run FPS debug logging; warn (but continue) if telemetry read fails. - Improve _read_telemetry to prefer resulting frame-rate nodes over requested ones, and to populate actual_fps, actual_exposure, actual_gain and other useful properties (pixel format, throughput, resolution, etc.) into the settings namespace for GUI/debugging. These changes make frame-rate/exposure behavior more observable and more tolerant across different camera implementations. --- dlclivegui/cameras/backends/gentl_backend.py | 168 +++++++++++++++++-- 1 file changed, 155 insertions(+), 13 deletions(-) diff --git a/dlclivegui/cameras/backends/gentl_backend.py b/dlclivegui/cameras/backends/gentl_backend.py index 260c55a..01c495f 100644 --- a/dlclivegui/cameras/backends/gentl_backend.py +++ b/dlclivegui/cameras/backends/gentl_backend.py @@ -558,6 +558,15 @@ def open(self) -> None: self._acquirer.start() + try: + self._read_telemetry(node_map) + self._debug_frame_rate_nodes(node_map, context="after starting acquisition") + except Exception: + LOG.warning( + "Failed to read telemetry after starting acquisition; some 'actual' values may be missing.", + exc_info=True, + ) + LOG.debug( "Opened GenTL camera index=%s serial=%s label=%s", selected_index, @@ -1126,6 +1135,48 @@ def _node_symbolics(node) -> list[str]: except Exception: return [] + @staticmethod + def _node_value(node_map, name: str, default=None): + """Best-effort read of a GenICam node value.""" + try: + node = getattr(node_map, name) + except Exception: + return default + + try: + return node.value + except Exception: + return default + + @classmethod + def _node_float(cls, node_map, *names: str) -> float | None: + """Return the first positive float value from a list of GenICam node names.""" + for name in names: + value = cls._node_value(node_map, name, None) + try: + fvalue = float(value) + except Exception: + continue + + if fvalue > 0: + return fvalue + + return None + + @classmethod + def _node_str(cls, node_map, *names: str) -> str | None: + """Return the first non-empty string value from a list of GenICam node names.""" + for name in names: + value = cls._node_value(node_map, name, None) + if value is None: + continue + + text = str(value).strip() + if text: + return text + + return None + def _set_enum_node(self, node_map, name: str, value: str, *, strict: bool = False) -> bool: node = self._node(node_map, name) if node is None: @@ -1605,21 +1656,48 @@ def _configure_frame_rate(self, node_map) -> None: return target = float(self.settings.fps) + LOG.info("Configuring GenTL frame rate: requested %.3f FPS", target) + for attr in ("AcquisitionFrameRateEnable", "AcquisitionFrameRateControlEnable"): try: - getattr(node_map, attr).value = True + node = getattr(node_map, attr) + before = getattr(node, "value", None) + node.value = True + after = getattr(node, "value", None) + LOG.info("Enabled GenTL %s: before=%r after=%r", attr, before, after) break except Exception: pass - for attr in ("AcquisitionFrameRate", "ResultingFrameRate", "AcquisitionFrameRateAbs"): + for attr in ("AcquisitionFrameRate", "AcquisitionFrameRateAbs"): try: - getattr(node_map, attr).value = target + node = getattr(node_map, attr) + before = getattr(node, "value", None) + node.value = target + after = getattr(node, "value", None) + + LOG.info( + "Set GenTL %s: before=%r requested=%.3f after=%r", + attr, + before, + target, + after, + ) + + try: + accepted = float(after) + if accepted > 0: + self._actual_fps = accepted + except Exception: + pass + return + except AttributeError: continue except Exception as e: LOG.warning("Failed to set frame rate via %s: %s", attr, e) + LOG.warning("Could not set frame rate to %s FPS", target) def _read_telemetry(self, node_map) -> None: @@ -1629,20 +1707,84 @@ def _read_telemetry(self, node_map) -> None: except Exception: pass - try: - self._actual_fps = float(node_map.ResultingFrameRate.value) - except Exception: - self._actual_fps = None + # Prefer true/resulting frame-rate readback nodes. + resulting_fps = self._node_float( + node_map, + "AcquisitionResultingFrameRate", + "ResultingFrameRate", + "AcquisitionFrameRateResulting", + "DeviceFrameRate", + ) - try: - self._actual_exposure = float(node_map.ExposureTime.value) - except Exception: - self._actual_exposure = None + # Fallback to requested/accepted frame-rate nodes only if no resulting node exists. + requested_fps = self._node_float( + node_map, + "AcquisitionFrameRate", + "AcquisitionFrameRateAbs", + ) + + if resulting_fps is not None: + self._actual_fps = resulting_fps + elif requested_fps is not None: + self._actual_fps = requested_fps + exposure = self._node_float( + node_map, + "ExposureTime", + "ExposureTimeAbs", + "Exposure", + ) + if exposure is not None: + self._actual_exposure = exposure + + gain = self._node_float( + node_map, + "Gain", + "GainRaw", + ) + if gain is not None: + self._actual_gain = gain + + # Persist useful telemetry into properties["gentl"] for GUI/debugging. try: - self._actual_gain = float(node_map.Gain.value) + ns = self._ensure_settings_ns() + + if self._actual_width and self._actual_height: + ns["actual_resolution"] = [int(self._actual_width), int(self._actual_height)] + + if self._actual_fps is not None: + ns["actual_fps"] = float(self._actual_fps) + + if resulting_fps is not None: + ns["actual_resulting_frame_rate"] = float(resulting_fps) + + if requested_fps is not None: + ns["actual_acquisition_frame_rate"] = float(requested_fps) + + if self._actual_exposure is not None: + ns["actual_exposure"] = float(self._actual_exposure) + + if self._actual_gain is not None: + ns["actual_gain"] = float(self._actual_gain) + + exposure_auto = self._node_str(node_map, "ExposureAuto") + if exposure_auto is not None: + ns["actual_exposure_auto"] = exposure_auto + + throughput = self._node_float(node_map, "DeviceLinkThroughputLimit") + if throughput is not None: + ns["actual_device_link_throughput_limit"] = float(throughput) + + throughput_mode = self._node_str(node_map, "DeviceLinkThroughputLimitMode") + if throughput_mode is not None: + ns["actual_device_link_throughput_limit_mode"] = throughput_mode + + pixel_format = self._node_str(node_map, "PixelFormat") + if pixel_format is not None: + ns["actual_pixel_format"] = pixel_format + except Exception: - self._actual_gain = None + pass # ------------------------------------------------------------------ # Frame conversion / local helpers From 0e7710fab5b7312bf8f6573e34b2b47b0aa8e4d5 Mon Sep 17 00:00:00 2001 From: Cyril Achard Date: Fri, 29 May 2026 17:13:56 +0200 Subject: [PATCH 3/5] Remove temp debug block --- dlclivegui/services/multi_camera_controller.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/dlclivegui/services/multi_camera_controller.py b/dlclivegui/services/multi_camera_controller.py index dc4024b..ee3061f 100644 --- a/dlclivegui/services/multi_camera_controller.py +++ b/dlclivegui/services/multi_camera_controller.py @@ -116,10 +116,6 @@ def run(self) -> None: continue consecutive_errors = 0 - # if True: # TEMP FIXME REMOVE - # self._timing.note_frame() - # self._timing.maybe_log() - # continue with self._timing.measure("Single.emit.frame_captured"): self.frame_captured.emit(self._camera_id, frame, timestamp) From 4f09b21770eb7a48d3c4ba73597333aa348529be Mon Sep 17 00:00:00 2001 From: Cyril Achard Date: Fri, 29 May 2026 17:26:52 +0200 Subject: [PATCH 4/5] Fix missing signal wiring Fix signal hookup and disable main-window timing flag. Replace the second connection of multi_camera_controller.frame_ready to _on_multi_frame_display_ready with multi_camera_controller.display_ready to separate processing-ready and display-ready events. Also comment out MAIN_WINDOW_DO_LOG_TIMING in config.py to disable main-window timing logging. --- dlclivegui/config.py | 2 +- dlclivegui/gui/main_window.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dlclivegui/config.py b/dlclivegui/config.py index 936a742..dac523d 100644 --- a/dlclivegui/config.py +++ b/dlclivegui/config.py @@ -26,7 +26,7 @@ ### Timing logs SINGLE_CAMERA_WORKER_DO_LOG_TIMING: bool = True MULTI_CAMERA_WORKER_DO_LOG_TIMING: bool = True -MAIN_WINDOW_DO_LOG_TIMING: bool = False +# MAIN_WINDOW_DO_LOG_TIMING: bool = False class CameraSettings(BaseModel): diff --git a/dlclivegui/gui/main_window.py b/dlclivegui/gui/main_window.py index 69061e9..ef06a5d 100644 --- a/dlclivegui/gui/main_window.py +++ b/dlclivegui/gui/main_window.py @@ -773,7 +773,7 @@ def _connect_signals(self) -> None: # Multi-camera controller signals (used for both single and multi-camera modes) self.multi_camera_controller.frame_ready.connect(self._on_multi_frame_processing_ready) - self.multi_camera_controller.frame_ready.connect(self._on_multi_frame_display_ready) + self.multi_camera_controller.display_ready.connect(self._on_multi_frame_display_ready) self.multi_camera_controller.all_started.connect(self._on_multi_camera_started) self.multi_camera_controller.all_stopped.connect(self._on_multi_camera_stopped) self.multi_camera_controller.camera_error.connect(self._on_multi_camera_error) From fb1b2ed2fe8d56ee3a3d8c8d954cd5e45119a1c7 Mon Sep 17 00:00:00 2001 From: Cyril Achard Date: Mon, 1 Jun 2026 16:21:36 +0200 Subject: [PATCH 5/5] Allow zero node values and reduce log level Change LOG.info to LOG.debug to reduce noise when printing node information. Add an allow_zero parameter to _node_float so callers can accept zero as a valid value (previously only positive values were returned). Update callers for exposure, gain, and DeviceLinkThroughputLimit to pass allow_zero=True so reported zeros are treated as real readings while preserving the original behavior by default. --- dlclivegui/cameras/backends/gentl_backend.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dlclivegui/cameras/backends/gentl_backend.py b/dlclivegui/cameras/backends/gentl_backend.py index 01c495f..26d5c2f 100644 --- a/dlclivegui/cameras/backends/gentl_backend.py +++ b/dlclivegui/cameras/backends/gentl_backend.py @@ -1149,7 +1149,7 @@ def _node_value(node_map, name: str, default=None): return default @classmethod - def _node_float(cls, node_map, *names: str) -> float | None: + def _node_float(cls, node_map, *names: str, allow_zero: bool = False) -> float | None: """Return the first positive float value from a list of GenICam node names.""" for name in names: value = cls._node_value(node_map, name, None) @@ -1158,7 +1158,7 @@ def _node_float(cls, node_map, *names: str) -> float | None: except Exception: continue - if fvalue > 0: + if fvalue > 0 or (allow_zero and fvalue == 0): return fvalue return None @@ -1733,6 +1733,7 @@ def _read_telemetry(self, node_map) -> None: "ExposureTime", "ExposureTimeAbs", "Exposure", + allow_zero=True, ) if exposure is not None: self._actual_exposure = exposure @@ -1741,6 +1742,7 @@ def _read_telemetry(self, node_map) -> None: node_map, "Gain", "GainRaw", + allow_zero=True, ) if gain is not None: self._actual_gain = gain @@ -1771,7 +1773,7 @@ def _read_telemetry(self, node_map) -> None: if exposure_auto is not None: ns["actual_exposure_auto"] = exposure_auto - throughput = self._node_float(node_map, "DeviceLinkThroughputLimit") + throughput = self._node_float(node_map, "DeviceLinkThroughputLimit", allow_zero=True) if throughput is not None: ns["actual_device_link_throughput_limit"] = float(throughput)