Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Optimize annotate method in BoxAnnotator for performance and scalability #1776

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions supervision/annotators/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,26 @@ def annotate(
supervision-annotator-examples/bounding-box-annotator-example-purple.png)
"""
assert isinstance(scene, np.ndarray)
for detection_idx in range(len(detections)):
x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int)
color = resolve_color(
# Precompute colors for all detections
color_lookup = (
self.color_lookup if custom_color_lookup is None else custom_color_lookup
)
precomputed_colors = [
resolve_color(
color=self.color,
detections=detections,
detection_idx=detection_idx,
color_lookup=self.color_lookup
if custom_color_lookup is None
else custom_color_lookup,
)
cv2.rectangle(
img=scene,
pt1=(x1, y1),
pt2=(x2, y2),
color=color.as_bgr(),
thickness=self.thickness,
detection_idx=i,
color_lookup=color_lookup,
)
for i in range(len(detections))
]

# Use NumPy to handle bounding box coordinates
bounding_boxes = detections.xyxy.astype(int)

# Draw all bounding boxes
for (x1, y1, x2, y2), color in zip(bounding_boxes, precomputed_colors):
cv2.rectangle(scene, (x1, y1), (x2, y2), color.as_bgr(), self.thickness)
return scene


Expand Down