From 9c062aac29722f94882cd436290e6b3e1ddd21ef Mon Sep 17 00:00:00 2001 From: hd Date: Mon, 3 Jul 2023 17:28:40 +0200 Subject: [PATCH 1/7] Support for openmmlab detections --- supervision/detection/core.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index b423aaad..0d47af29 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -228,6 +228,39 @@ class Detections: class_id=yolo_nas_results.prediction.labels.astype(int), ) + @classmethod + def from_mmdet(cls, mmdet_results) -> Detections: + """ + Creates a Detections instance from a [mmdetection](https://github.com/open-mmlab/mmdetection) inference result. + Also supported for [mmyolo](https://github.com/open-mmlab/mmyolo) + + Args: + mmdet_results (mmdet.structures.DetDataSample): The output Results instance from MMDetection + + Returns: + Detections: A new Detections object. + + Example: + ```python + >>> import cv2 + >>> import supervision as sv + >>> from mmdet.apis import inference_detector, init_detector + >>> from mmengine.config import Config + + >>> image = cv2.imread(SOURCE_IMAGE_PATH) + >>> config = Config.fromfile(CONFIG) + >>> model = init_detector(config, CHECKPOINT, device=device, cfg_options={}) + >>> img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + >>> mmdet_result = inference_detector(model, img_rgb) + >>> detections = sv.Detections.from_mmdet(mmdet_result) + ``` + """ + return cls( + xyxy=mmdet_results.pred_instances.bboxes.xyxy.cpu().numpy(), + confidence=mmdet_results.pred_instances.scores.cpu().numpy(), + class_id=mmdet_results.pred_instances.labels.cpu().numpy().astype(int) + ) + @classmethod def from_transformers(cls, transformers_results: dict) -> Detections: """ From ebfaee22eb6dabb1c9dd4998bcb93c396c4400ac Mon Sep 17 00:00:00 2001 From: hd Date: Mon, 3 Jul 2023 17:30:53 +0200 Subject: [PATCH 2/7] updated codebase --- supervision/detection/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 0d47af29..78ce1be2 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -229,7 +229,7 @@ class Detections: ) @classmethod - def from_mmdet(cls, mmdet_results) -> Detections: + def from_mmdetection(cls, mmdet_results) -> Detections: """ Creates a Detections instance from a [mmdetection](https://github.com/open-mmlab/mmdetection) inference result. Also supported for [mmyolo](https://github.com/open-mmlab/mmyolo) @@ -258,7 +258,7 @@ class Detections: return cls( xyxy=mmdet_results.pred_instances.bboxes.xyxy.cpu().numpy(), confidence=mmdet_results.pred_instances.scores.cpu().numpy(), - class_id=mmdet_results.pred_instances.labels.cpu().numpy().astype(int) + class_id=mmdet_results.pred_instances.labels.cpu().numpy().astype(int), ) @classmethod From a50e47e7c07b56b21283427c4064dda9623079cb Mon Sep 17 00:00:00 2001 From: hd Date: Wed, 5 Jul 2023 09:20:50 +0200 Subject: [PATCH 3/7] tracked detection method added along with trackstorage --- supervision/detection/core.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 78ce1be2..0b55d173 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -244,21 +244,17 @@ class Detections: ```python >>> import cv2 >>> import supervision as sv - >>> from mmdet.apis import inference_detector, init_detector - >>> from mmengine.config import Config + >>> from mmdet.apis import DetInferencer - >>> image = cv2.imread(SOURCE_IMAGE_PATH) - >>> config = Config.fromfile(CONFIG) - >>> model = init_detector(config, CHECKPOINT, device=device, cfg_options={}) - >>> img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) - >>> mmdet_result = inference_detector(model, img_rgb) + >>> inferencer = DetInferencer(model_name, checkpoint, device) + >>> mmdet_result = inferencer(SOURCE_IMAGE_PATH, out_dir='./output')["predictions"][0] >>> detections = sv.Detections.from_mmdet(mmdet_result) ``` """ return cls( - xyxy=mmdet_results.pred_instances.bboxes.xyxy.cpu().numpy(), - confidence=mmdet_results.pred_instances.scores.cpu().numpy(), - class_id=mmdet_results.pred_instances.labels.cpu().numpy().astype(int), + xyxy=np.asarray(mmdet_results["bboxes"]), + confidence=np.asarray(mmdet_results["scores"]), + class_id=np.asarray(mmdet_results["labels"], dtype=int), ) @classmethod From 5291706bc19ce858d1bf52a41ae6d5093f0037dc Mon Sep 17 00:00:00 2001 From: hd Date: Wed, 5 Jul 2023 09:59:16 +0200 Subject: [PATCH 4/7] revert to original method --- supervision/detection/core.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 0b55d173..f48f8380 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -252,9 +252,9 @@ class Detections: ``` """ return cls( - xyxy=np.asarray(mmdet_results["bboxes"]), - confidence=np.asarray(mmdet_results["scores"]), - class_id=np.asarray(mmdet_results["labels"], dtype=int), + xyxy=mmdet_results.predictions.bboxes.xyxy.cpu().numpy(), + confidence=mmdet_results.predictions.scores.cpu().numpy(), + class_id=mmdet_results.predictions.labels.cpu().numpy().astype(int), ) @classmethod From 597c776c39ff43c3a08731f8ffd57523c0f7a838 Mon Sep 17 00:00:00 2001 From: hd Date: Wed, 5 Jul 2023 10:06:23 +0200 Subject: [PATCH 5/7] Fixing typo in variable --- supervision/detection/core.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index f48f8380..1c845dd4 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -252,9 +252,9 @@ class Detections: ``` """ return cls( - xyxy=mmdet_results.predictions.bboxes.xyxy.cpu().numpy(), - confidence=mmdet_results.predictions.scores.cpu().numpy(), - class_id=mmdet_results.predictions.labels.cpu().numpy().astype(int), + xyxy=mmdet_results.pred_instances.bboxes.xyxy.cpu().numpy(), + confidence=mmdet_results.pred_instances.scores.cpu().numpy(), + class_id=mmdet_results.pred_instances.labels.cpu().numpy().astype(int), ) @classmethod From 183ba1e8098275e14af5f49c91bc40f1b406db89 Mon Sep 17 00:00:00 2001 From: hd Date: Wed, 5 Jul 2023 10:09:04 +0200 Subject: [PATCH 6/7] Fixing conversion --- supervision/detection/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 1c845dd4..1714e18b 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -252,8 +252,8 @@ class Detections: ``` """ return cls( - xyxy=mmdet_results.pred_instances.bboxes.xyxy.cpu().numpy(), - confidence=mmdet_results.pred_instances.scores.cpu().numpy(), + xyxy=mmdet_results.pred_instances.bboxes.cpu().numpy(), + confidence=mmdet_results.pred_instances.cpu().numpy(), class_id=mmdet_results.pred_instances.labels.cpu().numpy().astype(int), ) From 3f78150a1dd83f16237626eb3a2d330aa395e9cf Mon Sep 17 00:00:00 2001 From: hd Date: Wed, 5 Jul 2023 10:11:55 +0200 Subject: [PATCH 7/7] Fixing docstrings and model instructions --- supervision/detection/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 1714e18b..9aab3630 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -247,13 +247,13 @@ class Detections: >>> from mmdet.apis import DetInferencer >>> inferencer = DetInferencer(model_name, checkpoint, device) - >>> mmdet_result = inferencer(SOURCE_IMAGE_PATH, out_dir='./output')["predictions"][0] + >>> mmdet_result = inferencer(SOURCE_IMAGE_PATH, out_dir='./output', return_datasample=True)["predictions"][0] >>> detections = sv.Detections.from_mmdet(mmdet_result) ``` """ return cls( xyxy=mmdet_results.pred_instances.bboxes.cpu().numpy(), - confidence=mmdet_results.pred_instances.cpu().numpy(), + confidence=mmdet_results.pred_instances.scores.cpu().numpy(), class_id=mmdet_results.pred_instances.labels.cpu().numpy().astype(int), )