From 86bd91e26555fa7756110a46965f5623dd3174ea Mon Sep 17 00:00:00 2001 From: Archit H Barve Date: Tue, 12 Nov 2024 21:37:40 +0530 Subject: [PATCH 1/2] feat: add estimated polygon zone occupation functionality --- supervision/detection/utils.py | 50 ++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/supervision/detection/utils.py b/supervision/detection/utils.py index f6bcd33bc..c4cf5aaf8 100644 --- a/supervision/detection/utils.py +++ b/supervision/detection/utils.py @@ -1173,3 +1173,53 @@ def spread_out_boxes( xyxy_padded[:, [2, 3]] += force_vectors return pad_boxes(xyxy_padded, px=-1) + + +# Overlap estimation +def generate_mask_from_detections(img_shape: tuple[int, int], detection) -> np.ndarray: + """convert bounding polygon coord to binary mask""" + # Create binary mask + mask = np.zeros(img_shape[:2], np.uint8) + + # Segmentation case + if "mask" in detection: + mask = detection["mask"] + + # Object detection case + elif "xyxy" in detection: + x1, y1, x2, y2 = map(int, detection["xyxy"]) + cv2.rectangle(mask, (x1, y1), (x2, y2), color=(1,), thickness=cv2.FILLED) + + # Oriented Bounding box detection case + elif "polygon" in detection: + contour = np.array(detection["polygon"], dtype=np.int32).reshape(-1, 1, 2) + mask = cv2.drawContours(mask, [contour], -1, color=(1,), thickness=cv2.FILLED) + + return mask + + +def calculate_overlap_area(zone_mask: np.ndarray, masks: List[np.ndarray]) -> float: + """calculate how much % of the zone is occupied""" + # create one mask from the object masks as the union + + union_mask = np.bitwise_or.reduce(np.stack(masks), axis=0) + + # Calculate the overlap mask between union_mask & zone_mask + overlap_mask = np.bitwise_and(union_mask, zone_mask) + + # Calculate sizes + overlap_size = np.sum(overlap_mask) + zone_size = np.sum(zone_mask) + + return 100 * overlap_size / zone_size + + +def calculate_overlap_with_zone( + zone_mask: np.ndarray, + detections, + img_shape: tuple[int, int], + show_plot: bool = True, +) -> float: + masks = [generate_mask_from_detections(img_shape, det) for det in detections] + return calculate_overlap_area(zone_mask, masks) + From 4d5699bbe9a153513385067bd662264c82b94ba3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:19:03 +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/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/supervision/detection/utils.py b/supervision/detection/utils.py index c4cf5aaf8..5714e7e1c 100644 --- a/supervision/detection/utils.py +++ b/supervision/detection/utils.py @@ -1222,4 +1222,3 @@ def calculate_overlap_with_zone( ) -> float: masks = [generate_mask_from_detections(img_shape, det) for det in detections] return calculate_overlap_area(zone_mask, masks) -