feat: Add detection count tracking and display in the UI

This commit is contained in:
2026-05-13 22:08:13 +02:00
parent 3b8f7eb3d4
commit 83346dc985
3 changed files with 40 additions and 7 deletions

View File

@@ -42,6 +42,7 @@ class ResultPacket(NamedTuple):
detections: list # list of (x1, y1, x2, y2, conf, label) tuples detections: list # list of (x1, y1, x2, y2, conf, label) tuples
width: int # source frame width (for overlay scaling) width: int # source frame width (for overlay scaling)
height: int # source frame height height: int # source frame height
elapsed_ms: float = 0.0 # inference wall-clock time in milliseconds
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
@@ -145,7 +146,9 @@ def _select_device() -> str:
def _infer(model, packet: FramePacket) -> ResultPacket: def _infer(model, packet: FramePacket) -> ResultPacket:
"""Run model on one frame, return ResultPacket.""" """Run model on one frame, return ResultPacket with elapsed_ms."""
import time # noqa: PLC0415
import numpy as np # noqa: PLC0415 import numpy as np # noqa: PLC0415
frame_np = np.frombuffer(packet.raw_bytes, dtype=np.uint8).reshape( frame_np = np.frombuffer(packet.raw_bytes, dtype=np.uint8).reshape(
@@ -153,7 +156,9 @@ def _infer(model, packet: FramePacket) -> ResultPacket:
) )
device = _select_device() device = _select_device()
t0 = time.perf_counter()
results = model(frame_np, device=device, verbose=False) results = model(frame_np, device=device, verbose=False)
elapsed_ms = (time.perf_counter() - t0) * 1000.0
detections = [] detections = []
for r in results: for r in results:
@@ -180,6 +185,7 @@ def _infer(model, packet: FramePacket) -> ResultPacket:
detections=detections, detections=detections,
width=packet.width, width=packet.width,
height=packet.height, height=packet.height,
elapsed_ms=elapsed_ms,
) )

View File

@@ -55,6 +55,7 @@ class InferenceManager(QObject):
""" """
detections_ready = Signal(object, object) # list[Detection], tuple[int,int] detections_ready = Signal(object, object) # list[Detection], tuple[int,int]
detection_count_updated = Signal(int) # total frames with detections so far
inference_started = Signal() inference_started = Signal()
inference_stopped = Signal() inference_stopped = Signal()
inference_error = Signal(str) inference_error = Signal(str)
@@ -79,6 +80,9 @@ class InferenceManager(QObject):
# Paused flag — inference can be suspended without stopping the process # Paused flag — inference can be suspended without stopping the process
self._paused: bool = False self._paused: bool = False
# Detection counter — frames on which at least one detection occurred
self._detection_frame_count: int = 0
# QTimers (GUI thread) # QTimers (GUI thread)
self._poll_timer = QTimer(self) self._poll_timer = QTimer(self)
self._poll_timer.setInterval(INFERENCE_POLL_INTERVAL_MS) self._poll_timer.setInterval(INFERENCE_POLL_INTERVAL_MS)
@@ -104,6 +108,7 @@ class InferenceManager(QObject):
self._model_path = model_path self._model_path = model_path
self._restart_count = 0 self._restart_count = 0
self._paused = False self._paused = False
self._detection_frame_count = 0
self._start_worker() self._start_worker()
def stop(self) -> None: def stop(self) -> None:
@@ -199,7 +204,7 @@ class InferenceManager(QObject):
try: try:
self._input_queue.put_nowait(packet) self._input_queue.put_nowait(packet)
self._busy = True self._busy = True
logger.debug("InferenceManager: submitted frame %d", self._frame_id) # logger.debug("InferenceManager: submitted frame %d", self._frame_id)
except Exception as exc: except Exception as exc:
logger.warning("InferenceManager: could not enqueue frame: %s", exc) logger.warning("InferenceManager: could not enqueue frame: %s", exc)
@@ -289,10 +294,20 @@ class InferenceManager(QObject):
] ]
source_size = (packet.width, packet.height) source_size = (packet.width, packet.height)
logger.debug( if detections:
"InferenceManager: frame %d%d detections", self._detection_frame_count += 1
packet.frame_id, len(detections), conf_summary = ", ".join(
) f"{d.label} {d.conf:.2f}" for d in detections
)
logger.info(
"frame %d: %d detection(s) in %.1f ms — %s",
packet.frame_id,
len(detections),
packet.elapsed_ms,
conf_summary,
)
self.detection_count_updated.emit(self._detection_frame_count)
self.detections_ready.emit(detections, source_size) self.detections_ready.emit(detections, source_size)
except Exception: except Exception:

