def calculate_iou(predicted: Iterable[Number], target: Iterable[Number]):
    """Calculate the IoU between predicted and target bounding boxes."""

    def box_area(box):
        return (box[2] - box[0]) * (box[3] - box[1])

    def box_iou(box1, box2):
        # Calculate intersection coordinates
        x1 = max(box1[0], box2[0])
        y1 = max(box1[1], box2[1])
        x2 = min(box1[2], box2[2])
        y2 = min(box1[3], box2[3])

        # Calculate intersection area
        intersection = max(0, x2 - x1) * max(0, y2 - y1)

        # Calculate union area
        box1_area = box_area(box1)
        box2_area = box_area(box2)
        union = box1_area + box2_area - intersection

        # Calculate IoU
        iou = intersection / union if union > 0 else 0
        return iou

    # Calculate IoU for each pair of predicted and target boxes
    iou_scores = []
    for pred_box in predicted:
        best_iou = 0
        for target_box in target:
            iou = box_iou(pred_box, target_box)
            best_iou = max(best_iou, iou)
        iou_scores.append(best_iou)

    return iou_scores
Calculate the IoU between predicted and target bounding boxes.
vlmeval/dataset/utils/megabench/scoring/common/metrics.py