diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 52c5d124..e2843994 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -19,9 +19,9 @@ from supervision.detection.overlap_filter import ( mask_non_max_suppression, ) from supervision.detection.tools.transformers import ( - process_detection_result, - process_transformers_v4_segmentation_result, - process_transformers_v5_segmentation_result + process_detection_result, + process_transformers_v4_segmentation_result, + process_transformers_v5_segmentation_result, ) from supervision.detection.utils import ( box_iou_batch, @@ -29,9 +29,7 @@ from supervision.detection.utils import ( extract_ultralytics_masks, get_data_item, is_data_equal, - mask_to_xyxy, merge_data, - png_to_mask, process_roboflow_result, xywh_to_xyxy, ) @@ -494,11 +492,15 @@ class Detections: if "boxes" in transformers_results: return process_detection_result(transformers_results, id2label) - + if "masks" in transformers_results or "png_string" in transformers_results: - return process_transformers_v4_segmentation_result(transformers_results, id2label) + return process_transformers_v4_segmentation_result( + transformers_results, id2label + ) else: - return process_transformers_v5_segmentation_result(transformers_results, id2label) + return process_transformers_v5_segmentation_result( + transformers_results, id2label + ) @classmethod def from_detectron2(cls, detectron2_results) -> Detections: diff --git a/supervision/detection/tools/transformers.py b/supervision/detection/tools/transformers.py index 062bfc1d..4224556d 100644 --- a/supervision/detection/tools/transformers.py +++ b/supervision/detection/tools/transformers.py @@ -1,12 +1,11 @@ from typing import Dict, Optional + import numpy as np -from supervision.detection.core import Detections -from supervision.detection.utils import ( - mask_to_xyxy, - png_to_mask -) from supervision.config import CLASS_NAME_DATA_FIELD +from supervision.detection.core import Detections +from supervision.detection.utils import mask_to_xyxy, png_to_mask + def get_data(class_ids: np.ndarray, id2label: Optional[Dict[int, str]]) -> dict: """Helper function to create data dictionary with class names if available.""" @@ -16,32 +15,41 @@ def get_data(class_ids: np.ndarray, id2label: Optional[Dict[int, str]]) -> dict: data[CLASS_NAME_DATA_FIELD] = class_names return data -def process_tensor_result(segmentation_array: np.ndarray, id2label: Optional[Dict[int, str]]) -> Detections: + +def process_tensor_result( + segmentation_array: np.ndarray, id2label: Optional[Dict[int, str]] +) -> Detections: """Process segmentation array result for segmentation.""" class_ids = np.unique(segmentation_array) - masks = np.stack([(segmentation_array == class_id).astype(bool) for class_id in class_ids], axis=0) + masks = np.stack( + [(segmentation_array == class_id).astype(bool) for class_id in class_ids], + axis=0, + ) data = get_data(class_ids, id2label) return Detections( - xyxy=mask_to_xyxy(masks), - mask=masks, - class_id=class_ids, - data=data + xyxy=mask_to_xyxy(masks), mask=masks, class_id=class_ids, data=data ) -def process_detection_result(detection_result: dict, id2label: Optional[Dict[int, str]]) -> Detections: + +def process_detection_result( + detection_result: dict, id2label: Optional[Dict[int, str]] +) -> Detections: """Process detection results containing boxes and labels.""" class_ids = detection_result["labels"].cpu().detach().numpy().astype(int) data = get_data(class_ids, id2label) - + return Detections( xyxy=detection_result["boxes"].cpu().detach().numpy(), confidence=detection_result["scores"].cpu().detach().numpy(), class_id=class_ids, - data=data + data=data, ) -def process_transformers_v4_segmentation_result(segmentation_result: dict, id2label: Optional[Dict[int, str]]) -> Detections: + +def process_transformers_v4_segmentation_result( + segmentation_result: dict, id2label: Optional[Dict[int, str]] +) -> Detections: """ Process Transformers v4 segmentation results. @@ -52,7 +60,7 @@ def process_transformers_v4_segmentation_result(segmentation_result: dict, id2la Returns: Detections: A Detections object created from the segmentation results. """ - + if "png_string" in segmentation_result: return process_png_segmentation_result(segmentation_result, id2label) else: @@ -60,14 +68,17 @@ def process_transformers_v4_segmentation_result(segmentation_result: dict, id2la class_ids = segmentation_result["labels"].cpu().detach().numpy().astype(int) return Detections( - xyxy=mask_to_xyxy(masks), - mask=masks, - confidence=segmentation_result["scores"].cpu().detach().numpy(), - class_id=class_ids, - data=get_data(class_ids, id2label) - ) + xyxy=mask_to_xyxy(masks), + mask=masks, + confidence=segmentation_result["scores"].cpu().detach().numpy(), + class_id=class_ids, + data=get_data(class_ids, id2label), + ) -def process_transformers_v5_segmentation_result(segmentation_result: dict, id2label: Optional[Dict[int, str]]) -> Detections: + +def process_transformers_v5_segmentation_result( + segmentation_result: dict, id2label: Optional[Dict[int, str]] +) -> Detections: """ Process Transformers v5 segmentation results. @@ -78,20 +89,28 @@ def process_transformers_v5_segmentation_result(segmentation_result: dict, id2la Returns: Detections: A Detections object created from the segmentation results. """ - + if segmentation_result.__class__.__name__ == "Tensor": segmentation_array = segmentation_result.cpu().detach().numpy() return process_tensor_result(segmentation_array, id2label) - + return process_png_segmentation_result(segmentation_result, id2label) -def process_segmentation_result(segmentation_result: dict, id2label: Optional[Dict[int, str]]) -> Detections: + +def process_segmentation_result( + segmentation_result: dict, id2label: Optional[Dict[int, str]] +) -> Detections: """Process segmentation results with masks and scores.""" segments_info = segmentation_result["segments_info"] scores = np.array([segment["score"] for segment in segments_info]) class_ids = np.array([segment["label_id"] for segment in segments_info]) segmentation_array = segmentation_result["segmentation"].cpu().detach().numpy() - masks = np.array([(segmentation_array == segment["id"]).astype(bool) for segment in segments_info]) + masks = np.array( + [ + (segmentation_array == segment["id"]).astype(bool) + for segment in segments_info + ] + ) data = get_data(class_ids, id2label) return Detections( @@ -99,20 +118,25 @@ def process_segmentation_result(segmentation_result: dict, id2label: Optional[Di mask=masks, confidence=scores, class_id=class_ids, - data=data + data=data, ) -def process_png_segmentation_result(png_result: dict, id2label: Optional[Dict[int, str]]) -> Detections: + +def process_png_segmentation_result( + png_result: dict, id2label: Optional[Dict[int, str]] +) -> Detections: """Process segmentation results from a PNG string.""" segments_info = png_result["segments_info"] class_ids = np.array([segment["category_id"] for segment in segments_info]) segmentation_array = png_to_mask(png_result["png_string"]) - masks = np.array([(segmentation_array == segment["id"]).astype(bool) for segment in segments_info]) + masks = np.array( + [ + (segmentation_array == segment["id"]).astype(bool) + for segment in segments_info + ] + ) data = get_data(class_ids, id2label) return Detections( - xyxy=mask_to_xyxy(masks), - mask=masks, - class_id=class_ids, - data=data + xyxy=mask_to_xyxy(masks), mask=masks, class_id=class_ids, data=data )