From f044398c170c3ca98dca76dda02e4673ba5dc38f Mon Sep 17 00:00:00 2001 From: Onuralp SEZER Date: Sun, 20 Jul 2025 20:11:37 +0300 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20=20=F0=9F=90=9E=20update=20inference?= =?UTF-8?q?=5Fslicer.py=20for=20improved=20detection=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/detection/tools/inference_slicer.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/supervision/detection/tools/inference_slicer.py b/supervision/detection/tools/inference_slicer.py index ff4a44b2..69bc7d79 100644 --- a/supervision/detection/tools/inference_slicer.py +++ b/supervision/detection/tools/inference_slicer.py @@ -5,11 +5,11 @@ from collections.abc import Callable from concurrent.futures import ThreadPoolExecutor, as_completed import numpy as np - +from typing import Literal from supervision.config import ORIENTED_BOX_COORDINATES from supervision.detection.core import Detections from supervision.detection.utils.boxes import move_boxes, move_oriented_boxes -from supervision.detection.utils.iou_and_nms import OverlapFilter +from supervision.detection.utils.iou_and_nms import OverlapFilter,OverlapMetric from supervision.detection.utils.masks import move_masks from supervision.utils.image import crop_image from supervision.utils.internal import ( @@ -96,7 +96,7 @@ class InferenceSlicer: overlap_wh: tuple[int, int] | None = None, overlap_filter: OverlapFilter | str = OverlapFilter.NON_MAX_SUPPRESSION, iou_threshold: float = 0.5, - match_metric: str = "IOU", + match_metric: OverlapMetric = OverlapMetric.IOU, thread_workers: int = 1, ): if overlap_ratio_wh is not None: @@ -173,11 +173,11 @@ class InferenceSlicer: return merged elif self.overlap_filter == OverlapFilter.NON_MAX_SUPPRESSION: return merged.with_nms( - threshold=self.iou_threshold, match_metric=self.match_metric + threshold=self.iou_threshold, overlap_metric=self.match_metric ) elif self.overlap_filter == OverlapFilter.NON_MAX_MERGE: return merged.with_nmm( - threshold=self.iou_threshold, match_metric=self.match_metric + threshold=self.iou_threshold, overlap_metric=self.match_metric ) else: warnings.warn( From db51a9ff98d653e2b9167a59a96d59d3afaf25b1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 20 Jul 2025 17:12:18 +0000 Subject: [PATCH 2/4] =?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/tools/inference_slicer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/supervision/detection/tools/inference_slicer.py b/supervision/detection/tools/inference_slicer.py index 69bc7d79..456067d7 100644 --- a/supervision/detection/tools/inference_slicer.py +++ b/supervision/detection/tools/inference_slicer.py @@ -5,11 +5,11 @@ from collections.abc import Callable from concurrent.futures import ThreadPoolExecutor, as_completed import numpy as np -from typing import Literal + from supervision.config import ORIENTED_BOX_COORDINATES from supervision.detection.core import Detections from supervision.detection.utils.boxes import move_boxes, move_oriented_boxes -from supervision.detection.utils.iou_and_nms import OverlapFilter,OverlapMetric +from supervision.detection.utils.iou_and_nms import OverlapFilter, OverlapMetric from supervision.detection.utils.masks import move_masks from supervision.utils.image import crop_image from supervision.utils.internal import ( From 1adb75520f479e076e3f98ab9f76530a03ecc5bd Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Mon, 21 Jul 2025 00:02:14 +0200 Subject: [PATCH 3/4] bump version from `0.26.0` to `0.26.1`; improve docstrings; unify naming; improve parsing --- pyproject.toml | 2 +- supervision/detection/core.py | 8 +++--- .../detection/tools/inference_slicer.py | 12 ++++----- supervision/detection/utils/iou_and_nms.py | 27 ++++++++++++------- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1b830697..cae78492 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ name = "supervision" description = "A set of easy-to-use utils that will come in handy in any Computer Vision project" license = { text = "MIT" } -version = "0.26.0" +version = "0.26.1" readme = "README.md" requires-python = ">=3.9" authors = [ diff --git a/supervision/detection/core.py b/supervision/detection/core.py index efaa366a..e806513a 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -1939,8 +1939,8 @@ 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. - overlap_metric (OverlapMetric): Metric used for measuring overlap between - detections in slices. + overlap_metric (OverlapMetric): Metric used to compute the degree of + overlap between pairs of masks or boxes (e.g., IoU, IoS). Returns: Detections: A new Detections object containing the subset of detections @@ -2003,8 +2003,8 @@ 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. - overlap_metric (OverlapMetric): Metric used for measuring overlap between - detections in slices. + overlap_metric (OverlapMetric): Metric used to compute the degree of + overlap between pairs of masks or boxes (e.g., IoU, IoS). Returns: Detections: A new Detections object containing the subset of detections diff --git a/supervision/detection/tools/inference_slicer.py b/supervision/detection/tools/inference_slicer.py index 456067d7..aaecccb3 100644 --- a/supervision/detection/tools/inference_slicer.py +++ b/supervision/detection/tools/inference_slicer.py @@ -75,8 +75,8 @@ class InferenceSlicer: filtering or merging overlapping detections in slices. iou_threshold (float): Intersection over Union (IoU) threshold used when filtering by overlap. - match_metric (str): Metric used for matching detections in slices. - "IOU" or "IOS". Defaults "IOU". + overlap_metric (Union[OverlapMetric, str]): Metric used for matching detections + in slices. callback (Callable): A function that performs inference on a given image slice and returns detections. thread_workers (int): Number of threads for parallel execution. @@ -96,7 +96,7 @@ class InferenceSlicer: overlap_wh: tuple[int, int] | None = None, overlap_filter: OverlapFilter | str = OverlapFilter.NON_MAX_SUPPRESSION, iou_threshold: float = 0.5, - match_metric: OverlapMetric = OverlapMetric.IOU, + overlap_metric: OverlapMetric | str = OverlapMetric.IOU, thread_workers: int = 1, ): if overlap_ratio_wh is not None: @@ -112,7 +112,7 @@ class InferenceSlicer: self.slice_wh = slice_wh self.iou_threshold = iou_threshold - self.match_metric = match_metric + self.overlap_metric = OverlapMetric.from_value(overlap_metric) self.overlap_filter = OverlapFilter.from_value(overlap_filter) self.callback = callback self.thread_workers = thread_workers @@ -173,11 +173,11 @@ class InferenceSlicer: return merged elif self.overlap_filter == OverlapFilter.NON_MAX_SUPPRESSION: return merged.with_nms( - threshold=self.iou_threshold, overlap_metric=self.match_metric + threshold=self.iou_threshold, overlap_metric=self.overlap_metric ) elif self.overlap_filter == OverlapFilter.NON_MAX_MERGE: return merged.with_nmm( - threshold=self.iou_threshold, overlap_metric=self.match_metric + threshold=self.iou_threshold, overlap_metric=self.overlap_metric ) else: warnings.warn( diff --git a/supervision/detection/utils/iou_and_nms.py b/supervision/detection/utils/iou_and_nms.py index 56bdff71..1a6f80bc 100644 --- a/supervision/detection/utils/iou_and_nms.py +++ b/supervision/detection/utils/iou_and_nms.py @@ -164,7 +164,8 @@ def box_iou_batch( `shape = (N, 4)` where `N` is number of true objects. boxes_detection (np.ndarray): 2D `np.ndarray` representing detection boxes. `shape = (M, 4)` where `M` is number of detected objects. - overlap_metric (OverlapMetric): Metric used for matching detections in slices. + overlap_metric (OverlapMetric): Metric used to compute the degree of overlap + between pairs of boxes (e.g., IoU, IoS). Returns: np.ndarray: Pairwise IoU of boxes from `boxes_true` and `boxes_detection`. @@ -381,7 +382,8 @@ def _mask_iou_batch_split( Args: masks_true (np.ndarray): 3D `np.ndarray` representing ground-truth masks. masks_detection (np.ndarray): 3D `np.ndarray` representing detection masks. - overlap_metric (OverlapMetric): Metric used for matching detections in slices. + overlap_metric (OverlapMetric): Metric used to compute the degree of overlap + between pairs of masks (e.g., IoU, IoS). Returns: np.ndarray: Pairwise IoU of masks from `masks_true` and `masks_detection`. @@ -433,7 +435,8 @@ def mask_iou_batch( Args: masks_true (np.ndarray): 3D `np.ndarray` representing ground-truth masks. masks_detection (np.ndarray): 3D `np.ndarray` representing detection masks. - overlap_metric (OverlapMetric): Metric used for matching detections in slices. + overlap_metric (OverlapMetric): Metric used to compute the degree of overlap + between pairs of masks (e.g., IoU, IoS). memory_limit (int): memory limit in MB, default is 1024 * 5 MB (5GB). Returns: @@ -492,7 +495,8 @@ def mask_non_max_suppression( dimensions of each mask. iou_threshold (float): The intersection-over-union threshold to use for non-maximum suppression. - overlap_metric (OverlapMetric): Metric used for matching detections in slices. + overlap_metric (OverlapMetric): Metric used to compute the degree of overlap + between pairs of masks (e.g., IoU, IoS). mask_dimension (int): The dimension to which the masks should be resized before computing IOU values. Defaults to 640. @@ -543,7 +547,8 @@ def box_non_max_suppression( or `(x_min, y_min, x_max, y_max, score, class)`. iou_threshold (float): The intersection-over-union threshold to use for non-maximum suppression. - overlap_metric (OverlapMetric): Metric used for matching detections in slices. + overlap_metric (OverlapMetric): Metric used to compute the degree of overlap + between pairs of boxes (e.g., IoU, IoS). Returns: np.ndarray: A boolean array indicating which predictions to keep after n @@ -603,7 +608,8 @@ def _group_overlapping_masks( the predictions. iou_threshold (float): The intersection-over-union threshold to use for non-maximum suppression. Defaults to 0.5. - overlap_metric (OverlapMetric): Metric used for matching detections in slices. + overlap_metric (OverlapMetric): Metric used to compute the degree of overlap + between pairs of masks (e.g., IoU, IoS). Returns: list[list[int]]: Groups of prediction indices be merged. @@ -664,7 +670,8 @@ def mask_non_max_merge( to use for non-maximum suppression. mask_dimension (int): The dimension to which the masks should be resized before computing IOU values. Defaults to 640. - overlap_metric (OverlapMetric): Metric used for matching detections in slices. + overlap_metric (OverlapMetric): Metric used to compute the degree of overlap + between pairs of masks (e.g., IoU, IoS). Returns: np.ndarray: A boolean array indicating which predictions to keep after @@ -717,7 +724,8 @@ def _group_overlapping_boxes( and the confidence scores. iou_threshold (float): The intersection-over-union threshold to use for non-maximum suppression. Defaults to 0.5. - overlap_metric (OverlapMetric): Metric used for matching detections in slices. + overlap_metric (OverlapMetric): Metric used to compute the degree of overlap + between pairs of boxes (e.g., IoU, IoS). Returns: list[list[int]]: Groups of prediction indices be merged. @@ -765,7 +773,8 @@ def box_non_max_merge( detections of different classes to be merged. iou_threshold (float): The intersection-over-union threshold to use for non-maximum suppression. Defaults to 0.5. - overlap_metric (OverlapMetric): Metric used for matching detections in slices. + overlap_metric (OverlapMetric): Metric used to compute the degree of overlap + between pairs of boxes (e.g., IoU, IoS). Returns: list[list[int]]: Groups of prediction indices be merged. From c54d16c57e6f6ad5398cbd90bfe25f32514fb3a8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 20 Jul 2025 22:02:36 +0000 Subject: [PATCH 4/4] =?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 e806513a..ea466b89 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -2003,7 +2003,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. - overlap_metric (OverlapMetric): Metric used to compute the degree of + overlap_metric (OverlapMetric): Metric used to compute the degree of overlap between pairs of masks or boxes (e.g., IoU, IoS). Returns: