Skip to content

Commit

Permalink
Merge pull request #1029 from roboflow/feat/extend-dynamic-zones-to-r…
Browse files Browse the repository at this point in the history
…eturn-updated-detections

Extend dynamic_zones block to expose updated detections as extra output
  • Loading branch information
grzegorz-roboflow authored Feb 14, 2025
2 parents 260bc34 + 9381679 commit 1cd951d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion inference/core/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.38.0rc1"
__version__ = "0.38.0"


if __name__ == "__main__":
Expand Down
3 changes: 3 additions & 0 deletions inference/core/workflows/core_steps/common/deserializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
PARENT_ID_KEY,
PATH_DEVIATION_KEY_IN_INFERENCE_RESPONSE,
PATH_DEVIATION_KEY_IN_SV_DETECTIONS,
POLYGON_KEY_IN_INFERENCE_RESPONSE,
POLYGON_KEY_IN_SV_DETECTIONS,
TIME_IN_ZONE_KEY_IN_INFERENCE_RESPONSE,
TIME_IN_ZONE_KEY_IN_SV_DETECTIONS,
)
Expand Down Expand Up @@ -166,6 +168,7 @@ def deserialize_detections_kind(
optional_elements_keys = [
(PATH_DEVIATION_KEY_IN_INFERENCE_RESPONSE, PATH_DEVIATION_KEY_IN_SV_DETECTIONS),
(TIME_IN_ZONE_KEY_IN_INFERENCE_RESPONSE, TIME_IN_ZONE_KEY_IN_SV_DETECTIONS),
(POLYGON_KEY_IN_INFERENCE_RESPONSE, POLYGON_KEY_IN_SV_DETECTIONS),
(
BOUNDING_RECT_ANGLE_KEY_IN_INFERENCE_RESPONSE,
BOUNDING_RECT_ANGLE_KEY_IN_SV_DETECTIONS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
IMAGE_DIMENSIONS_KEY,
KEYPOINTS_XY_KEY_IN_SV_DETECTIONS,
PATH_DEVIATION_KEY_IN_SV_DETECTIONS,
POLYGON_KEY_IN_SV_DETECTIONS,
PREDICTION_TYPE_KEY,
TIME_IN_ZONE_KEY_IN_SV_DETECTIONS,
)
Expand Down Expand Up @@ -63,6 +64,7 @@ class DetectionsProperty(Enum):
BOTTOM_RIGHT = "bottom_right"
IN_OUT = DETECTIONS_IN_OUT_PARAM
PATH_DEVIATION = PATH_DEVIATION_KEY_IN_SV_DETECTIONS
POLYGON = POLYGON_KEY_IN_SV_DETECTIONS
TIME_IN_ZONE = TIME_IN_ZONE_KEY_IN_SV_DETECTIONS
TRACKER_ID = "tracker_id"
VELOCITY = VELOCITY_KEY_IN_SV_DETECTIONS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
IMAGE_DIMENSIONS_KEY,
KEYPOINTS_XY_KEY_IN_SV_DETECTIONS,
PATH_DEVIATION_KEY_IN_SV_DETECTIONS,
POLYGON_KEY_IN_SV_DETECTIONS,
PREDICTION_TYPE_KEY,
TIME_IN_ZONE_KEY_IN_SV_DETECTIONS,
)
Expand Down Expand Up @@ -53,6 +54,7 @@
DetectionsProperty.PATH_DEVIATION: lambda x: x[5].get(
PATH_DEVIATION_KEY_IN_SV_DETECTIONS
),
DetectionsProperty.POLYGON: lambda x: x[5].get(POLYGON_KEY_IN_SV_DETECTIONS),
DetectionsProperty.TIME_IN_ZONE: lambda x: x[5].get(
TIME_IN_ZONE_KEY_IN_SV_DETECTIONS
),
Expand Down
6 changes: 6 additions & 0 deletions inference/core/workflows/core_steps/common/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
PATH_DEVIATION_KEY_IN_INFERENCE_RESPONSE,
PATH_DEVIATION_KEY_IN_SV_DETECTIONS,
POLYGON_KEY,
POLYGON_KEY_IN_INFERENCE_RESPONSE,
POLYGON_KEY_IN_SV_DETECTIONS,
TIME_IN_ZONE_KEY_IN_INFERENCE_RESPONSE,
TIME_IN_ZONE_KEY_IN_SV_DETECTIONS,
TRACKER_ID_KEY,
Expand Down Expand Up @@ -81,6 +83,10 @@ def serialise_sv_detections(detections: sv.Detections) -> dict:
detection_dict[TIME_IN_ZONE_KEY_IN_INFERENCE_RESPONSE] = data[
TIME_IN_ZONE_KEY_IN_SV_DETECTIONS
]
if POLYGON_KEY_IN_SV_DETECTIONS in data:
detection_dict[POLYGON_KEY_IN_INFERENCE_RESPONSE] = data[
POLYGON_KEY_IN_SV_DETECTIONS
]
if (
BOUNDING_RECT_ANGLE_KEY_IN_SV_DETECTIONS in data
and BOUNDING_RECT_RECT_KEY_IN_SV_DETECTIONS in data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import supervision as sv
from pydantic import ConfigDict, Field

from inference.core.workflows.execution_engine.constants import (
POLYGON_KEY_IN_SV_DETECTIONS,
)
from inference.core.workflows.execution_engine.entities.base import (
Batch,
OutputDefinition,
Expand All @@ -23,6 +26,7 @@
)

OUTPUT_KEY: str = "zones"
OUTPUT_KEY_DETECTIONS: str = "predictions"
TYPE: str = "roboflow_core/dynamic_zone@v1"
SHORT_DESCRIPTION = (
"Simplify polygons so they are geometrically convex "
Expand Down Expand Up @@ -81,6 +85,9 @@ def get_parameters_accepting_batches(cls) -> List[str]:
def describe_outputs(cls) -> List[OutputDefinition]:
return [
OutputDefinition(name=OUTPUT_KEY, kind=[LIST_OF_VALUES_KIND]),
OutputDefinition(
name=OUTPUT_KEY_DETECTIONS, kind=[INSTANCE_SEGMENTATION_PREDICTION_KIND]
),
]

@classmethod
Expand Down Expand Up @@ -161,20 +168,40 @@ def run(
result.append({OUTPUT_KEY: None})
continue
simplified_polygons = []
updated_detections = []
if detections.mask is None:
result.append({OUTPUT_KEY: []})
continue
for mask in detections.mask:
for i, mask in enumerate(detections.mask):
# copy
updated_detection = detections[i]

simplified_polygon = calculate_simplified_polygon(
mask=mask,
required_number_of_vertices=required_number_of_vertices,
)
if len(simplified_polygon) != required_number_of_vertices:
continue
simplified_polygon = scale_polygon(
polygon=simplified_polygon,
scale=scale_ratio,
updated_detection[POLYGON_KEY_IN_SV_DETECTIONS] = np.array(
[simplified_polygon]
)
simplified_polygons.append(simplified_polygon)
result.append({OUTPUT_KEY: simplified_polygons})
if len(simplified_polygon) == required_number_of_vertices:
simplified_polygon = scale_polygon(
polygon=simplified_polygon,
scale=scale_ratio,
)
simplified_polygons.append(simplified_polygon)
updated_detection.mask = np.array(
[
sv.polygon_to_mask(
polygon=simplified_polygon,
resolution_wh=mask.shape,
)
]
)
updated_detections.append(updated_detection)
result.append(
{
OUTPUT_KEY: simplified_polygons,
OUTPUT_KEY_DETECTIONS: sv.Detections.merge(updated_detections),
}
)
return result
2 changes: 2 additions & 0 deletions inference/core/workflows/execution_engine/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
BOUNDING_RECT_ANGLE_KEY_IN_INFERENCE_RESPONSE = "angle"
PATH_DEVIATION_KEY_IN_SV_DETECTIONS = "path_deviation"
PATH_DEVIATION_KEY_IN_INFERENCE_RESPONSE = "path_deviation"
POLYGON_KEY_IN_SV_DETECTIONS = "polygon"
POLYGON_KEY_IN_INFERENCE_RESPONSE = "polygon"
TIME_IN_ZONE_KEY_IN_SV_DETECTIONS = "time_in_zone"
TIME_IN_ZONE_KEY_IN_INFERENCE_RESPONSE = "time_in_zone"
SCALING_RELATIVE_TO_PARENT_KEY = "scaling_relative_to_parent"
Expand Down

0 comments on commit 1cd951d

Please sign in to comment.