Skip to content

Instantly share code, notes, and snippets.

@ppushp7

ppushp7/iou.py Secret

Created June 8, 2020 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppushp7/a3bdafb19acc9ed20c6d0b4ad1f15b48 to your computer and use it in GitHub Desktop.
Save ppushp7/a3bdafb19acc9ed20c6d0b4ad1f15b48 to your computer and use it in GitHub Desktop.
def iou(box1, box2):
"""
box1 -- first box, list object with coordinates (x1, y1, x2, y2)
box2 -- second box, list object with coordinates (x1, y1, x2, y2)
"""
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)
box1_area = (box1[2]-box1[0])*(box1[3]-box1[1])
box2_area = (box2[2]-box2[0])*(box2[3]-box2[1])
union_area = box1_area+box2_area-inter_area
# compute the IoU
iou = inter_area/union_area
return iou
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment