Skip to content

Commit f48c631

Browse files
authored
Merge pull request #1898 from roboflow/motion-detection-input-parameter
fix: allow workflow input selectors for motion detection parameters
2 parents 1602dae + 38bdd33 commit f48c631

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import cv2
55
import numpy as np
66
import supervision as sv
7-
from pydantic import AliasChoices, ConfigDict, Field
7+
from pydantic import AliasChoices, ConfigDict, Field, PositiveInt
88
from shapely.geometry import Polygon
99

1010
from inference.core.workflows.execution_engine.entities.base import (
@@ -90,34 +90,32 @@ class MotionDetectionManifest(WorkflowBlockManifest):
9090
validation_alias=AliasChoices("image", "images"),
9191
)
9292

93-
minimum_contour_area: Union[Selector(kind=[INTEGER_KIND]), int] = Field(
93+
minimum_contour_area: Union[PositiveInt, Selector(kind=[INTEGER_KIND])] = Field(
9494
title="Minimum Contour Area",
9595
description="Minimum area in square pixels for a motion region to be detected. Contours smaller than this threshold are filtered out to ignore noise, small shadows, or minor pixel variations. Lower values increase sensitivity but may detect more false positives (e.g., 100 for very sensitive detection, 500 for only large objects). Default is 200 square pixels.",
96-
gt=0,
9796
examples=[200, 100, 500],
9897
default=200,
9998
)
10099

101-
morphological_kernel_size: Union[Selector(kind=[INTEGER_KIND]), int] = Field(
102-
title="Morphological Kernel Size",
103-
description="Size of the morphological kernel in pixels used to combine nearby motion regions and filter noise. Larger values merge more distant motion regions into single contours but may also merge separate objects. Smaller values preserve more detail but may leave fragmented detections. The kernel uses an elliptical shape. Default is 3 pixels.",
104-
gt=0,
105-
examples=[3, 5, 7],
106-
default=3,
100+
morphological_kernel_size: Union[PositiveInt, Selector(kind=[INTEGER_KIND])] = (
101+
Field(
102+
title="Morphological Kernel Size",
103+
description="Size of the morphological kernel in pixels used to combine nearby motion regions and filter noise. Larger values merge more distant motion regions into single contours but may also merge separate objects. Smaller values preserve more detail but may leave fragmented detections. The kernel uses an elliptical shape. Default is 3 pixels.",
104+
examples=[3, 5, 7],
105+
default=3,
106+
)
107107
)
108108

109-
threshold: Union[Selector(kind=[INTEGER_KIND]), int] = Field(
109+
threshold: Union[PositiveInt, Selector(kind=[INTEGER_KIND])] = Field(
110110
title="Threshold",
111111
description="Threshold value for the squared Mahalanobis distance used by the MOG2 background subtraction algorithm. Controls sensitivity to motion - smaller values increase sensitivity (detect smaller changes) but may produce more false positives, larger values decrease sensitivity (only detect significant changes) but may miss subtle motion. Recommended range is 8-32. Default is 16.",
112-
gt=0,
113112
examples=[16, 8, 24, 32],
114113
default=16,
115114
)
116115

117-
history: Union[Selector(kind=[INTEGER_KIND]), int] = Field(
116+
history: Union[PositiveInt, Selector(kind=[INTEGER_KIND])] = Field(
118117
title="History",
119118
description="Number of previous frames used to build the background model. Controls how quickly the background adapts to changes - larger values (e.g., 50-100) create a more stable background model that's less sensitive to temporary changes but adapts slowly to permanent background changes. Smaller values (e.g., 10-20) allow faster adaptation but may treat moving objects as background if they stop moving. Default is 30 frames.",
120-
gt=0,
121119
examples=[30, 50, 100],
122120
default=30,
123121
)

tests/workflows/unit_tests/core_steps/classical_cv/test_motion_detection.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,29 @@ def test_motion_detection_block_with_list_detection_zone() -> None:
465465
assert isinstance(output["detections"], sv.Detections)
466466

467467

468+
def test_motion_detection_manifest_accepts_workflow_input_selectors() -> None:
469+
"""Test that manifest accepts workflow input selectors for integer parameters."""
470+
# given
471+
data = {
472+
"type": "roboflow_core/motion_detection@v1",
473+
"name": "motion_detector",
474+
"image": "$inputs.image",
475+
"minimum_contour_area": "$inputs.min_contour_area",
476+
"morphological_kernel_size": "$inputs.kernel_size",
477+
"threshold": "$inputs.threshold",
478+
"history": "$inputs.history",
479+
}
480+
481+
# when
482+
result = MotionDetectionManifest.model_validate(data)
483+
484+
# then
485+
assert result.minimum_contour_area == "$inputs.min_contour_area"
486+
assert result.morphological_kernel_size == "$inputs.kernel_size"
487+
assert result.threshold == "$inputs.threshold"
488+
assert result.history == "$inputs.history"
489+
490+
468491
def test_motion_detection_block_changes_threshold() -> None:
469492
"""Test that the background subtractor is only created once (on first run)."""
470493
# given

0 commit comments

Comments
 (0)