View File

@@ -105,7 +105,11 @@ class MainWindow(QMainWindow):
self._status_bar = QStatusBar(self) self._status_bar = QStatusBar(self)
self.setStatusBar(self._status_bar) self.setStatusBar(self._status_bar)
self._status_label = QLabel("Initialising\u2026") self._status_label = QLabel("Initialising\u2026")
self._status_bar.addWidget(self._status_label) self._status_bar.addWidget(self._status_label, stretch=1)
# Detection counter — right-aligned permanent widget
self._detection_label = QLabel("")
self._detection_label.setVisible(False)
self._status_bar.addPermanentWidget(self._detection_label)
# --- Wire signals --- # --- Wire signals ---
self._wire_signals() self._wire_signals()
@@ -179,6 +183,7 @@ class MainWindow(QMainWindow):
# ---- InferenceManager ---- # ---- InferenceManager ----
self._inference.detections_ready.connect(self._bbox_overlay.on_detections) self._inference.detections_ready.connect(self._bbox_overlay.on_detections)
self._inference.detection_count_updated.connect(self._on_detection_count_updated)
self._inference.inference_started.connect(self._on_inference_started) self._inference.inference_started.connect(self._on_inference_started)
self._inference.inference_stopped.connect(self._on_inference_stopped) self._inference.inference_stopped.connect(self._on_inference_stopped)
self._inference.inference_error.connect(self._on_inference_error) self._inference.inference_error.connect(self._on_inference_error)
@@ -259,6 +264,9 @@ class MainWindow(QMainWindow):
self._status_label.setText("Inference running") self._status_label.setText("Inference running")
self._menu.set_inference_checked(True) self._menu.set_inference_checked(True)
def _on_detection_count_updated(self, count: int) -> None:
self._detection_label.setText(f"Detections: {count} frames")
def _on_inference_stopped(self) -> None: def _on_inference_stopped(self) -> None:
self._bbox_overlay.clear() self._bbox_overlay.clear()
@@ -267,6 +275,7 @@ class MainWindow(QMainWindow):
self._menu.set_inference_available(False) self._menu.set_inference_available(False)
self._menu.set_inference_checked(False) self._menu.set_inference_checked(False)
self._bbox_overlay.visible = False self._bbox_overlay.visible = False
self._detection_label.setVisible(False)
QMessageBox.critical(self, "Inference Error", message) QMessageBox.critical(self, "Inference Error", message)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
@@ -331,6 +340,8 @@ class MainWindow(QMainWindow):
self._inference.submit_frame, drop_if_busy=True self._inference.submit_frame, drop_if_busy=True
) )
self._bbox_overlay.visible = True self._bbox_overlay.visible = True
self._detection_label.setText("Detections: 0 frames")
self._detection_label.setVisible(True)
self._status_label.setText("Inference enabled") self._status_label.setText("Inference enabled")
logger.info("Inference enabled") logger.info("Inference enabled")
else: else:
@@ -338,6 +349,7 @@ class MainWindow(QMainWindow):
self._dispatcher.unsubscribe(self._inference.submit_frame) self._dispatcher.unsubscribe(self._inference.submit_frame)
self._bbox_overlay.clear() self._bbox_overlay.clear()
self._bbox_overlay.visible = False self._bbox_overlay.visible = False
self._detection_label.setVisible(False)
self._status_label.setText("Inference disabled") self._status_label.setText("Inference disabled")
logger.info("Inference disabled") logger.info("Inference disabled")