from collections import namedtuple
import numpy as np
import cv2

#Detection = namedtuple("Detection", ["image_path", "gt", "pred"])

def bb_intersection_over_union(fa, fb):

    boxA = [fa[0],fa[1],fa[0]+fa[2],fa[1]+fa[3]]    
    boxB = [fb[0][0],fb[0][1],fb[0][0]+fb[0][2],fb[0][1]+fb[0][3]]    
	# determine the (x, y)-coordinates of the intersection rectangle
    xA = max(boxA[0], boxB[0])
    yA = max(boxA[1], boxB[1])
    xB = min(boxA[2], boxB[2])
    yB = min(boxA[3], boxB[3])

	# compute the area of intersection rectangle
    interArea = (xB - xA + 1) * (yB - yA + 1)

	# compute the area of both the prediction and ground-truth
	# rectangles
    boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
    boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)

	# 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 = interArea / float(boxAArea + boxBArea - interArea)

    if iou > 0.5:
        result = 1
    else:
        result = 0
	# return the intersection over union value
    return iou, result