Skip to content

Commit d77ec3f

Browse files
Merge pull request #1313 from roboflow/feat/accept-static-reference-polygon-in-measurement-block
Accept static reference polygon as input in size measurement block
2 parents b0f6e31 + 61b5d75 commit d77ec3f

1 file changed

Lines changed: 36 additions & 18 deletions

File tree

  • inference/core/workflows/core_steps/classical_cv/size_measurement

inference/core/workflows/core_steps/classical_cv/size_measurement/v1.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,16 @@ class SizeMeasurementManifest(WorkflowBlockManifest):
8585
examples=["$segmentation.object_predictions"],
8686
)
8787

88-
reference_predictions: Selector(
89-
kind=[
90-
INSTANCE_SEGMENTATION_PREDICTION_KIND,
91-
OBJECT_DETECTION_PREDICTION_KIND,
92-
]
93-
) = Field(
88+
reference_predictions: Union[
89+
list,
90+
Selector(
91+
kind=[
92+
INSTANCE_SEGMENTATION_PREDICTION_KIND,
93+
OBJECT_DETECTION_PREDICTION_KIND,
94+
LIST_OF_VALUES_KIND,
95+
]
96+
),
97+
] = Field(
9498
description="Reference object used to calculate the dimensions of the specified objects. If multiple objects are provided, the highest confidence prediction will be used.",
9599
examples=["$segmentation.reference_predictions"],
96100
)
@@ -176,7 +180,7 @@ def compute_aligned_dimensions(contour: np.ndarray) -> Tuple[float, float]:
176180

177181
def get_detection_dimensions(
178182
detection: sv.Detections, index: int
179-
) -> Tuple[float, float]:
183+
) -> Tuple[Optional[float], Optional[float]]:
180184
"""
181185
Retrieve the width and height dimensions of a detected object in pixels.
182186
@@ -212,6 +216,15 @@ def get_detection_dimensions(
212216
h = bbox[3] - bbox[1]
213217
return float(w), float(h)
214218

219+
return None, None
220+
221+
222+
def get_polygon_dimensions(polygon: list) -> Tuple[Optional[float], Optional[float]]:
223+
polygon = np.array(polygon)
224+
if len(polygon) >= 3 and cv.contourArea(polygon) > 0:
225+
return compute_aligned_dimensions(polygon)
226+
return None, None
227+
215228

216229
def parse_reference_dimensions(
217230
reference_dimensions: Union[str, Tuple[float, float], List[float]],
@@ -241,26 +254,31 @@ def get_manifest(cls) -> Type[WorkflowBlockManifest]:
241254

242255
def run(
243256
self,
244-
reference_predictions: sv.Detections,
257+
reference_predictions: Union[list, sv.Detections],
245258
object_predictions: sv.Detections,
246259
reference_dimensions: Union[str, Tuple[float, float], List[float]],
247260
) -> BlockResult:
248261
ref_width_actual, ref_height_actual = parse_reference_dimensions(
249262
reference_dimensions
250263
)
251264

252-
if (
253-
reference_predictions.confidence is None
254-
or len(reference_predictions.confidence) == 0
255-
):
256-
return {OUTPUT_KEY: None}
265+
if hasattr(reference_predictions, "confidence"):
266+
if len(reference_predictions.confidence) == 0:
267+
return {OUTPUT_KEY: None}
257268

258-
ref_index = int(np.argmax(reference_predictions.confidence))
259-
ref_width_pixels, ref_height_pixels = get_detection_dimensions(
260-
reference_predictions, ref_index
261-
)
269+
ref_index = int(np.argmax(reference_predictions.confidence))
270+
ref_width_pixels, ref_height_pixels = get_detection_dimensions(
271+
reference_predictions, ref_index
272+
)
273+
elif isinstance(reference_predictions, list):
274+
if len(reference_predictions) < 2:
275+
return {OUTPUT_KEY: None}
276+
277+
ref_width_pixels, ref_height_pixels = get_polygon_dimensions(
278+
reference_predictions
279+
)
262280

263-
if ref_width_pixels <= 0 or ref_height_pixels <= 0:
281+
if not ref_width_pixels or not ref_height_pixels:
264282
return {OUTPUT_KEY: None}
265283

266284
width_scale = ref_width_actual / ref_width_pixels

0 commit comments

Comments
 (0)