def _iou(box1, box2, precision=1e-5):
    """
    Calculate the Intersection over Union value for 2 bounding boxes

    :param box1: array of 4 values
    (top left and bottom right coords): [x0, y0, x1, x2]
    :param box2: same as box1
    :param precision: calculate precision for calculating
    :return: IoU
    """
    box1_x0, box1_y0, box1_x1, box1_y1 = box1
    box2_x0, box2_y0, box2_x1, box2_y1 = box2

    int_x0 = max(box1_x0, box2_x0)
    int_y0 = max(box1_y0, box2_y0)
    int_x1 = min(box1_x1, box2_x1)
    int_y1 = min(box1_y1, box2_y1)

    int_area = max(int_x1 - int_x0, 0) * max(int_y1 - int_y0, 0)

    b1_area = (box1_x1 - box1_x0) * (box1_y1 - box1_y0)
    b2_area = (box2_x1 - box2_x0) * (box2_y1 - box2_y0)

    # we add small epsilon of 1e-05 to avoid division by 0
    ret_iou = int_area / (b1_area + b2_area - int_area + precision)
    return ret_iou