From c67078b53c8a7fb6c60f4b1f7568e160400ac9a2 Mon Sep 17 00:00:00 2001 From: Griffin-Sullivan Date: Mon, 25 Mar 2024 18:03:57 -0400 Subject: [PATCH 1/2] Add segmentation model support to from_transformers --- supervision/detection/core.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index f170563c..0af7c12e 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -14,6 +14,7 @@ from supervision.detection.utils import ( get_data_item, is_data_equal, mask_non_max_suppression, + mask_to_xyxy, merge_data, process_roboflow_result, validate_detections_fields, @@ -390,18 +391,30 @@ class Detections: @classmethod def from_transformers(cls, transformers_results: dict) -> Detections: """ - Creates a Detections instance from object detection + Creates a Detections instance from object detection or segmentation [transformer](https://github.com/huggingface/transformers) inference result. Returns: Detections: A new Detections object. """ + boxes = transformers_results.get("boxes") - return cls( - xyxy=transformers_results["boxes"].cpu().numpy(), - confidence=transformers_results["scores"].cpu().numpy(), - class_id=transformers_results["labels"].cpu().numpy().astype(int), - ) + # If the boxes key is in the transformers_results then we know it's an + # object detection result. Else, we can assume it's a segmentation model + if boxes: + return cls( + xyxy=transformers_results["boxes"].cpu().numpy(), + confidence=transformers_results["scores"].cpu().numpy(), + class_id=transformers_results["labels"].cpu().numpy().astype(int), + ) + else: + masks = transformers_results["masks"].cpu().numpy().astype(bool) + return cls( + xyxy=mask_to_xyxy(masks), + mask=masks, + confidence=transformers_results["scores"].cpu().numpy(), + class_id=transformers_results["labels"].cpu().numpy().astype(int), + ) @classmethod def from_detectron2(cls, detectron2_results) -> Detections: From 13f85ae6cc49e204bdcd932fbafb824c7809690d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 26 Mar 2024 21:05:02 +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/detection/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 0af7c12e..60ff07f3 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -399,7 +399,7 @@ class Detections: """ boxes = transformers_results.get("boxes") - # If the boxes key is in the transformers_results then we know it's an + # If the boxes key is in the transformers_results then we know it's an # object detection result. Else, we can assume it's a segmentation model if boxes: return cls(