diff --git a/supervision/detection/annotate.py b/supervision/detection/annotate.py index 3b3e40da..17c78df3 100644 --- a/supervision/detection/annotate.py +++ b/supervision/detection/annotate.py @@ -37,6 +37,179 @@ class BoxAnnotator: self.text_thickness: int = text_thickness self.text_padding: int = text_padding + def annotate_advance( + self, + scene: np.ndarray, + detections: Detections, + labels: Optional[List[str]] = None, + skip_label: bool = False, + ): + """ + Draws bounding boxes on the frame using the detections provided. + + Args: + scene (np.ndarray): The image on which the bounding boxes will be drawn + detections (Detections): The detections for which the bounding boxes will be drawn + labels (Optional[List[str]]): An optional list of labels corresponding to each detection. If `labels` are not provided, corresponding `class_id` will be used as label. + skip_label (bool): Is set to `True`, skips bounding box label annotation. + Returns: + np.ndarray: The image with the bounding boxes drawn on it + + Example: + ```python + >>> import supervision as sv + + >>> classes = ['person', ...] + >>> image = ... + >>> detections = sv.Detections(...) + + >>> box_annotator = sv.BoxAnnotator() + >>> labels = [ + ... f"{classes[class_id]} {confidence:0.2f}" + ... for _, _, confidence, class_id, _ + ... in detections + ... ] + >>> annotated_frame = box_annotator.annotate( + ... scene=image.copy(), + ... detections=detections, + ... labels=labels + ... ) + ``` + """ + font = cv2.FONT_HERSHEY_SIMPLEX + overlay_img = np.zeros_like(scene, np.uint8) + alpha = 0.8 + + line_thickness = self.thickness + 2 + for i in range(len(detections)): + x1, y1, x2, y2 = detections.xyxy[i].astype(int) + class_id = ( + detections.class_id[i] if detections.class_id is not None else None + ) + idx = class_id if class_id is not None else i + color = ( + self.color.by_idx(idx) + if isinstance(self.color, ColorPalette) + else self.color + ) + + box_width = x2 - x1 + box_height = y2 - y1 + + cv2.line( + scene, + (x1, y1), + (x1 + int(0.2 * box_width), y1), + color.as_bgr(), + thickness=line_thickness, + ) + cv2.line( + scene, + (x2 - int(0.2 * box_width), y1), + (x2, y1), + color.as_bgr(), + thickness=line_thickness, + ) + + cv2.line( + scene, + (x1, y2), + (x1 + int(0.2 * box_width), y2), + color.as_bgr(), + thickness=line_thickness, + ) + cv2.line( + scene, + (x2 - int(0.2 * box_width), y2), + (x2, y2), + color.as_bgr(), + thickness=line_thickness, + ) + # + cv2.line( + scene, + (x1, y1), + (x1, y1 + int(0.2 * box_height)), + color.as_bgr(), + thickness=line_thickness, + ) + cv2.line( + scene, + (x2, y1 + int(0.2 * box_height)), + (x2, y1), + color.as_bgr(), + thickness=line_thickness, + ) + + cv2.line( + scene, + (x1, y2 - int(0.2 * box_height)), + (x1, y2), + color.as_bgr(), + thickness=line_thickness, + ) + cv2.line( + scene, + (x2, y2 - int(0.2 * box_height)), + (x2, y2), + color.as_bgr(), + thickness=line_thickness, + ) + + cv2.rectangle( + img=overlay_img, + pt1=(x1, y1), + pt2=(x2, y2), + color=color.as_bgr(), + thickness=-1, + ) + if skip_label: + continue + + text = ( + f"{class_id}" + if (labels is None or len(detections) != len(labels)) + else labels[i] + ) + + text_width, text_height = cv2.getTextSize( + text=text, + fontFace=font, + fontScale=self.text_scale, + thickness=self.text_thickness, + )[0] + + text_x = x1 + self.text_padding + text_y = y1 - self.text_padding + + text_background_x1 = x1 + text_background_y1 = y1 - 2 * self.text_padding - text_height + + text_background_x2 = x1 + 2 * self.text_padding + text_width + text_background_y2 = y1 + + cv2.rectangle( + img=scene, + pt1=(text_background_x1, text_background_y1), + pt2=(text_background_x2, text_background_y2), + color=color.as_bgr(), + thickness=cv2.FILLED, + ) + cv2.putText( + img=scene, + text=text, + org=(text_x, text_y), + fontFace=font, + fontScale=self.text_scale, + color=self.text_color.as_rgb(), + thickness=self.text_thickness, + lineType=cv2.LINE_AA, + ) + + mask = overlay_img.astype(bool) + scene[mask] = cv2.addWeighted(scene, alpha, overlay_img, 1 - alpha, 0)[mask] + return scene + def annotate( self, scene: np.ndarray,