ability to define unit system in config

This commit is contained in:
Josh Hawkins 2024-12-09 08:05:43 -06:00
parent 3af548602a
commit aff20f0712
3 changed files with 13 additions and 17 deletions

View File

@ -261,6 +261,7 @@ class CameraState:
new_obj = tracked_objects[id] = TrackedObject(
self.config.model,
self.camera_config,
self.config.ui,
self.frame_cache,
current_detections[id],
)

View File

@ -12,6 +12,7 @@ import numpy as np
from frigate.config import (
CameraConfig,
ModelConfig,
UIConfig,
)
from frigate.review.types import SeverityEnum
from frigate.util.image import (
@ -32,6 +33,7 @@ class TrackedObject:
self,
model_config: ModelConfig,
camera_config: CameraConfig,
ui_config: UIConfig,
frame_cache,
obj_data: dict[str, any],
):
@ -43,6 +45,7 @@ class TrackedObject:
self.colormap = model_config.colormap
self.logos = model_config.all_attribute_logos
self.camera_config = camera_config
self.ui_config = ui_config
self.frame_cache = frame_cache
self.zone_presence: dict[str, int] = {}
self.zone_loitering: dict[str, int] = {}
@ -181,7 +184,7 @@ class TrackedObject:
# update speed
if zone.distances and name in self.entered_zones:
self.estimated_speed, self.velocity_angle = (
speed_magnitude, self.velocity_angle = (
calculate_real_world_speed(
zone.contour,
zone.distances,
@ -192,10 +195,14 @@ class TrackedObject:
if self.active
else 0
)
if self.ui_config.unit_system == "metric":
# Convert m/s to km/h
self.estimated_speed = speed_magnitude * 3.6
elif self.ui_config.unit_system == "imperial":
# Convert ft/s to mph
self.estimated_speed = speed_magnitude * 0.681818
logger.debug(
f"Camera: {self.camera_config.name}, zone: {name}, tracked object ID: {self.obj_data['id']}, \
pixel velocity: {str(tuple(np.round(self.obj_data["estimate_velocity"]).flatten().astype(int)))} \
estimated speed: {self.estimated_speed:.1f}"
f"Camera: {self.camera_config.name}, zone: {name}, tracked object ID: {self.obj_data['id']}, pixel velocity: {str(tuple(np.round(self.obj_data['estimate_velocity']).flatten().astype(int)))} estimated speed: {self.estimated_speed:.1f}"
)
if self.estimated_speed > self.max_estimated_speed:

View File

@ -2,9 +2,6 @@ import math
import numpy as np
unit_system = "imperial"
magnitude = "mph"
def create_ground_plane(zone_points, distances):
"""
@ -68,7 +65,7 @@ def calculate_real_world_speed(
:param velocity_pixels: List of tuples representing velocity in pixels/frame
:param position: Current position of the object (x, y) in pixels
:param camera_fps: Frames per second of the camera
:return: speed in the specified unit system (m/s for metric, ft/s for imperial) and velocity direction
:return: speed and velocity angle direction
"""
ground_plane = create_ground_plane(zone_contour, distances)
@ -90,13 +87,4 @@ def calculate_real_world_speed(
if angle < 0:
angle += 360
if unit_system == "metric":
if magnitude == "kmh":
# Convert m/s to km/h
speed_magnitude *= 3.6
elif unit_system == "imperial":
if magnitude == "mph":
# Convert ft/s to mph
speed_magnitude *= 0.681818
return speed_magnitude, angle