def get_iou(gt_bbx, pred_bbx):
    """
    Calculate the Intersection over Union (IoU) of two bounding boxes.
    Based on:
       https://stackoverflow.com/questions/25349178/
       calculating-percentage-of-bounding-box-overlap-for-image-detector-evaluation

    Parameters
    ----------
    gt_bbx : dict
        ymin, xmin, ymax, xmax]
        Keys: {'xmin', 'xmax', 'ymin', 'ymax'}
        The (xmin, ymin) position is at the top left corner,
        the (xmax, ymax) position is at the bottom right corner
    pred_bbx : dict
        Keys: {'xmin', 'xmax', 'ymin', 'ymax'}
        The (xmin, ymin) position is at the top left corner,
        the (xmax, ymax) position is at the bottom right corner

    Returns
    -------
    float
        in [0, 1]
    """

    assert gt_bbx['xmin'] < gt_bbx['xmax']
    assert gt_bbx['ymin'] < gt_bbx['ymax']
    assert pred_bbx['xmin'] < pred_bbx['xmax']
    assert pred_bbx['ymin'] < pred_bbx['ymax']

    # determine the coordinates of the intersection rectangle
    x_left = max(gt_bbx['xmin'], pred_bbx['xmin'])
    y_top = max(gt_bbx['ymin'], pred_bbx['ymin'])
    x_right = min(gt_bbx['xmax'], pred_bbx['xmax'])
    y_bottom = min(gt_bbx['ymax'], pred_bbx['ymax'])

    if (x_right < x_left) or (y_bottom < y_top):
        iou = 0.0
        intersection_area = (x_right - x_left) * (y_bottom - y_top)
    else:
        # The intersection of two axis-aligned bounding boxes is always an
        # axis-aligned bounding box
        intersection_area = (x_right - x_left) * (y_bottom - y_top)

        # compute the area of both BBs
        gt_bbx_area = (gt_bbx['xmax']-gt_bbx['xmin']) * \
            (gt_bbx['ymax']-gt_bbx['ymin'])
        pred_bbx_area = (pred_bbx['xmax']-pred_bbx['xmin']) * \
            (pred_bbx['ymax']-pred_bbx['ymin'])

        # compute the intersection over union by taking the intersection
        # area and dividing it by the sum of prediction + ground-truth
        # areas - the interesection area
        iou = intersection_area / \
            float(gt_bbx_area + pred_bbx_area - intersection_area)
        assert iou >= 0.0
        assert iou <= 1.0
    return iou, intersection_area