configura fps preview e yolo

This commit is contained in:
administrator
2026-05-16 09:10:48 +02:00
parent 16458d98e9
commit 1186c3bb35
4 changed files with 170 additions and 57 deletions

View File

@@ -59,11 +59,15 @@ class NavigationDemoEngine:
raise RuntimeError(f"impossibile aprire sorgente video: {self.source_name}")
video_fps = self.cap.get(cv2.CAP_PROP_FPS)
preview_fps = args.preview_fps if args.preview_fps and args.preview_fps > 0 else video_fps
if args.preview_fps and args.preview_fps > 0 and (args.video is None or str(args.video).isdigit()):
self.cap.set(cv2.CAP_PROP_FPS, float(args.preview_fps))
self.frame_delay = (
1.0 / video_fps
if args.realtime_playback and video_fps and video_fps > 1
1.0 / preview_fps
if args.realtime_playback and preview_fps and preview_fps > 1
else 0.0
)
self.yolo_interval = 1.0 / args.yolo_fps if args.yolo_fps and args.yolo_fps > 0 else 0.0
self.tracker = nav.LightweightTracker(
max_missed=args.max_track_missed,
min_match_score=args.min_match_score,
@@ -75,6 +79,10 @@ class NavigationDemoEngine:
self.last_loop_end = self.start_time
self.yolo_total_ms = 0.0
self.yolo_cycles = 0
self.next_yolo_time = self.start_time
self.last_yolo_ms = 0.0
self.gaylords: list[nav.Detection] = []
self.tracks: list[nav.Track] = []
self.stop_reason = ""
def close(self) -> None:
@@ -99,39 +107,42 @@ class NavigationDemoEngine:
self.stop_reason = f"Raggiunto max_frames={self.args.max_frames}"
return None
detections, yolo_ms = self.detector.detect(
frame,
self.args.min_confidence,
self.args.input_size,
)
self.yolo_total_ms += yolo_ms
self.yolo_cycles += 1
gaylords = [
det for det in detections
if det.class_name.strip().lower() == self.args.target_class.strip().lower()
]
tracks = self.tracker.update(gaylords, self.frame_id, frame.shape[1])
if (
self.args.motion_report_interval > 0
and self.frame_id % self.args.motion_report_interval == 0
):
self.navigator.set_motion_text(
nav.estimate_motion_from_tracks(tracks, self.args.motion_min_pixels)
)
new_snapshots: list[nav.NavigationSnapshot] = []
for track in tracks:
if track.missed == 0:
snapshot = self.navigator.process_track(
track,
frame,
self.frame_id,
timestamp,
run_yolo = self.yolo_interval <= 0 or timestamp >= self.next_yolo_time
if run_yolo:
self.next_yolo_time = timestamp + self.yolo_interval
detections, self.last_yolo_ms = self.detector.detect(
frame,
self.args.min_confidence,
self.args.input_size,
)
self.yolo_total_ms += self.last_yolo_ms
self.yolo_cycles += 1
self.gaylords = [
det for det in detections
if det.class_name.strip().lower() == self.args.target_class.strip().lower()
]
self.tracks = self.tracker.update(self.gaylords, self.frame_id, frame.shape[1])
if (
self.args.motion_report_interval > 0
and self.yolo_cycles % self.args.motion_report_interval == 0
):
self.navigator.set_motion_text(
nav.estimate_motion_from_tracks(self.tracks, self.args.motion_min_pixels)
)
if snapshot is not None:
new_snapshots.append(snapshot)
for track in self.tracks:
if track.missed == 0:
snapshot = self.navigator.process_track(
track,
frame,
self.frame_id,
timestamp,
)
if snapshot is not None:
new_snapshots.append(snapshot)
for snapshot in new_snapshots:
self.navigator.simulate_remote_response(snapshot)
@@ -139,12 +150,13 @@ class NavigationDemoEngine:
elapsed = max(time.perf_counter() - self.start_time, 0.001)
fps_text = (
f"frame={self.frame_id} fps={self.frame_id / elapsed:.1f} "
f"det={len(gaylords)} tracks={len(tracks)} "
f"yolo_fps={self.yolo_cycles / elapsed:.1f} yolo={self.last_yolo_ms:.0f}ms "
f"det={len(self.gaylords)} tracks={len(self.tracks)} "
f"snap={self.navigator.snapshot_counter}"
)
display = nav.draw_navigation_debug(
frame,
tracks,
self.tracks,
self.args,
self.navigator.last_command_text,
fps_text,
@@ -156,10 +168,10 @@ class NavigationDemoEngine:
source_frame=frame,
display_frame=display,
snapshot_frame=self.navigator.last_ocr_payload_frame,
tracks=tracks,
detections_count=len(gaylords),
tracks=self.tracks,
detections_count=len(self.gaylords),
snapshots=new_snapshots,
yolo_ms=yolo_ms,
yolo_ms=self.last_yolo_ms,
fps_text=fps_text,
)