Intersection-over-union between a ground truth box and a predicted box, in xyxy format.
def compute_iou(gt, pred):
ix1 = max(gt[0], pred[0])
iy1 = max(gt[1], pred[1])
ix2 = min(gt[2], pred[2])
iy2 = min(gt[3], pred[3])
inter = max(0, ix2 - ix1) * max(0, iy2 - iy1)
area_g = (gt[2] - gt[0]) * (gt[3] - gt[1])
area_p = (pred[2] - pred[0]) * (pred[3] - pred[1])
union = area_g + area_p - inter
return inter / union if union > 0 else 0.0
Boxes are (x1, y1, x2, y2). A detection is typically counted as a true positive when this returns 0.5 or higher against its matched ground truth box.