def _delete_overlapping_boxes(self, regions: dict[str, torch.Tensor], threshold: float) -> dict[str, torch.Tensor]:
        """Delete overlapping bounding boxes.

        For each bounding box, the overlap with all other bounding boxes relative to
        their own surface area is computed. When the relative overlap with any other
        box is higher than the specified threshold, the box is removed. When both boxes
        have a relative overlap higher than the threshold, only the smaller box is
        removed.

        Args:
            regions (dict[str, torch.Tensor]): Region detections for a single image in
                the batch.
            threshold (float): Maximum allowed overlap between bounding boxes.

        Returns:
            dict[str, torch.Tensor]: Region detections from which overlapping regions
                have been removed.
        """
        # sort boxes by area
        areas = box_area(regions["boxes"])
        indices = areas.argsort()

        keep = []
        for idx in range(len(indices)):
            overlap_coords = torch.hstack(
                [
                    torch.max(regions["boxes"][indices[idx], :2], regions["boxes"][indices[idx + 1 :], :2]),  # x1, y1
                    torch.min(regions["boxes"][indices[idx], 2:], regions["boxes"][indices[idx + 1 :], 2:]),  # x2, y2
                ],
            )
            mask = torch.all(overlap_coords[:, :2] < overlap_coords[:, 2:], dim=1)  # filter non-overlapping
            overlap = box_area(overlap_coords) * mask.int()
            overlap_ratio = overlap / areas[indices[idx]]

            if not any(overlap_ratio > threshold):
                keep.append(indices[idx])

        return self.subsample_regions(regions, torch.tensor(keep, dtype=torch.int64))
Delete overlapping bounding boxes.

        For each bounding box, the overlap with all other bounding boxes relative to
        their own surface area is computed. When the relative overlap with any other
        box is higher than the specified threshold, the box is removed. When both boxes
        have a relative overlap higher than the threshold, only the smaller box is
        removed.

        Args:
            regions (dict[str, torch.Tensor]): Region detections for a single image in
                the batch.
            threshold (float): Maximum allowed overlap between bounding boxes.

        Returns:
            dict[str, torch.Tensor]: Region detections from which overlapping regions
                have been removed.
        
src/anomalib/models/video/ai_vad/regions.py