Skip to content

Commit e73a2e5

Browse files
committed
Expose more annotation params for Roboflow processors
1 parent 210b64d commit e73a2e5

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

plugins/roboflow/vision_agents/plugins/roboflow/roboflow_cloud_processor.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,16 @@ async def on_detection_completed(event: roboflow.DetectionCompletedEvent):
7070
Example: ["person", "sports ball"]
7171
Verify that the classes a supported by the given model.
7272
Default - None (all classes are detected).
73+
client: optional custom instance of `inference_sdk.InferenceHTTPClient`.
7374
annotate: if True, annotate the detected objects with boxes and labels.
7475
Default - True.
7576
dim_background_factor: how much to dim the background around detected objects from 0 to 1.0.
7677
Effective only when annotate=True.
7778
Default - 0.0 (no dimming).
78-
client: optional custom instance of `inference_sdk.InferenceHTTPClient`.
79+
annotate_text_scale: annontation text scale. Default - 0.75.
80+
annotate_text_padding: annotation text padding. Default - 1.0.
81+
annotate_box_thickness: annotation box thickness. Default - 2.
82+
annotate_text_position: annotation text position. Default - `sv.Position.TOP_CENTER`.
7983
8084
Examples:
8185
Example usage:
@@ -104,10 +108,14 @@ def __init__(
104108
api_url: Optional[str] = None,
105109
conf_threshold: float = 0.5,
106110
fps: int = 5,
107-
annotate: bool = True,
108111
classes: Optional[list[str]] = None,
109-
dim_background_factor: float = 0.0,
110112
client: Optional[InferenceHTTPClient] = None,
113+
annotate: bool = True,
114+
dim_background_factor: float = 0.0,
115+
annotate_text_scale: float = 0.75,
116+
annotate_text_padding: int = 1,
117+
annotate_box_thickness: int = 2,
118+
annotate_text_position: sv.Position = sv.Position.TOP_CENTER,
111119
):
112120
super().__init__(interval=0, receive_audio=False, receive_video=True)
113121

@@ -139,6 +147,10 @@ def __init__(
139147
self.fps = fps
140148
self.dim_background_factor = max(0.0, dim_background_factor)
141149
self.annotate = annotate
150+
self._annotate_text_scale = annotate_text_scale
151+
self._annotate_text_padding = annotate_text_padding
152+
self._annotate_box_thickness = annotate_box_thickness
153+
self._annotate_text_position = annotate_text_position
142154

143155
self._events: Optional[EventManager] = None
144156
self._client.configure(
@@ -237,7 +249,14 @@ async def _process_frame(self, frame: av.VideoFrame):
237249
if self.annotate:
238250
# Annotate frame with detections
239251
annotated_image = annotate_image(
240-
image, detections, classes, dim_factor=self.dim_background_factor
252+
image,
253+
detections,
254+
classes,
255+
dim_factor=self.dim_background_factor,
256+
text_scale=self._annotate_text_scale,
257+
text_position=self._annotate_text_position,
258+
text_padding=self._annotate_text_padding,
259+
box_thickness=self._annotate_box_thickness,
241260
)
242261

243262
# Convert back to av.VideoFrame

plugins/roboflow/vision_agents/plugins/roboflow/roboflow_local_processor.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,17 @@ async def on_detection_completed(event: roboflow.DetectionCompletedEvent):
8989
Example: ["person", "sports ball"]
9090
Verify that the classes a supported by the given model.
9191
Default - None (all classes are detected).
92+
model: optional instance of `RFDETRModel` to be used for detections.
93+
Use it provide a model of choosing with custom parameters.
9294
annotate: if True, annotate the detected objects with boxes and labels.
9395
Default - True.
9496
dim_background_factor: how much to dim the background around detected objects from 0 to 1.0.
9597
Effective only when annotate=True.
9698
Default - 0.0 (no dimming).
97-
model: optional instance of `RFDETRModel` to be used for detections.
98-
Use it provide a model of choosing with custom parameters.
99+
annotate_text_scale: annontation text scale. Default - 0.75.
100+
annotate_text_padding: annotation text padding. Default - 1.0.
101+
annotate_box_thickness: annotation box thickness. Default - 2.
102+
annotate_text_position: annotation text position. Default - `sv.Position.TOP_CENTER`.
99103
"""
100104

101105
name = "roboflow_local"
@@ -106,9 +110,13 @@ def __init__(
106110
conf_threshold: float = 0.5,
107111
fps: int = 10,
108112
classes: Optional[list[str]] = None,
113+
model: Optional[RFDETR] = None,
109114
annotate: bool = True,
110115
dim_background_factor: float = 0.0,
111-
model: Optional[RFDETR] = None,
116+
annotate_text_scale: float = 0.75,
117+
annotate_text_padding: int = 1,
118+
annotate_box_thickness: int = 2,
119+
annotate_text_position: sv.Position = sv.Position.TOP_CENTER,
112120
):
113121
super().__init__(interval=0, receive_audio=False, receive_video=True)
114122

@@ -153,6 +161,10 @@ def __init__(
153161
fps=self.fps,
154162
max_queue_size=self.fps, # Buffer 1s of the video
155163
)
164+
self._annotate_text_scale = annotate_text_scale
165+
self._annotate_text_padding = annotate_text_padding
166+
self._annotate_box_thickness = annotate_box_thickness
167+
self._annotate_text_position = annotate_text_position
156168

157169
async def process_video(
158170
self,
@@ -267,6 +279,10 @@ async def _process_frame(self, frame: av.VideoFrame) -> None:
267279
detections,
268280
classes=self._model.class_names,
269281
dim_factor=self.dim_background_factor,
282+
text_scale=self._annotate_text_scale,
283+
text_position=self._annotate_text_position,
284+
text_padding=self._annotate_text_padding,
285+
box_thickness=self._annotate_box_thickness,
270286
)
271287
# Convert back to av.VideoFrame
272288
annotated_frame = av.VideoFrame.from_ndarray(annotated_image)

plugins/roboflow/vision_agents/plugins/roboflow/utils.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ def annotate_image(
1010
detections: sv.Detections,
1111
classes: dict[int, str],
1212
dim_factor: Optional[float] = None,
13+
text_scale: float = 0.75,
14+
text_padding: int = 1,
15+
box_thickness: int = 2,
16+
text_position: sv.Position = sv.Position.TOP_CENTER,
1317
) -> np.ndarray:
1418
"""
1519
Draw bounding boxes and labels on frame.
@@ -23,14 +27,16 @@ def annotate_image(
2327
cv2.rectangle(mask, (x1, y1), (x2, y2), 255, -1)
2428
image[mask == 0] = (image[mask == 0] * dim_factor).astype(np.uint8)
2529

26-
boxed_image = sv.BoxAnnotator(thickness=1).annotate(image.copy(), detections)
30+
boxed_image = sv.BoxAnnotator(thickness=box_thickness).annotate(
31+
image.copy(), detections
32+
)
2733
detected_class_ids: Iterable[int] = (
2834
detections.class_id if detections.class_id is not None else []
2935
)
3036
labels = [classes[class_id] for class_id in detected_class_ids]
3137
labeled_image = sv.LabelAnnotator(
32-
text_position=sv.Position.BOTTOM_CENTER,
33-
text_scale=0.25,
34-
text_padding=1,
38+
text_position=text_position,
39+
text_scale=text_scale,
40+
text_padding=text_padding,
3541
).annotate(boxed_image, detections, labels)
3642
return labeled_image

0 commit comments

Comments
 (0)