Skip to content

Instantly share code, notes, and snippets.

@danielajisafe
Last active July 25, 2018 12:07
Show Gist options
  • Save danielajisafe/48a5a1e1b67c1c1c4fb3cf7c0e1ae69c to your computer and use it in GitHub Desktop.
Save danielajisafe/48a5a1e1b67c1c1c4fb3cf7c0e1ae69c to your computer and use it in GitHub Desktop.
def iou(box1, box2):
"""Implement the intersection over union (IoU) between box1 and box2
# Calculate the (y1, x1, y2, x2) coordinates of the intersection of box1 and box2. Calculate its Area.
### START CODE HERE ### (≈ 5 lines)
xi1 = max(box1[0],box2[0])
yi1 = max(box1[1],box2[1])
xi2 = min(box1[2],box2[2])
yi2 = min(box1[3],box2[3])
inter_area = (yi2-yi1) * (xi2-xi1)
### END CODE HERE ###
# Calculate the Union area by using Formula: Union(A,B) = A + B - Inter(A,B)
### START CODE HERE ### (≈ 3 lines)
box1_area = (box1[3]-box1[1]) * (box1[2]-box1[0])
box2_area = (box2[3]-box2[1]) * (box2[2]-box2[0])
union_area = box1_area + box2_area - inter_area
### END CODE HERE ###
# compute the IoU
### START CODE HERE ### (≈ 1 line)
iou = float(inter_area)/float(union_area)
### END CODE HERE ###
return iou
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment