from collections import namedtuple
import numpy as np
import cv2
import json
import os
# define the `Detection` object
Detection = namedtuple("Detection", ["image_path", "gt", "pred"])

data_json_gt = json.load(open('/home/ekta/AI_current/vdot/vdot/Just_400/train_annotations/train_annotations.json'))
data_json_dt= json.load(open('/home/ekta/AI_current/efficientdet-pytorch/results.json'))
bbox_gt=[]
bbox_dt=[]
filename_gt=[]
for i in data_json_gt.values():
    bbox_gt.append(i['assets']['drop_inlet'])
    filename_gt.append(i['filename'])
for j in bbox_dt:
    bbox_dt.append(j['bbox'])
#print(bbox_dt[0])
print(filename_gt[0])

gt_list=[]
dt_list=[]
def bb_intersection_over_union(boxA, boxB):
	# 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 = max(0, xB - xA + 1) * max(0, 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)
	# return the intersection over union value
	return iou

    # define the list of example detections
examples = [
	Detection("/home/ekta/AI_current/vdot/vdot/Just_400/train_set/202010211319290307.jpg" , bbox_gt[0], [26, 600, 87, 600]),
	Detection("/home/ekta/AI_current/vdot/vdot/Just_400/train_set/202010221259310362.jpg", bbox_gt[1], [27, 600, 87, 600])]

for detection in examples:
    # load the image
    image = cv2.imread(detection.image_path)
	# draw the ground-truth bounding box along with the predicted
	# bounding box
    cv2.rectangle(image, tuple(detection.gt[:2]), tuple(detection.gt[2:]), (0, 255, 0),2)
    cv2.rectangle(image, tuple(detection.pred[:2]), tuple(detection.pred[2:]), (0, 0, 255), 2)
	# compute the intersection over union and display it
    iou = bb_intersection_over_union(detection.gt, detection.pred)
    cv2.putText(image, "IoU: {:.4f}".format(iou), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
    print("{}: {:.4f}".format(detection.image_path, iou))
	# show the output image
    cv2.imshow("Image", image)
    cv2.waitKey(0)