def IOU(box1, box2):
    """
    box1: (x1, y1, x2, y2)
    box2: (x1, y2, x2, y2)
    """
    minX = max(box1[0], box2[0])
    maxX = min(box1[2], box2[2])
    minY = max(box1[1], box2[1])
    maxY = min(box1[3], box2[3])

    if minX > maxX or minY > maxY:
         return 0
         
    intersection  = (maxX - minX) * (maxY - minY)
    area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
    area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])

    union = area1 + area2 - intersection

    return intersection / union