feat: Add detection count tracking and display in the UI
This commit is contained in:
@@ -42,6 +42,7 @@ class ResultPacket(NamedTuple):
|
||||
detections: list # list of (x1, y1, x2, y2, conf, label) tuples
|
||||
width: int # source frame width (for overlay scaling)
|
||||
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:
|
||||
"""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
|
||||
|
||||
frame_np = np.frombuffer(packet.raw_bytes, dtype=np.uint8).reshape(
|
||||
@@ -153,7 +156,9 @@ def _infer(model, packet: FramePacket) -> ResultPacket:
|
||||
)
|
||||
|
||||
device = _select_device()
|
||||
t0 = time.perf_counter()
|
||||
results = model(frame_np, device=device, verbose=False)
|
||||
elapsed_ms = (time.perf_counter() - t0) * 1000.0
|
||||
|
||||
detections = []
|
||||
for r in results:
|
||||
@@ -180,6 +185,7 @@ def _infer(model, packet: FramePacket) -> ResultPacket:
|
||||
detections=detections,
|
||||
width=packet.width,
|
||||
height=packet.height,
|
||||
elapsed_ms=elapsed_ms,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -55,6 +55,7 @@ class InferenceManager(QObject):
|
||||
"""
|
||||
|
||||
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_stopped = Signal()
|
||||
inference_error = Signal(str)
|
||||
@@ -79,6 +80,9 @@ class InferenceManager(QObject):
|
||||
# Paused flag — inference can be suspended without stopping the process
|
||||
self._paused: bool = False
|
||||
|
||||
# Detection counter — frames on which at least one detection occurred
|
||||
self._detection_frame_count: int = 0
|
||||
|
||||
# QTimers (GUI thread)
|
||||
self._poll_timer = QTimer(self)
|
||||
self._poll_timer.setInterval(INFERENCE_POLL_INTERVAL_MS)
|
||||
@@ -104,6 +108,7 @@ class InferenceManager(QObject):
|
||||
self._model_path = model_path
|
||||
self._restart_count = 0
|
||||
self._paused = False
|
||||
self._detection_frame_count = 0
|
||||
self._start_worker()
|
||||
|
||||
def stop(self) -> None:
|
||||
@@ -199,7 +204,7 @@ class InferenceManager(QObject):
|
||||
try:
|
||||
self._input_queue.put_nowait(packet)
|
||||
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:
|
||||
logger.warning("InferenceManager: could not enqueue frame: %s", exc)
|
||||
|
||||
@@ -289,10 +294,20 @@ class InferenceManager(QObject):
|
||||
]
|
||||
source_size = (packet.width, packet.height)
|
||||
|
||||
logger.debug(
|
||||
"InferenceManager: frame %d → %d detections",
|
||||
packet.frame_id, len(detections),
|
||||
)
|
||||
if detections:
|
||||
self._detection_frame_count += 1
|
||||
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)
|
||||
|
||||
except Exception:
|
||||
|
||||
Reference in New Issue
Block a user