From 82437dc40598f9c683ced90db3430374bcc85d52 Mon Sep 17 00:00:00 2001 From: Suyash Wattamwar Date: Tue, 14 Jan 2025 11:43:17 -0500 Subject: [PATCH 1/2] Refactor - Optimize annotate method in BoxAnnotator for performance and scalability --- supervision/annotators/core.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index a2b2ed565..c07aa082d 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -104,23 +104,24 @@ 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 From 702030b96063199f654622dfbec1641465dd0875 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Jan 2025 16:47:33 +0000 Subject: [PATCH 2/2] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20?= =?UTF-8?q?format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/annotators/core.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index c07aa082d..456a2b942 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -105,7 +105,9 @@ def annotate( """ assert isinstance(scene, np.ndarray) # Precompute colors for all detections - color_lookup = self.color_lookup if custom_color_lookup is None else custom_color_lookup + color_lookup = ( + self.color_lookup if custom_color_lookup is None else custom_color_lookup + ) precomputed_colors = [ resolve_color( color=self.color, @@ -115,10 +117,10 @@ def annotate( ) 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)