Refactor detection handling by separating DetectionWorker and integrating it into MainWindow
This commit is contained in:
@@ -21,7 +21,7 @@ from PySide6.QtWidgets import (
|
||||
QStyle,
|
||||
)
|
||||
|
||||
from app.camera import CameraWorker
|
||||
from app.camera import CameraWorker, DetectionWorker
|
||||
from app.config import AppConfig
|
||||
from app.detection import DetectionResult
|
||||
from app.media import MediaStore, VideoRecorder
|
||||
@@ -36,6 +36,8 @@ class MainWindow(QMainWindow):
|
||||
self.last_frame: np.ndarray | None = None
|
||||
self.overlay_result: DetectionResult | None = None
|
||||
self.last_detection: DetectionResult | None = None
|
||||
self.detecting = False
|
||||
self.detection_frame_count = 0
|
||||
self.media_store = MediaStore(self.config, self.app_config)
|
||||
self.video_recorder = VideoRecorder(self.config, self.app_config)
|
||||
|
||||
@@ -45,10 +47,13 @@ class MainWindow(QMainWindow):
|
||||
|
||||
self.worker = CameraWorker(self.config, self.app_config)
|
||||
self.worker.frame_ready.connect(self.on_frame_ready)
|
||||
self.worker.detection_ready.connect(self.on_detection_ready)
|
||||
self.worker.camera_error.connect(self.on_camera_error)
|
||||
self.worker.start()
|
||||
|
||||
self.detection_worker = DetectionWorker(self.config, self.app_config)
|
||||
self.detection_worker.detection_ready.connect(self.on_detection_ready)
|
||||
self.detection_worker.start()
|
||||
|
||||
def _build_ui(self) -> None:
|
||||
self.stage = QWidget()
|
||||
self.setCentralWidget(self.stage)
|
||||
@@ -150,7 +155,9 @@ class MainWindow(QMainWindow):
|
||||
if self.video_recorder.is_recording:
|
||||
self.video_recorder.stop(self.current_metadata("video"))
|
||||
self.worker.stop()
|
||||
self.detection_worker.stop()
|
||||
self.worker.wait(2000)
|
||||
self.detection_worker.wait(2000)
|
||||
super().closeEvent(event)
|
||||
|
||||
@Slot(object)
|
||||
@@ -158,10 +165,13 @@ class MainWindow(QMainWindow):
|
||||
self.last_frame = frame.copy()
|
||||
if self.video_recorder.is_recording:
|
||||
self.video_recorder.write(frame)
|
||||
self._maybe_request_detection(frame)
|
||||
self._show_frame(frame)
|
||||
|
||||
@Slot(object)
|
||||
def on_detection_ready(self, result: DetectionResult) -> None:
|
||||
if not self.detecting:
|
||||
return
|
||||
self.last_detection = result
|
||||
self.overlay_result = result if result.xyxy else None
|
||||
self._update_result_text(result)
|
||||
@@ -172,11 +182,12 @@ class MainWindow(QMainWindow):
|
||||
|
||||
def start_detection(self) -> None:
|
||||
self.overlay_result = None
|
||||
self.detecting = True
|
||||
self.detection_frame_count = 0
|
||||
self.result_text.setPlainText("Wykrywanie...")
|
||||
self.worker.start_detection()
|
||||
|
||||
def accept_detection(self) -> None:
|
||||
self.worker.accept_detection()
|
||||
self.detecting = False
|
||||
self.overlay_result = None
|
||||
if self.last_detection:
|
||||
self._update_result_text(self.last_detection, accepted=True)
|
||||
@@ -218,6 +229,17 @@ class MainWindow(QMainWindow):
|
||||
self.app_config.save(self.config)
|
||||
self.worker.update_camera_config(camera_config)
|
||||
|
||||
def _maybe_request_detection(self, frame: np.ndarray) -> None:
|
||||
if not self.detecting:
|
||||
return
|
||||
|
||||
frame_stride = max(1, int(self.config["detection"].get("frame_stride", 5)))
|
||||
self.detection_frame_count += 1
|
||||
if self.detection_frame_count % frame_stride != 0:
|
||||
return
|
||||
|
||||
self.detection_worker.request_detection(frame)
|
||||
|
||||
def current_metadata(self, media_type: str) -> dict[str, Any]:
|
||||
return {
|
||||
"media_type": media_type,
|
||||
|
||||
Reference in New Issue
Block a user