Skip to content

Commit f284753

Browse files
Merge pull request #985 from roboflow/extend-DetectionsProperty-to-handle-all-blocks-custom-data-keys
Extend query_language DetectionsProperty to cover sv.Detections data keys produced by blocks
2 parents 57edf77 + 67943be commit f284753

File tree

3 files changed

+111
-12
lines changed
  • inference/core/workflows/core_steps

3 files changed

+111
-12
lines changed

inference/core/workflows/core_steps/analytics/velocity/v1.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
Note: due to perspective and camera distortions calculated velocity will be different depending on object position in relation to the camera.
3535
3636
"""
37+
VELOCITY_KEY_IN_SV_DETECTIONS = "velocity"
38+
SPEED_KEY_IN_SV_DETECTIONS = "speed"
39+
SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS = "smoothed_velocity"
40+
SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS = "smoothed_speed"
3741

3842

3943
class VelocityManifest(WorkflowBlockManifest):
@@ -197,21 +201,27 @@ def run(
197201
detections.data = {}
198202

199203
# Initialize dictionaries if not present
200-
if "velocity" not in detections.data:
201-
detections.data["velocity"] = {}
202-
if "speed" not in detections.data:
203-
detections.data["speed"] = {}
204-
if "smoothed_velocity" not in detections.data:
205-
detections.data["smoothed_velocity"] = {}
206-
if "smoothed_speed" not in detections.data:
207-
detections.data["smoothed_speed"] = {}
204+
if VELOCITY_KEY_IN_SV_DETECTIONS not in detections.data:
205+
detections.data[VELOCITY_KEY_IN_SV_DETECTIONS] = {}
206+
if SPEED_KEY_IN_SV_DETECTIONS not in detections.data:
207+
detections.data[SPEED_KEY_IN_SV_DETECTIONS] = {}
208+
if SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS not in detections.data:
209+
detections.data[SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS] = {}
210+
if SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS not in detections.data:
211+
detections.data[SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS] = {}
208212

209213
# Assign velocity data to the corresponding tracker_id
210-
detections.data["velocity"][tracker_id] = velocity_m_s.tolist() # [vx, vy]
211-
detections.data["speed"][tracker_id] = speed_m_s # Scalar
212-
detections.data["smoothed_velocity"][
214+
detections.data[VELOCITY_KEY_IN_SV_DETECTIONS][
215+
tracker_id
216+
] = velocity_m_s.tolist() # [vx, vy]
217+
detections.data[SPEED_KEY_IN_SV_DETECTIONS][
218+
tracker_id
219+
] = speed_m_s # Scalar
220+
detections.data[SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS][
213221
tracker_id
214222
] = smoothed_velocity_m_s.tolist() # [vx, vy]
215-
detections.data["smoothed_speed"][tracker_id] = smoothed_speed_m_s # Scalar
223+
detections.data[SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS][
224+
tracker_id
225+
] = smoothed_speed_m_s # Scalar
216226

217227
return {OUTPUT_KEY: detections}

inference/core/workflows/core_steps/common/query_language/entities/enums.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
from enum import Enum
22

3+
from inference.core.workflows.core_steps.analytics.line_counter.v2 import (
4+
DETECTIONS_IN_OUT_PARAM,
5+
)
6+
from inference.core.workflows.core_steps.analytics.velocity.v1 import (
7+
SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS,
8+
SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS,
9+
SPEED_KEY_IN_SV_DETECTIONS,
10+
VELOCITY_KEY_IN_SV_DETECTIONS,
11+
)
12+
from inference.core.workflows.execution_engine.constants import (
13+
BOUNDING_RECT_ANGLE_KEY_IN_SV_DETECTIONS,
14+
BOUNDING_RECT_HEIGHT_KEY_IN_SV_DETECTIONS,
15+
BOUNDING_RECT_RECT_KEY_IN_SV_DETECTIONS,
16+
BOUNDING_RECT_WIDTH_KEY_IN_SV_DETECTIONS,
17+
IMAGE_DIMENSIONS_KEY,
18+
KEYPOINTS_XY_KEY_IN_SV_DETECTIONS,
19+
PATH_DEVIATION_KEY_IN_SV_DETECTIONS,
20+
PREDICTION_TYPE_KEY,
21+
TIME_IN_ZONE_KEY_IN_SV_DETECTIONS,
22+
)
23+
324

425
class NumberCastingMode(Enum):
526
INT = "int"
@@ -40,6 +61,21 @@ class DetectionsProperty(Enum):
4061
TOP_RIGHT = "top_right"
4162
BOTTOM_LEFT = "bottom_left"
4263
BOTTOM_RIGHT = "bottom_right"
64+
IN_OUT = DETECTIONS_IN_OUT_PARAM
65+
PATH_DEVIATION = PATH_DEVIATION_KEY_IN_SV_DETECTIONS
66+
TIME_IN_ZONE = TIME_IN_ZONE_KEY_IN_SV_DETECTIONS
67+
TRACKER_ID = "tracker_id"
68+
VELOCITY = VELOCITY_KEY_IN_SV_DETECTIONS
69+
SPEED = SPEED_KEY_IN_SV_DETECTIONS
70+
SMOOTHED_VELOCITY = SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS
71+
SMOOTHED_SPEED = SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS
72+
DIMENSIONS = IMAGE_DIMENSIONS_KEY
73+
PREDICTION_TYPE = PREDICTION_TYPE_KEY
74+
KEYPOINTS_XY = KEYPOINTS_XY_KEY_IN_SV_DETECTIONS
75+
BOUNDING_RECT = BOUNDING_RECT_RECT_KEY_IN_SV_DETECTIONS
76+
BOUNDING_RECT_WIDTH = BOUNDING_RECT_WIDTH_KEY_IN_SV_DETECTIONS
77+
BOUNDING_RECT_HEIGHT = BOUNDING_RECT_HEIGHT_KEY_IN_SV_DETECTIONS
78+
BOUNDING_RECT_ANGLE = BOUNDING_RECT_ANGLE_KEY_IN_SV_DETECTIONS
4379

4480

4581
class DetectionsSortProperties(Enum):

inference/core/workflows/core_steps/common/query_language/operations/detection/base.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
from typing import Any
22

3+
from inference.core.workflows.core_steps.analytics.line_counter.v2 import (
4+
DETECTIONS_IN_OUT_PARAM,
5+
)
6+
from inference.core.workflows.core_steps.analytics.velocity.v1 import (
7+
SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS,
8+
SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS,
9+
SPEED_KEY_IN_SV_DETECTIONS,
10+
VELOCITY_KEY_IN_SV_DETECTIONS,
11+
)
312
from inference.core.workflows.core_steps.common.query_language.entities.enums import (
413
DetectionsProperty,
514
)
@@ -9,6 +18,17 @@
918
from inference.core.workflows.core_steps.common.query_language.operations.utils import (
1019
safe_stringify,
1120
)
21+
from inference.core.workflows.execution_engine.constants import (
22+
BOUNDING_RECT_ANGLE_KEY_IN_SV_DETECTIONS,
23+
BOUNDING_RECT_HEIGHT_KEY_IN_SV_DETECTIONS,
24+
BOUNDING_RECT_RECT_KEY_IN_SV_DETECTIONS,
25+
BOUNDING_RECT_WIDTH_KEY_IN_SV_DETECTIONS,
26+
IMAGE_DIMENSIONS_KEY,
27+
KEYPOINTS_XY_KEY_IN_SV_DETECTIONS,
28+
PATH_DEVIATION_KEY_IN_SV_DETECTIONS,
29+
PREDICTION_TYPE_KEY,
30+
TIME_IN_ZONE_KEY_IN_SV_DETECTIONS,
31+
)
1232

1333
DETECTION_PROPERTY_EXTRACTION = {
1434
DetectionsProperty.X_MIN: lambda x: x[0][0].item(),
@@ -29,6 +49,39 @@
2949
DetectionsProperty.TOP_RIGHT: lambda xyxy: (xyxy[2], xyxy[1]),
3050
DetectionsProperty.BOTTOM_LEFT: lambda xyxy: (xyxy[0], xyxy[3]),
3151
DetectionsProperty.BOTTOM_RIGHT: lambda xyxy: (xyxy[2], xyxy[3]),
52+
DetectionsProperty.IN_OUT: lambda x: x[5].get(DETECTIONS_IN_OUT_PARAM),
53+
DetectionsProperty.PATH_DEVIATION: lambda x: x[5].get(
54+
PATH_DEVIATION_KEY_IN_SV_DETECTIONS
55+
),
56+
DetectionsProperty.TIME_IN_ZONE: lambda x: x[5].get(
57+
TIME_IN_ZONE_KEY_IN_SV_DETECTIONS
58+
),
59+
DetectionsProperty.TRACKER_ID: lambda x: x[5].get("tracker_id"),
60+
DetectionsProperty.VELOCITY: lambda x: x[5].get(VELOCITY_KEY_IN_SV_DETECTIONS),
61+
DetectionsProperty.SPEED: lambda x: x[5].get(SPEED_KEY_IN_SV_DETECTIONS),
62+
DetectionsProperty.SMOOTHED_VELOCITY: lambda x: x[5].get(
63+
SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS
64+
),
65+
DetectionsProperty.SMOOTHED_SPEED: lambda x: x[5].get(
66+
SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS
67+
),
68+
DetectionsProperty.DIMENSIONS: lambda x: x[5].get(IMAGE_DIMENSIONS_KEY),
69+
DetectionsProperty.PREDICTION_TYPE: lambda x: x[5].get(PREDICTION_TYPE_KEY),
70+
DetectionsProperty.KEYPOINTS_XY: lambda x: x[5].get(
71+
KEYPOINTS_XY_KEY_IN_SV_DETECTIONS
72+
),
73+
DetectionsProperty.BOUNDING_RECT: lambda x: x[5].get(
74+
BOUNDING_RECT_RECT_KEY_IN_SV_DETECTIONS
75+
),
76+
DetectionsProperty.BOUNDING_RECT_WIDTH: lambda x: x[5].get(
77+
BOUNDING_RECT_WIDTH_KEY_IN_SV_DETECTIONS
78+
),
79+
DetectionsProperty.BOUNDING_RECT_HEIGHT: lambda x: x[5].get(
80+
BOUNDING_RECT_HEIGHT_KEY_IN_SV_DETECTIONS
81+
),
82+
DetectionsProperty.BOUNDING_RECT_ANGLE: lambda x: x[5].get(
83+
BOUNDING_RECT_ANGLE_KEY_IN_SV_DETECTIONS
84+
),
3285
}
3386

3487

0 commit comments

Comments
 (0)