Source code for iaa_od.metrics.iou

from iaa_od.models import AnnotationProtocol

[docs] def iou(lhs: AnnotationProtocol, rhs: AnnotationProtocol, /) -> float: """ Compute the Intersection over Union (IoU) between two bounding boxes. Parameters: lhs (AnnotationProtocol): The first annotation. rhs (AnnotationProtocol): The second annotation. Returns: float: The IoU value between the two bounding boxes. """ inter_area: float = _intersection(lhs, rhs) union_area: float = _union(lhs, rhs) if union_area == 0: return 0.0 iou_value: float = inter_area / union_area return iou_value
def _intersection(lhs: AnnotationProtocol, rhs: AnnotationProtocol) -> float: """ Compute the area of intersection between two bounding boxes. Parameters: lhs (AnnotationProtocol): The first annotation. rhs (AnnotationProtocol): The second annotation. Returns: float: The area of intersection between the two bounding boxes. """ x1_min, y1_min, x1_max, y1_max = lhs.bbox_coords.coords.to_xyxy() x2_min, y2_min, x2_max, y2_max = rhs.bbox_coords.coords.to_xyxy() inter_x_min = max(x1_min, x2_min) inter_y_min = max(y1_min, y2_min) inter_x_max = min(x1_max, x2_max) inter_y_max = min(y1_max, y2_max) inter_width = max(0, inter_x_max - inter_x_min) inter_height = max(0, inter_y_max - inter_y_min) inter_area = inter_width * inter_height return inter_area def _union(lhs: AnnotationProtocol, rhs: AnnotationProtocol) -> float: """ Compute the area of union between two bounding boxes. Parameters: lhs (AnnotationProtocol): The first annotation. rhs (AnnotationProtocol): The second annotation. Returns: float: The area of union between the two bounding boxes. """ area_lhs = lhs.bbox_coords.area area_rhs = rhs.bbox_coords.area inter_area = _intersection(lhs, rhs) union_area = area_lhs + area_rhs - inter_area return union_area
[docs] def iom(lhs: AnnotationProtocol, rhs: AnnotationProtocol, /) -> float: """ Computes Intersection over Minimum (IoM), which divides the intersection area of two bounding boxes by the area of the smaller bounding box. Parameters: lhs (AnnotationProtocol): The first annotation. rhs (AnnotationProtocol): The second annotation. Returns: float: The IoM value between the two bounding boxes. """ lhs_area: float = lhs.bbox_coords.area rhs_area: float = rhs.bbox_coords.area min_area: float = min(lhs_area, rhs_area) inter_area: float = _intersection(lhs, rhs) if min_area == 0: raise ValueError("A bounding box returned an area of zero.") return inter_area / min_area