def iou(box1, box2):
    """Intersection over Union value."""
    # Intersection rectangle
    intersect_x1 = max(box1[0], box2[0])
    intersect_y1 = max(box1[1], box2[1])
    intersect_x2 = min(box1[2], box2[2])
    intersect_y2 = min(box1[3], box2[3])

    # Area of intersection rectangle
    if intersect_x1 >= intersect_x2 or intersect_y1 >= intersect_y2:
        return 0.0
    area_intersection = (intersect_x2 - intersect_x1) * (intersect_y2 - intersect_y1)

    # Area of both boxes
    area_box1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
    area_box2 = (box2[2] - box2[0]) * (box2[3] - box2[1])

    # Intersection over union ratio: intersection_area/union_area
    area_union = float(area_box1 + area_box2 - area_intersection)
    return area_intersection / area_union if area_union != 0 else 1