Add FPS display feature and update configuration for display settings
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
@@ -38,6 +39,9 @@ class MainWindow(QMainWindow):
|
||||
self.last_detection: DetectionResult | None = None
|
||||
self.detecting = False
|
||||
self.detection_frame_count = 0
|
||||
self.fps_frame_count = 0
|
||||
self.fps_last_time = time.monotonic()
|
||||
self.display_fps = 0.0
|
||||
self.media_store = MediaStore(self.config, self.app_config)
|
||||
self.video_recorder = VideoRecorder(self.config, self.app_config)
|
||||
|
||||
@@ -162,6 +166,7 @@ class MainWindow(QMainWindow):
|
||||
|
||||
@Slot(object)
|
||||
def on_frame_ready(self, frame: np.ndarray) -> None:
|
||||
self._update_fps()
|
||||
self.last_frame = frame.copy()
|
||||
if self.video_recorder.is_recording:
|
||||
self.video_recorder.write(frame)
|
||||
@@ -240,6 +245,17 @@ class MainWindow(QMainWindow):
|
||||
|
||||
self.detection_worker.request_detection(frame)
|
||||
|
||||
def _update_fps(self) -> None:
|
||||
self.fps_frame_count += 1
|
||||
now = time.monotonic()
|
||||
elapsed = now - self.fps_last_time
|
||||
if elapsed < 1.0:
|
||||
return
|
||||
|
||||
self.display_fps = self.fps_frame_count / elapsed
|
||||
self.fps_frame_count = 0
|
||||
self.fps_last_time = now
|
||||
|
||||
def current_metadata(self, media_type: str) -> dict[str, Any]:
|
||||
return {
|
||||
"media_type": media_type,
|
||||
@@ -274,6 +290,8 @@ class MainWindow(QMainWindow):
|
||||
display_frame = frame_bgr.copy()
|
||||
if self.overlay_result is not None:
|
||||
self._draw_detection(display_frame, self.overlay_result)
|
||||
if self.config.get("display", {}).get("show_fps", True):
|
||||
self._draw_fps(display_frame)
|
||||
|
||||
frame_rgb = cv2.cvtColor(display_frame, cv2.COLOR_BGR2RGB)
|
||||
h, w, channels = frame_rgb.shape
|
||||
@@ -306,6 +324,20 @@ class MainWindow(QMainWindow):
|
||||
cv2.LINE_AA,
|
||||
)
|
||||
|
||||
def _draw_fps(self, frame_bgr: np.ndarray) -> None:
|
||||
label = f"FPS: {self.display_fps:.1f}"
|
||||
cv2.rectangle(frame_bgr, (12, 12), (122, 46), (0, 0, 0), -1)
|
||||
cv2.putText(
|
||||
frame_bgr,
|
||||
label,
|
||||
(20, 36),
|
||||
cv2.FONT_HERSHEY_SIMPLEX,
|
||||
0.7,
|
||||
(255, 255, 255),
|
||||
2,
|
||||
cv2.LINE_AA,
|
||||
)
|
||||
|
||||
|
||||
def run_app(app_config: AppConfig) -> int:
|
||||
app = QApplication([])
|
||||
|
||||
Reference in New Issue
Block a user