Skip to content

Commit

Permalink
Merge pull request #985 from roboflow/extend-DetectionsProperty-to-ha…
Browse files Browse the repository at this point in the history
…ndle-all-blocks-custom-data-keys

Extend query_language DetectionsProperty to cover sv.Detections data keys produced by blocks
  • Loading branch information
grzegorz-roboflow authored Jan 29, 2025
2 parents 57edf77 + 67943be commit f284753
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 12 deletions.
34 changes: 22 additions & 12 deletions inference/core/workflows/core_steps/analytics/velocity/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
Note: due to perspective and camera distortions calculated velocity will be different depending on object position in relation to the camera.
"""
VELOCITY_KEY_IN_SV_DETECTIONS = "velocity"
SPEED_KEY_IN_SV_DETECTIONS = "speed"
SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS = "smoothed_velocity"
SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS = "smoothed_speed"


class VelocityManifest(WorkflowBlockManifest):
Expand Down Expand Up @@ -197,21 +201,27 @@ def run(
detections.data = {}

# Initialize dictionaries if not present
if "velocity" not in detections.data:
detections.data["velocity"] = {}
if "speed" not in detections.data:
detections.data["speed"] = {}
if "smoothed_velocity" not in detections.data:
detections.data["smoothed_velocity"] = {}
if "smoothed_speed" not in detections.data:
detections.data["smoothed_speed"] = {}
if VELOCITY_KEY_IN_SV_DETECTIONS not in detections.data:
detections.data[VELOCITY_KEY_IN_SV_DETECTIONS] = {}
if SPEED_KEY_IN_SV_DETECTIONS not in detections.data:
detections.data[SPEED_KEY_IN_SV_DETECTIONS] = {}
if SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS not in detections.data:
detections.data[SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS] = {}
if SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS not in detections.data:
detections.data[SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS] = {}

# Assign velocity data to the corresponding tracker_id
detections.data["velocity"][tracker_id] = velocity_m_s.tolist() # [vx, vy]
detections.data["speed"][tracker_id] = speed_m_s # Scalar
detections.data["smoothed_velocity"][
detections.data[VELOCITY_KEY_IN_SV_DETECTIONS][
tracker_id
] = velocity_m_s.tolist() # [vx, vy]
detections.data[SPEED_KEY_IN_SV_DETECTIONS][
tracker_id
] = speed_m_s # Scalar
detections.data[SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS][
tracker_id
] = smoothed_velocity_m_s.tolist() # [vx, vy]
detections.data["smoothed_speed"][tracker_id] = smoothed_speed_m_s # Scalar
detections.data[SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS][
tracker_id
] = smoothed_speed_m_s # Scalar

return {OUTPUT_KEY: detections}
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
from enum import Enum

from inference.core.workflows.core_steps.analytics.line_counter.v2 import (
DETECTIONS_IN_OUT_PARAM,
)
from inference.core.workflows.core_steps.analytics.velocity.v1 import (
SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS,
SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS,
SPEED_KEY_IN_SV_DETECTIONS,
VELOCITY_KEY_IN_SV_DETECTIONS,
)
from inference.core.workflows.execution_engine.constants import (
BOUNDING_RECT_ANGLE_KEY_IN_SV_DETECTIONS,
BOUNDING_RECT_HEIGHT_KEY_IN_SV_DETECTIONS,
BOUNDING_RECT_RECT_KEY_IN_SV_DETECTIONS,
BOUNDING_RECT_WIDTH_KEY_IN_SV_DETECTIONS,
IMAGE_DIMENSIONS_KEY,
KEYPOINTS_XY_KEY_IN_SV_DETECTIONS,
PATH_DEVIATION_KEY_IN_SV_DETECTIONS,
PREDICTION_TYPE_KEY,
TIME_IN_ZONE_KEY_IN_SV_DETECTIONS,
)


class NumberCastingMode(Enum):
INT = "int"
Expand Down Expand Up @@ -40,6 +61,21 @@ class DetectionsProperty(Enum):
TOP_RIGHT = "top_right"
BOTTOM_LEFT = "bottom_left"
BOTTOM_RIGHT = "bottom_right"
IN_OUT = DETECTIONS_IN_OUT_PARAM
PATH_DEVIATION = PATH_DEVIATION_KEY_IN_SV_DETECTIONS
TIME_IN_ZONE = TIME_IN_ZONE_KEY_IN_SV_DETECTIONS
TRACKER_ID = "tracker_id"
VELOCITY = VELOCITY_KEY_IN_SV_DETECTIONS
SPEED = SPEED_KEY_IN_SV_DETECTIONS
SMOOTHED_VELOCITY = SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS
SMOOTHED_SPEED = SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS
DIMENSIONS = IMAGE_DIMENSIONS_KEY
PREDICTION_TYPE = PREDICTION_TYPE_KEY
KEYPOINTS_XY = KEYPOINTS_XY_KEY_IN_SV_DETECTIONS
BOUNDING_RECT = BOUNDING_RECT_RECT_KEY_IN_SV_DETECTIONS
BOUNDING_RECT_WIDTH = BOUNDING_RECT_WIDTH_KEY_IN_SV_DETECTIONS
BOUNDING_RECT_HEIGHT = BOUNDING_RECT_HEIGHT_KEY_IN_SV_DETECTIONS
BOUNDING_RECT_ANGLE = BOUNDING_RECT_ANGLE_KEY_IN_SV_DETECTIONS


class DetectionsSortProperties(Enum):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
from typing import Any

from inference.core.workflows.core_steps.analytics.line_counter.v2 import (
DETECTIONS_IN_OUT_PARAM,
)
from inference.core.workflows.core_steps.analytics.velocity.v1 import (
SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS,
SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS,
SPEED_KEY_IN_SV_DETECTIONS,
VELOCITY_KEY_IN_SV_DETECTIONS,
)
from inference.core.workflows.core_steps.common.query_language.entities.enums import (
DetectionsProperty,
)
Expand All @@ -9,6 +18,17 @@
from inference.core.workflows.core_steps.common.query_language.operations.utils import (
safe_stringify,
)
from inference.core.workflows.execution_engine.constants import (
BOUNDING_RECT_ANGLE_KEY_IN_SV_DETECTIONS,
BOUNDING_RECT_HEIGHT_KEY_IN_SV_DETECTIONS,
BOUNDING_RECT_RECT_KEY_IN_SV_DETECTIONS,
BOUNDING_RECT_WIDTH_KEY_IN_SV_DETECTIONS,
IMAGE_DIMENSIONS_KEY,
KEYPOINTS_XY_KEY_IN_SV_DETECTIONS,
PATH_DEVIATION_KEY_IN_SV_DETECTIONS,
PREDICTION_TYPE_KEY,
TIME_IN_ZONE_KEY_IN_SV_DETECTIONS,
)

DETECTION_PROPERTY_EXTRACTION = {
DetectionsProperty.X_MIN: lambda x: x[0][0].item(),
Expand All @@ -29,6 +49,39 @@
DetectionsProperty.TOP_RIGHT: lambda xyxy: (xyxy[2], xyxy[1]),
DetectionsProperty.BOTTOM_LEFT: lambda xyxy: (xyxy[0], xyxy[3]),
DetectionsProperty.BOTTOM_RIGHT: lambda xyxy: (xyxy[2], xyxy[3]),
DetectionsProperty.IN_OUT: lambda x: x[5].get(DETECTIONS_IN_OUT_PARAM),
DetectionsProperty.PATH_DEVIATION: lambda x: x[5].get(
PATH_DEVIATION_KEY_IN_SV_DETECTIONS
),
DetectionsProperty.TIME_IN_ZONE: lambda x: x[5].get(
TIME_IN_ZONE_KEY_IN_SV_DETECTIONS
),
DetectionsProperty.TRACKER_ID: lambda x: x[5].get("tracker_id"),
DetectionsProperty.VELOCITY: lambda x: x[5].get(VELOCITY_KEY_IN_SV_DETECTIONS),
DetectionsProperty.SPEED: lambda x: x[5].get(SPEED_KEY_IN_SV_DETECTIONS),
DetectionsProperty.SMOOTHED_VELOCITY: lambda x: x[5].get(
SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS
),
DetectionsProperty.SMOOTHED_SPEED: lambda x: x[5].get(
SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS
),
DetectionsProperty.DIMENSIONS: lambda x: x[5].get(IMAGE_DIMENSIONS_KEY),
DetectionsProperty.PREDICTION_TYPE: lambda x: x[5].get(PREDICTION_TYPE_KEY),
DetectionsProperty.KEYPOINTS_XY: lambda x: x[5].get(
KEYPOINTS_XY_KEY_IN_SV_DETECTIONS
),
DetectionsProperty.BOUNDING_RECT: lambda x: x[5].get(
BOUNDING_RECT_RECT_KEY_IN_SV_DETECTIONS
),
DetectionsProperty.BOUNDING_RECT_WIDTH: lambda x: x[5].get(
BOUNDING_RECT_WIDTH_KEY_IN_SV_DETECTIONS
),
DetectionsProperty.BOUNDING_RECT_HEIGHT: lambda x: x[5].get(
BOUNDING_RECT_HEIGHT_KEY_IN_SV_DETECTIONS
),
DetectionsProperty.BOUNDING_RECT_ANGLE: lambda x: x[5].get(
BOUNDING_RECT_ANGLE_KEY_IN_SV_DETECTIONS
),
}


Expand Down

0 comments on commit f284753

Please sign in to comment.