9194ef5abd
Docs/Test Workflow / Test docs build (push) Failing after 0s
Check links & references / links-check (push) Failing after 1s
Pytest/Test Workflow / Import Test and Pytest Run (ubuntu-latest, 3.10) (push) Failing after 0s
Pytest/Test Workflow / Import Test and Pytest Run (ubuntu-latest, 3.11) (push) Failing after 0s
PR Conflict Labeler / main (push) Failing after 2s
Pytest/Test Workflow / Import Test and Pytest Run (ubuntu-latest, 3.12) (push) Failing after 2s
Pytest/Test Workflow / Import Test and Pytest Run (ubuntu-latest, 3.13) (push) Failing after 0s
Pytest/Test Workflow / Build this Package (push) Failing after 5s
Pytest/Test Workflow / Import Test and Pytest Run (macos-latest, 3.10) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (macos-latest, 3.11) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (macos-latest, 3.12) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (macos-latest, 3.13) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (windows-latest, 3.10) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (windows-latest, 3.11) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (windows-latest, 3.12) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (windows-latest, 3.13) (push) Has been cancelled
Pytest/Test Workflow / testing-guardian (push) Has been cancelled
89 lines
2.8 KiB
Python
89 lines
2.8 KiB
Python
from datetime import datetime
|
|
|
|
import numpy as np
|
|
|
|
import supervision as sv
|
|
|
|
|
|
class FPSBasedTimer:
|
|
"""
|
|
A timer that calculates the duration each object has been detected based on frames
|
|
per second (FPS).
|
|
|
|
Attributes:
|
|
fps (float): The frame rate of the video stream, used to calculate
|
|
time durations.
|
|
frame_id (int): The current frame number in the sequence.
|
|
tracker_id2frame_id (Dict[int, int]): Maps each tracker's ID to the frame number
|
|
at which it was first detected.
|
|
"""
|
|
|
|
def __init__(self, fps: float = 30) -> None:
|
|
"""Initializes the FPSBasedTimer with the specified frames per second rate.
|
|
|
|
Args:
|
|
fps (float): The frame rate of the video stream. Defaults to 30.
|
|
"""
|
|
self.fps = fps
|
|
self.frame_id = 0
|
|
self.tracker_id2frame_id: dict[int, int] = {}
|
|
|
|
def tick(self, detections: sv.Detections) -> np.ndarray:
|
|
"""Processes the current frame, updating time durations for each tracker.
|
|
|
|
Args:
|
|
detections: The detections for the current frame, including tracker IDs.
|
|
|
|
Returns:
|
|
np.ndarray: Time durations (in seconds) for each detected tracker, since
|
|
their first detection.
|
|
"""
|
|
self.frame_id += 1
|
|
times = []
|
|
|
|
for tracker_id in detections.tracker_id:
|
|
self.tracker_id2frame_id.setdefault(tracker_id, self.frame_id)
|
|
|
|
start_frame_id = self.tracker_id2frame_id[tracker_id]
|
|
time_duration = (self.frame_id - start_frame_id) / self.fps
|
|
times.append(time_duration)
|
|
|
|
return np.array(times)
|
|
|
|
|
|
class ClockBasedTimer:
|
|
"""
|
|
A timer that calculates the duration each object has been detected based on the
|
|
system clock.
|
|
|
|
Attributes:
|
|
tracker_id2start_time (Dict[int, datetime]): Maps each tracker's ID to the
|
|
datetime when it was first detected.
|
|
"""
|
|
|
|
def __init__(self) -> None:
|
|
"""Initializes the ClockBasedTimer."""
|
|
self.tracker_id2start_time: dict[int, datetime] = {}
|
|
|
|
def tick(self, detections: sv.Detections) -> np.ndarray:
|
|
"""Processes the current frame, updating time durations for each tracker.
|
|
|
|
Args:
|
|
detections: The detections for the current frame, including tracker IDs.
|
|
|
|
Returns:
|
|
np.ndarray: Time durations (in seconds) for each detected tracker, since
|
|
their first detection.
|
|
"""
|
|
current_time = datetime.now()
|
|
times = []
|
|
|
|
for tracker_id in detections.tracker_id:
|
|
self.tracker_id2start_time.setdefault(tracker_id, current_time)
|
|
|
|
start_time = self.tracker_id2start_time[tracker_id]
|
|
time_duration = (current_time - start_time).total_seconds()
|
|
times.append(time_duration)
|
|
|
|
return np.array(times)
|