I added exception handling.
def iou(box1, box2):
xi1 = max(box1[0][0], box2[0][0])
yi1 = max(box1[0][1], box2[0][1])
xi2 = min(box1[1][0], box2[1][0])
yi2 = min(box1[1][1], box2[1][1])
if(((xi2 - xi1) < 0) or ((yi2 - yi1) < 0)):
IoU = 0
return IoU
else:
inter_area = (xi2 - xi1)*(yi2 - yi1)
# Formula: Union(A,B) = A + B - Inter(A,B)
box1_area = (box1[1][1] - box1[0][1])*(box1[1][0]- box1[0][0])
box2_area = (box2[1][1] - box2[0][1])*(box2[1][0]- box2[0][0])
union_area = (box1_area + box2_area) - inter_area
# compute the IoU
IoU = inter_area / union_area
return IoU