| def calculate_iou(box1, box2): | |
| """Compute IoU between two boxes.""" | |
| xA = max(box1[0], box2[0]) | |
| yA = max(box1[1], box2[1]) | |
| xB = min(box1[2], box2[2]) | |
| yB = min(box1[3], box2[3]) | |
| inter_area = max(0, xB - xA) * max(0, yB - yA) | |
| box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1]) | |
| box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1]) | |
| union_area = box1_area + box2_area - inter_area | |
| return inter_area / union_area if union_area > 0 else 0 | |