From 96d8badf1a697e17ba736084be2f92bac070f482 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Mon, 27 Feb 2023 17:35:07 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=A5=20add=20from=5Fcoco=5Fannotations?= =?UTF-8?q?=20support?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/detection/core.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 88d8ccdd..e768def5 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -157,6 +157,17 @@ class Detections: class_id=detectron2_results["instances"].pred_classes.cpu().numpy().astype(int) ) + @classmethod + def from_coco_annotations(cls, coco_annotation: dict): + xyxy, class_id = [], [] + + for annotation in coco_annotation: + x_min, y_min, width, height = annotation['bbox'] + xyxy.append([x_min, y_min, x_min + width, y_min + height]) + class_id.append(annotation['category_id']) + + return cls(xyxy=np.array(xyxy), class_id=np.array(class_id)) + def filter(self, mask: np.ndarray, inplace: bool = False) -> Optional[Detections]: """ Filter the detections by applying a mask. @@ -225,6 +236,7 @@ class Detections: ) def with_nms(self, threshold: float = 0.5) -> Detections: + assert (self.confidence is not None, f"Detections confidence must be given for NMS to be executed.") indices = non_max_suppression(self.xyxy, self.confidence, threshold=threshold) return self[indices]