diff --git a/docs/docs/configuration/reference.md b/docs/docs/configuration/reference.md index 30b14f687..93ab40ee7 100644 --- a/docs/docs/configuration/reference.md +++ b/docs/docs/configuration/reference.md @@ -662,7 +662,10 @@ cameras: front_steps: # Required: List of x,y coordinates to define the polygon of the zone. # NOTE: Presence in a zone is evaluated only based on the bottom center of the objects bounding box. - coordinates: 0.284,0.997,0.389,0.869,0.410,0.745 + coordinates: 0.033,0.306,0.324,0.138,0.439,0.185,0.042,0.428 + # Optional: The real-world distances of a 4-sided zone used for zones with speed estimation enabled (default: none) + # List distances in order of the zone points coordinates and use the unit system defined in the ui config + distances: 10,15,12,11 # Optional: Number of consecutive frames required for object to be considered present in the zone (default: shown below). inertia: 3 # Optional: Number of seconds that an object must loiter to be considered in the zone (default: shown below) @@ -813,6 +816,9 @@ ui: # https://www.gnu.org/software/libc/manual/html_node/Formatting-Calendar-Time.html # possible values are shown above (default: not set) strftime_fmt: "%Y/%m/%d %H:%M" + # Optional: Set the unit system to either "imperial" or "metric" (default: metric) + # Used in the UI and in MQTT topics + unit_system: metric # Optional: Telemetry configuration telemetry: diff --git a/docs/docs/configuration/zones.md b/docs/docs/configuration/zones.md index aef6b0a5b..1d1e66c27 100644 --- a/docs/docs/configuration/zones.md +++ b/docs/docs/configuration/zones.md @@ -122,16 +122,59 @@ cameras: - car ``` -### Loitering Time +### Speed Estimation -Zones support a `loitering_time` configuration which can be used to only consider an object as part of a zone if they loiter in the zone for the specified number of seconds. This can be used, for example, to create alerts for cars that stop on the street but not cars that just drive past your camera. +Frigate can be configured to estimate the speed of objects moving through a zone. This works by combining data from Frigate's object tracker and "real world" distance measurements of the edges of the zone. The recommended use case for this feature is to track the speed of vehicles on a road as they move through the zone. + +Your zone must be defined with exactly 4 points and should be aligned to the ground where objects are moving. + + + +Speed estimation requires a minimum number of frames for your object to be tracked before a valid estimate can be calculated, so create your zone away from places where objects enter and exit for the best results. _Your zone should not take up the full frame._ An object's speed is tracked while it is in the zone and then saved to Frigate's database. + +Accurate real-world distance measurements are required to estimate speeds. These distances can be specified in your zone config through the `distances` field. ```yaml cameras: name_of_your_camera: zones: - front_yard: - loitering_time: 5 # unit is in seconds - objects: - - person + street: + coordinates: 0.033,0.306,0.324,0.138,0.439,0.185,0.042,0.428 + distances: 10,12,11,13.5 +``` + +Each number in the `distance` field represents the real-world distance between the points in the `coordinates` list. So in the example above, the distance between the first two points ([0.033,0.306] and [0.324,0.138]) is 10. The distance between the second and third set of points ([0.324,0.138] and [0.439,0.185]) is 12, and so on. The fastest and most accurate way to configure this is through the Zone Editor in the Frigate UI. + +The `distance` values are measured in meters or feet, depending on how `unit_system` is configured in your `ui` config: + +```yaml +ui: + # can be "metric" or "imperial", default is metric + unit_system: metric +``` + +The average speed of your object as it moved through your zone is saved in Frigate's database and can be seen in the UI in the Tracked Object Details pane in Explore. Current estimated speed can also be seen on the debug view as the third value in the object label (see the caveats below). Current estimated speed, average estimated speed, and velocity angle (the angle of the direction the object is moving relative to the frame) of tracked objects is also sent through the `events` MQTT topic. See the [MQTT docs](../integrations/mqtt.md#frigateevents). These speed values are output as a number in miles per hour (mph) or kilometers per hour (kph), depending on how `unit_system` is configured in your `ui` config. + +#### Best practices and caveats + +- Speed estimation works best with a straight road or path when your object travels in a straight line across that path. If your object makes turns, speed estimation may not be accurate. +- Create a zone where the bottom center of your object's bounding box travels directly through it and does not become obscured at any time. +- Depending on the size and location of your zone, you may want to decrease the zone's `inertia` value from the default of 3. +- The more accurate your real-world dimensions can be measured, the more accurate speed estimation will be. However, due to the way Frigate's tracking algorithm works, you may need to tweak the real-world distance values so that estimated speeds better match real-world speeds. +- Once an object leaves the zone, speed accuracy will likely decrease due to perspective distortion and misalignment with the calibrated area. Therefore, speed values will show as a zero through MQTT and will not be visible on the debug view when an object is outside of a speed tracking zone. +- The speeds are only an _estimation_ and are highly dependent on camera position, zone points, and real-world measurements. This feature should not be used for law enforcement. + +### Speed Threshold + +Zones can be configured with a minimum speed requirement, meaning an object must be moving at or above this speed to be considered inside the zone. Zone `distances` must be defined as described above. + +```yaml +cameras: + name_of_your_camera: + zones: + sidewalk: + coordinates: ... + distances: ... + inertia: 1 + speed_threshold: 20 # unit is in kph or mph, depending on how unit_system is set (see above) ``` diff --git a/docs/docs/integrations/mqtt.md b/docs/docs/integrations/mqtt.md index 194821cbd..45d95c9f4 100644 --- a/docs/docs/integrations/mqtt.md +++ b/docs/docs/integrations/mqtt.md @@ -52,7 +52,9 @@ Message published for each changed tracked object. The first message is publishe "attributes": { "face": 0.64 }, // attributes with top score that have been identified on the object at any point - "current_attributes": [] // detailed data about the current attributes in this frame + "current_attributes": [], // detailed data about the current attributes in this frame + "current_estimated_speed": 0.71, // current estimated speed (mph or kph) for objects moving through zones with speed estimation enabled + "velocity_angle": 180 // direction of travel relative to the frame for objects moving through zones with speed estimation enabled }, "after": { "id": "1607123955.475377-mxklsc", @@ -89,7 +91,9 @@ Message published for each changed tracked object. The first message is publishe "box": [442, 506, 534, 524], "score": 0.86 } - ] + ], + "current_estimated_speed": 0.77, // current estimated speed (mph or kph) for objects moving through zones with speed estimation enabled + "velocity_angle": 180 // direction of travel relative to the frame for objects moving through zones with speed estimation enabled } } ``` diff --git a/docs/static/img/ground-plane.jpg b/docs/static/img/ground-plane.jpg new file mode 100644 index 000000000..f7ea4db2a Binary files /dev/null and b/docs/static/img/ground-plane.jpg differ diff --git a/frigate/api/defs/query/events_query_parameters.py b/frigate/api/defs/query/events_query_parameters.py index 5a2b61d43..01c79abb0 100644 --- a/frigate/api/defs/query/events_query_parameters.py +++ b/frigate/api/defs/query/events_query_parameters.py @@ -25,6 +25,8 @@ class EventsQueryParams(BaseModel): favorites: Optional[int] = None min_score: Optional[float] = None max_score: Optional[float] = None + min_speed: Optional[float] = None + max_speed: Optional[float] = None is_submitted: Optional[int] = None min_length: Optional[float] = None max_length: Optional[float] = None @@ -51,6 +53,8 @@ class EventsSearchQueryParams(BaseModel): timezone: Optional[str] = "utc" min_score: Optional[float] = None max_score: Optional[float] = None + min_speed: Optional[float] = None + max_speed: Optional[float] = None sort: Optional[str] = None diff --git a/frigate/api/event.py b/frigate/api/event.py index fc51a06c2..247366920 100644 --- a/frigate/api/event.py +++ b/frigate/api/event.py @@ -92,6 +92,8 @@ def events(params: EventsQueryParams = Depends()): favorites = params.favorites min_score = params.min_score max_score = params.max_score + min_speed = params.min_speed + max_speed = params.max_speed is_submitted = params.is_submitted min_length = params.min_length max_length = params.max_length @@ -226,6 +228,12 @@ def events(params: EventsQueryParams = Depends()): if min_score is not None: clauses.append((Event.data["score"] >= min_score)) + if max_speed is not None: + clauses.append((Event.data["average_estimated_speed"] <= max_speed)) + + if min_speed is not None: + clauses.append((Event.data["average_estimated_speed"] >= min_speed)) + if min_length is not None: clauses.append(((Event.end_time - Event.start_time) >= min_length)) @@ -249,6 +257,10 @@ def events(params: EventsQueryParams = Depends()): order_by = Event.data["score"].asc() elif sort == "score_desc": order_by = Event.data["score"].desc() + elif sort == "speed_asc": + order_by = Event.data["average_estimated_speed"].asc() + elif sort == "speed_desc": + order_by = Event.data["average_estimated_speed"].desc() elif sort == "date_asc": order_by = Event.start_time.asc() elif sort == "date_desc": @@ -316,7 +328,15 @@ def events_explore(limit: int = 10): k: v for k, v in event.data.items() if k - in ["type", "score", "top_score", "description", "sub_label_score"] + in [ + "type", + "score", + "top_score", + "description", + "sub_label_score", + "average_estimated_speed", + "velocity_angle", + ] }, "event_count": label_counts[event.label], } @@ -367,6 +387,8 @@ def events_search(request: Request, params: EventsSearchQueryParams = Depends()) before = params.before min_score = params.min_score max_score = params.max_score + min_speed = params.min_speed + max_speed = params.max_speed time_range = params.time_range has_clip = params.has_clip has_snapshot = params.has_snapshot @@ -466,6 +488,16 @@ def events_search(request: Request, params: EventsSearchQueryParams = Depends()) if max_score is not None: event_filters.append((Event.data["score"] <= max_score)) + if min_speed is not None and max_speed is not None: + event_filters.append( + (Event.data["average_estimated_speed"].between(min_speed, max_speed)) + ) + else: + if min_speed is not None: + event_filters.append((Event.data["average_estimated_speed"] >= min_speed)) + if max_speed is not None: + event_filters.append((Event.data["average_estimated_speed"] <= max_speed)) + if time_range != DEFAULT_TIME_RANGE: tz_name = params.timezone hour_modifier, minute_modifier, _ = get_tz_modifiers(tz_name) @@ -581,7 +613,16 @@ def events_search(request: Request, params: EventsSearchQueryParams = Depends()) processed_event["data"] = { k: v for k, v in event["data"].items() - if k in ["type", "score", "top_score", "description"] + if k + in [ + "type", + "score", + "top_score", + "description", + "sub_label_score", + "average_estimated_speed", + "velocity_angle", + ] } if event["id"] in search_results: @@ -596,6 +637,10 @@ def events_search(request: Request, params: EventsSearchQueryParams = Depends()) processed_events.sort(key=lambda x: x["score"]) elif min_score is not None and max_score is not None and sort == "score_desc": processed_events.sort(key=lambda x: x["score"], reverse=True) + elif min_speed is not None and max_speed is not None and sort == "speed_asc": + processed_events.sort(key=lambda x: x["average_estimated_speed"]) + elif min_speed is not None and max_speed is not None and sort == "speed_desc": + processed_events.sort(key=lambda x: x["average_estimated_speed"], reverse=True) elif sort == "date_asc": processed_events.sort(key=lambda x: x["start_time"]) else: diff --git a/frigate/config/camera/zone.py b/frigate/config/camera/zone.py index 37fc1b744..3e69240d5 100644 --- a/frigate/config/camera/zone.py +++ b/frigate/config/camera/zone.py @@ -1,13 +1,16 @@ # this uses the base model because the color is an extra attribute +import logging from typing import Optional, Union import numpy as np -from pydantic import BaseModel, Field, PrivateAttr, field_validator +from pydantic import BaseModel, Field, PrivateAttr, field_validator, model_validator from .objects import FilterConfig __all__ = ["ZoneConfig"] +logger = logging.getLogger(__name__) + class ZoneConfig(BaseModel): filters: dict[str, FilterConfig] = Field( @@ -16,6 +19,10 @@ class ZoneConfig(BaseModel): coordinates: Union[str, list[str]] = Field( title="Coordinates polygon for the defined zone." ) + distances: Optional[Union[str, list[str]]] = Field( + default_factory=list, + title="Real-world distances for the sides of quadrilateral for the defined zone.", + ) inertia: int = Field( default=3, title="Number of consecutive frames required for object to be considered present in the zone.", @@ -26,6 +33,11 @@ class ZoneConfig(BaseModel): ge=0, title="Number of seconds that an object must loiter to be considered in the zone.", ) + speed_threshold: Optional[float] = Field( + default=None, + ge=0.1, + title="Minimum speed value for an object to be considered in the zone.", + ) objects: Union[str, list[str]] = Field( default_factory=list, title="List of objects that can trigger the zone.", @@ -49,6 +61,34 @@ class ZoneConfig(BaseModel): return v + @field_validator("distances", mode="before") + @classmethod + def validate_distances(cls, v): + if v is None: + return None + + if isinstance(v, str): + distances = list(map(str, map(float, v.split(",")))) + elif isinstance(v, list): + distances = [str(float(val)) for val in v] + else: + raise ValueError("Invalid type for distances") + + if len(distances) != 4: + raise ValueError("distances must have exactly 4 values") + + return distances + + @model_validator(mode="after") + def check_loitering_time_constraints(self): + if self.loitering_time > 0 and ( + self.speed_threshold is not None or len(self.distances) > 0 + ): + logger.warning( + "loitering_time should not be set on a zone if speed_threshold or distances is set." + ) + return self + def __init__(self, **config): super().__init__(**config) diff --git a/frigate/config/ui.py b/frigate/config/ui.py index a562edf61..2f66aeed3 100644 --- a/frigate/config/ui.py +++ b/frigate/config/ui.py @@ -5,7 +5,7 @@ from pydantic import Field from .base import FrigateBaseModel -__all__ = ["TimeFormatEnum", "DateTimeStyleEnum", "UIConfig"] +__all__ = ["TimeFormatEnum", "DateTimeStyleEnum", "UnitSystemEnum", "UIConfig"] class TimeFormatEnum(str, Enum): @@ -21,6 +21,11 @@ class DateTimeStyleEnum(str, Enum): short = "short" +class UnitSystemEnum(str, Enum): + imperial = "imperial" + metric = "metric" + + class UIConfig(FrigateBaseModel): timezone: Optional[str] = Field(default=None, title="Override UI timezone.") time_format: TimeFormatEnum = Field( @@ -35,3 +40,6 @@ class UIConfig(FrigateBaseModel): strftime_fmt: Optional[str] = Field( default=None, title="Override date and time format using strftime syntax." ) + unit_system: UnitSystemEnum = Field( + default=UnitSystemEnum.metric, title="The unit system to use for measurements." + ) diff --git a/frigate/events/maintainer.py b/frigate/events/maintainer.py index 68e7432ab..ebc506c73 100644 --- a/frigate/events/maintainer.py +++ b/frigate/events/maintainer.py @@ -25,6 +25,9 @@ def should_update_db(prev_event: Event, current_event: Event) -> bool: or prev_event["entered_zones"] != current_event["entered_zones"] or prev_event["thumbnail"] != current_event["thumbnail"] or prev_event["end_time"] != current_event["end_time"] + or prev_event["average_estimated_speed"] + != current_event["average_estimated_speed"] + or prev_event["velocity_angle"] != current_event["velocity_angle"] ): return True return False @@ -210,6 +213,8 @@ class EventProcessor(threading.Thread): "score": score, "top_score": event_data["top_score"], "attributes": attributes, + "average_estimated_speed": event_data["average_estimated_speed"], + "velocity_angle": event_data["velocity_angle"], "type": "object", "max_severity": event_data.get("max_severity"), }, diff --git a/frigate/object_processing.py b/frigate/object_processing.py index ba2e15b20..dfaf894f7 100644 --- a/frigate/object_processing.py +++ b/frigate/object_processing.py @@ -160,7 +160,12 @@ class CameraState: box[2], box[3], text, - f"{obj['score']:.0%} {int(obj['area'])}", + f"{obj['score']:.0%} {int(obj['area'])}" + + ( + f" {float(obj['current_estimated_speed']):.1f}" + if obj["current_estimated_speed"] != 0 + else "" + ), thickness=thickness, color=color, ) @@ -254,6 +259,7 @@ class CameraState: new_obj = tracked_objects[id] = TrackedObject( self.config.model, self.camera_config, + self.config.ui, self.frame_cache, current_detections[id], ) diff --git a/frigate/track/tracked_object.py b/frigate/track/tracked_object.py index 3da2a5e04..ea1aeedcb 100644 --- a/frigate/track/tracked_object.py +++ b/frigate/track/tracked_object.py @@ -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 ( @@ -22,6 +23,7 @@ from frigate.util.image import ( is_better_thumbnail, ) from frigate.util.object import box_inside +from frigate.util.velocity import calculate_real_world_speed logger = logging.getLogger(__name__) @@ -31,6 +33,7 @@ class TrackedObject: self, model_config: ModelConfig, camera_config: CameraConfig, + ui_config: UIConfig, frame_cache, obj_data: dict[str, any], ): @@ -42,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] = {} @@ -58,6 +62,10 @@ class TrackedObject: self.frame = None self.active = True self.pending_loitering = False + self.speed_history = [] + self.current_estimated_speed = 0 + self.average_estimated_speed = 0 + self.velocity_angle = 0 self.previous = self.to_dict() @property @@ -129,6 +137,8 @@ class TrackedObject: "region": obj_data["region"], "score": obj_data["score"], "attributes": obj_data["attributes"], + "current_estimated_speed": self.current_estimated_speed, + "velocity_angle": self.velocity_angle, } thumb_update = True @@ -136,6 +146,7 @@ class TrackedObject: current_zones = [] bottom_center = (obj_data["centroid"][0], obj_data["box"][3]) in_loitering_zone = False + in_speed_zone = False # check each zone for name, zone in self.camera_config.zones.items(): @@ -144,12 +155,66 @@ class TrackedObject: continue contour = zone.contour zone_score = self.zone_presence.get(name, 0) + 1 + # check if the object is in the zone if cv2.pointPolygonTest(contour, bottom_center, False) >= 0: # if the object passed the filters once, dont apply again if name in self.current_zones or not zone_filtered(self, zone.filters): - # an object is only considered present in a zone if it has a zone inertia of 3+ + # Calculate speed first if this is a speed zone + if ( + zone.distances + and obj_data["frame_time"] == current_frame_time + and self.active + ): + speed_magnitude, self.velocity_angle = ( + calculate_real_world_speed( + zone.contour, + zone.distances, + self.obj_data["estimate_velocity"], + bottom_center, + self.camera_config.detect.fps, + ) + ) + + if self.ui_config.unit_system == "metric": + self.current_estimated_speed = ( + speed_magnitude * 3.6 + ) # m/s to km/h + else: + self.current_estimated_speed = ( + speed_magnitude * 0.681818 + ) # ft/s to mph + + self.speed_history.append(self.current_estimated_speed) + if len(self.speed_history) > 10: + self.speed_history = self.speed_history[-10:] + + self.average_estimated_speed = sum(self.speed_history) / len( + self.speed_history + ) + + # we've exceeded the speed threshold on the zone + # or we don't have a speed threshold set + if ( + zone.speed_threshold is None + or self.average_estimated_speed > zone.speed_threshold + ): + in_speed_zone = True + + logger.debug( + f"Camera: {self.camera_config.name}, tracked object ID: {self.obj_data['id']}, " + f"zone: {name}, pixel velocity: {str(tuple(np.round(self.obj_data['estimate_velocity']).flatten().astype(int)))}, " + f"speed magnitude: {speed_magnitude}, velocity angle: {self.velocity_angle}, " + f"estimated speed: {self.current_estimated_speed:.1f}, " + f"average speed: {self.average_estimated_speed:.1f}, " + f"length: {len(self.speed_history)}" + ) + + # Check zone entry conditions - for speed zones, require both inertia and speed if zone_score >= zone.inertia: + if zone.distances and not in_speed_zone: + continue # Skip zone entry for speed zones until speed threshold met + # if the zone has loitering time, update loitering status if zone.loitering_time > 0: in_loitering_zone = True @@ -174,6 +239,10 @@ class TrackedObject: if 0 < zone_score < zone.inertia: self.zone_presence[name] = zone_score - 1 + # Reset speed if not in speed zone + if zone.distances and name not in current_zones: + self.current_estimated_speed = 0 + # update loitering status self.pending_loitering = in_loitering_zone @@ -255,6 +324,9 @@ class TrackedObject: "current_attributes": self.obj_data["attributes"], "pending_loitering": self.pending_loitering, "max_severity": self.max_severity, + "current_estimated_speed": self.current_estimated_speed, + "average_estimated_speed": self.average_estimated_speed, + "velocity_angle": self.velocity_angle, } if include_thumbnail: @@ -339,7 +411,12 @@ class TrackedObject: box[2], box[3], self.obj_data["label"], - f"{int(self.thumbnail_data['score'] * 100)}% {int(self.thumbnail_data['area'])}", + f"{int(self.thumbnail_data['score'] * 100)}% {int(self.thumbnail_data['area'])}" + + ( + f" {self.thumbnail_data['current_estimated_speed']:.1f}" + if self.thumbnail_data["current_estimated_speed"] != 0 + else "" + ), thickness=thickness, color=color, ) diff --git a/frigate/util/velocity.py b/frigate/util/velocity.py new file mode 100644 index 000000000..207215bfb --- /dev/null +++ b/frigate/util/velocity.py @@ -0,0 +1,127 @@ +import math + +import numpy as np + + +def order_points_clockwise(points): + """ + Ensure points are sorted in clockwise order starting from the top left + + :param points: Array of zone corner points in pixel coordinates + :return: Ordered list of points + """ + top_left = min( + points, key=lambda p: (p[1], p[0]) + ) # Find the top-left point (min y, then x) + + # Remove the top-left point from the list of points + remaining_points = [p for p in points if not np.array_equal(p, top_left)] + + # Sort the remaining points based on the angle relative to the top-left point + def angle_from_top_left(point): + x, y = point[0] - top_left[0], point[1] - top_left[1] + return math.atan2(y, x) + + sorted_points = sorted(remaining_points, key=angle_from_top_left) + + return [top_left] + sorted_points + + +def create_ground_plane(zone_points, distances): + """ + Create a ground plane that accounts for perspective distortion using real-world dimensions for each side of the zone. + + :param zone_points: Array of zone corner points in pixel coordinates + [[x1, y1], [x2, y2], [x3, y3], [x4, y4]] + :param distances: Real-world dimensions ordered by A, B, C, D + :return: Function that calculates real-world distance per pixel at any coordinate + """ + A, B, C, D = zone_points + + # Calculate pixel lengths of each side + AB_px = np.linalg.norm(np.array(B) - np.array(A)) + BC_px = np.linalg.norm(np.array(C) - np.array(B)) + CD_px = np.linalg.norm(np.array(D) - np.array(C)) + DA_px = np.linalg.norm(np.array(A) - np.array(D)) + + AB, BC, CD, DA = map(float, distances) + + AB_scale = AB / AB_px + BC_scale = BC / BC_px + CD_scale = CD / CD_px + DA_scale = DA / DA_px + + def distance_per_pixel(x, y): + """ + Calculate the real-world distance per pixel at a given (x, y) coordinate. + + :param x: X-coordinate in the image + :param y: Y-coordinate in the image + :return: Real-world distance per pixel at the given (x, y) coordinate + """ + # Normalize x and y within the zone + x_norm = (x - A[0]) / (B[0] - A[0]) + y_norm = (y - A[1]) / (D[1] - A[1]) + + # Interpolate scales horizontally and vertically + vertical_scale = AB_scale + (CD_scale - AB_scale) * y_norm + horizontal_scale = DA_scale + (BC_scale - DA_scale) * x_norm + + # Combine horizontal and vertical scales + return (vertical_scale + horizontal_scale) / 2 + + return distance_per_pixel + + +def calculate_real_world_speed( + zone_contour, + distances, + velocity_pixels, + position, + camera_fps, +): + """ + Calculate the real-world speed of a tracked object, accounting for perspective, + directly from the zone string. + + :param zone_contour: Array of absolute zone points + :param distances: List of distances of each side, ordered by A, B, C, D + :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 and velocity angle direction + """ + # order the zone_contour points clockwise starting at top left + ordered_zone_contour = order_points_clockwise(zone_contour) + + # find the indices that would sort the original zone_contour to match ordered_zone_contour + sort_indices = [ + np.where((zone_contour == point).all(axis=1))[0][0] + for point in ordered_zone_contour + ] + + # Reorder distances to match the new order of zone_contour + distances = np.array(distances) + ordered_distances = distances[sort_indices] + + ground_plane = create_ground_plane(ordered_zone_contour, ordered_distances) + + if not isinstance(velocity_pixels, np.ndarray): + velocity_pixels = np.array(velocity_pixels) + + avg_velocity_pixels = velocity_pixels.mean(axis=0) + + # get the real-world distance per pixel at the object's current position and calculate real speed + scale = ground_plane(position[0], position[1]) + speed_real = avg_velocity_pixels * scale * camera_fps + + # euclidean speed in real-world units/second + speed_magnitude = np.linalg.norm(speed_real) + + # movement direction + dx, dy = avg_velocity_pixels + angle = math.degrees(math.atan2(dy, dx)) + if angle < 0: + angle += 360 + + return speed_magnitude, angle diff --git a/web/src/components/filter/SearchFilterGroup.tsx b/web/src/components/filter/SearchFilterGroup.tsx index 8aa87f8e4..740a3bce7 100644 --- a/web/src/components/filter/SearchFilterGroup.tsx +++ b/web/src/components/filter/SearchFilterGroup.tsx @@ -116,6 +116,9 @@ export default function SearchFilterGroup({ if (filter?.min_score || filter?.max_score) { sortTypes.push("score_desc", "score_asc"); } + if (filter?.min_speed || filter?.max_speed) { + sortTypes.push("speed_desc", "speed_asc"); + } if (filter?.event_id || filter?.query) { sortTypes.push("relevance"); } @@ -498,6 +501,8 @@ export function SortTypeContent({ date_desc: "Date (Descending)", score_asc: "Object Score (Ascending)", score_desc: "Object Score (Descending)", + speed_asc: "Estimated Speed (Ascending)", + speed_desc: "Estimated Speed (Descending)", relevance: "Relevance", }; diff --git a/web/src/components/input/InputWithTags.tsx b/web/src/components/input/InputWithTags.tsx index d5904b2a5..3ae78e70a 100644 --- a/web/src/components/input/InputWithTags.tsx +++ b/web/src/components/input/InputWithTags.tsx @@ -216,11 +216,14 @@ export default function InputWithTags({ type == "after" || type == "time_range" || type == "min_score" || - type == "max_score" + type == "max_score" || + type == "min_speed" || + type == "max_speed" ) { const newFilters = { ...filters }; let timestamp = 0; let score = 0; + let speed = 0; switch (type) { case "before": @@ -294,6 +297,40 @@ export default function InputWithTags({ newFilters[type] = score / 100; } break; + case "min_speed": + case "max_speed": + speed = parseFloat(value); + if (score >= 0) { + // Check for conflicts between min_speed and max_speed + if ( + type === "min_speed" && + filters.max_speed !== undefined && + speed > filters.max_speed + ) { + toast.error( + "The 'min_speed' must be less than or equal to the 'max_speed'.", + { + position: "top-center", + }, + ); + return; + } + if ( + type === "max_speed" && + filters.min_speed !== undefined && + speed < filters.min_speed + ) { + toast.error( + "The 'max_speed' must be greater than or equal to the 'min_speed'.", + { + position: "top-center", + }, + ); + return; + } + newFilters[type] = speed; + } + break; case "time_range": newFilters[type] = value; break; @@ -369,6 +406,10 @@ export default function InputWithTags({ }`; } else if (filterType === "min_score" || filterType === "max_score") { return Math.round(Number(filterValues) * 100).toString() + "%"; + } else if (filterType === "min_speed" || filterType === "max_speed") { + return ( + filterValues + (config?.ui.unit_system == "metric" ? " kph" : " mph") + ); } else if ( filterType === "has_clip" || filterType === "has_snapshot" || @@ -397,7 +438,11 @@ export default function InputWithTags({ ((filterType === "min_score" || filterType === "max_score") && !isNaN(Number(trimmedValue)) && Number(trimmedValue) >= 50 && - Number(trimmedValue) <= 100) + Number(trimmedValue) <= 100) || + ((filterType === "min_speed" || filterType === "max_speed") && + !isNaN(Number(trimmedValue)) && + Number(trimmedValue) >= 1 && + Number(trimmedValue) <= 150) ) { createFilter( filterType, diff --git a/web/src/components/overlay/detail/SearchDetailDialog.tsx b/web/src/components/overlay/detail/SearchDetailDialog.tsx index f15627b71..dd088ad83 100644 --- a/web/src/components/overlay/detail/SearchDetailDialog.tsx +++ b/web/src/components/overlay/detail/SearchDetailDialog.tsx @@ -25,6 +25,7 @@ import { baseUrl } from "@/api/baseUrl"; import { cn } from "@/lib/utils"; import ActivityIndicator from "@/components/indicators/activity-indicator"; import { + FaArrowRight, FaCheckCircle, FaChevronDown, FaDownload, @@ -329,6 +330,30 @@ function ObjectDetailsTab({ } }, [search]); + const averageEstimatedSpeed = useMemo(() => { + if (!search || !search.data?.average_estimated_speed) { + return undefined; + } + + if (search.data?.average_estimated_speed != 0) { + return search.data?.average_estimated_speed.toFixed(1); + } else { + return undefined; + } + }, [search]); + + const velocityAngle = useMemo(() => { + if (!search || !search.data?.velocity_angle) { + return undefined; + } + + if (search.data?.velocity_angle != 0) { + return search.data?.velocity_angle.toFixed(1); + } else { + return undefined; + } + }, [search]); + const updateDescription = useCallback(() => { if (!search) { return; @@ -440,6 +465,29 @@ function ObjectDetailsTab({ {score}%{subLabelScore && ` (${subLabelScore}%)`} + {averageEstimatedSpeed && ( +