Introduction
IoU helps us measure how much two shapes overlap. It tells us how well a predicted area matches the true area.
def iou(box1, box2): # box format: [x1, y1, x2, y2] x_left = max(box1[0], box2[0]) y_top = max(box1[1], box2[1]) x_right = min(box1[2], box2[2]) y_bottom = min(box1[3], box2[3]) if x_right <= x_left or y_bottom <= y_top: return 0.0 intersection_area = (x_right - x_left) * (y_bottom - y_top) box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1]) box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1]) iou_value = intersection_area / float(box1_area + box2_area - intersection_area) return iou_value
boxA = [1, 1, 4, 4] boxB = [2, 2, 5, 5] iou_score = iou(boxA, boxB) print(f"IoU: {iou_score:.2f}")
boxA = [0, 0, 2, 2] boxB = [3, 3, 5, 5] iou_score = iou(boxA, boxB) print(f"IoU: {iou_score:.2f}")
def iou(box1, box2): x_left = max(box1[0], box2[0]) y_top = max(box1[1], box2[1]) x_right = min(box1[2], box2[2]) y_bottom = min(box1[3], box2[3]) if x_right <= x_left or y_bottom <= y_top: return 0.0 intersection_area = (x_right - x_left) * (y_bottom - y_top) box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1]) box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1]) iou_value = intersection_area / float(box1_area + box2_area - intersection_area) return iou_value # Example boxes box1 = [1, 1, 4, 4] box2 = [2, 2, 5, 5] box3 = [0, 0, 2, 2] box4 = [3, 3, 5, 5] print(f"IoU between box1 and box2: {iou(box1, box2):.2f}") print(f"IoU between box3 and box4: {iou(box3, box4):.2f}")
def iou(area_a, area_b, intersection):
union = area_a + area_b + intersection
return intersection / union
print(iou(15, 25, 5))
What is the error in this code?Pass the test and this pattern moves unknown