From fb16bd9ee1a005a1dbdede7375a32058f16a891d Mon Sep 17 00:00:00 2001 From: soumik12345 <19soumik.rakshit96@gmail.com> Date: Tue, 15 Jul 2025 13:07:01 +0530 Subject: [PATCH] chore: change match_metric to overlap_metric --- supervision/detection/core.py | 17 ++++++------- supervision/detection/overlap_filter.py | 32 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index ed776496..6bbfbf4a 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -16,6 +16,7 @@ from supervision.detection.overlap_filter import ( box_non_max_suppression, mask_non_max_merge, mask_non_max_suppression, + OverlapMetric, ) from supervision.detection.tools.transformers import ( process_transformers_detection_result, @@ -1572,7 +1573,7 @@ class Detections: self, threshold: float = 0.5, class_agnostic: bool = False, - match_metric: str = "IOU", + overlap_metric: OverlapMetric = OverlapMetric.IOU, ) -> Detections: """ Performs non-max suppression on detection set. If the detections result @@ -1585,7 +1586,7 @@ class Detections: class_agnostic (bool): Whether to perform class-agnostic non-maximum suppression. If True, the class_id of each detection will be ignored. Defaults to False. - match_metric (str): Metric used for matching detections in slices. + overlap_metric (OverlapMetric): Metric used for matching detections in slices. "IOU" or "IOS". Defaults "IOU". Returns: @@ -1623,13 +1624,13 @@ class Detections: predictions=predictions, masks=self.mask, iou_threshold=threshold, - match_metric=match_metric, + match_metric=overlap_metric, ) else: indices = box_non_max_suppression( predictions=predictions, iou_threshold=threshold, - match_metric=match_metric, + match_metric=overlap_metric, ) return self[indices] @@ -1638,7 +1639,7 @@ class Detections: self, threshold: float = 0.5, class_agnostic: bool = False, - match_metric: str = "IOU", + overlap_metric: OverlapMetric = OverlapMetric.IOU, ) -> Detections: """ Perform non-maximum merging on the current set of object detections. @@ -1649,7 +1650,7 @@ class Detections: class_agnostic (bool): Whether to perform class-agnostic non-maximum merging. If True, the class_id of each detection will be ignored. Defaults to False. - match_metric (str): Metric used for matching detections in slices. + overlap_metric (OverlapMetric): Metric used for matching detections in slices. "IOU" or "IOS". Defaults "IOU". Returns: @@ -1689,13 +1690,13 @@ class Detections: predictions=predictions, masks=self.mask, iou_threshold=threshold, - match_metric=match_metric, + match_metric=overlap_metric, ) else: merge_groups = box_non_max_merge( predictions=predictions, iou_threshold=threshold, - match_metric=match_metric, + match_metric=overlap_metric, ) result = [] diff --git a/supervision/detection/overlap_filter.py b/supervision/detection/overlap_filter.py index 26edeaef..ab70c54e 100644 --- a/supervision/detection/overlap_filter.py +++ b/supervision/detection/overlap_filter.py @@ -406,3 +406,35 @@ class OverlapFilter(Enum): f"Invalid value type: {type(value)}. Must be an instance of " f"{cls.__name__} or str." ) + + +class OverlapMetric(Enum): + """ + Enum specifying the metric for measuring overlap between detections. + + Attributes: + IOU: Intersection over Union. + IOS: Intersection over Similarity. + """ + + IOU = "IOU" + IOS = "IOS" + + @classmethod + def list(cls): + return list(map(lambda c: c.value, cls)) + + @classmethod + def from_value(cls, value: Union[OverlapFilter, str]) -> OverlapFilter: + if isinstance(value, cls): + return value + if isinstance(value, str): + value = value.lower() + try: + return cls(value) + except ValueError: + raise ValueError(f"Invalid value: {value}. Must be one of {cls.list()}") + raise ValueError( + f"Invalid value type: {type(value)}. Must be an instance of " + f"{cls.__name__} or str." + )