def calculate_iou(bbox1, bbox2):
  """Calculates the Intersection-Over-Union of two bounding boxes.

  IOU is a ratio for determining how much two bounding boxes match.

  Args:
    bbox1: The first bounding box.
    bbox2: The bounding box to compare with.

  Returns:
    The IOU as a float from 0.0 to 1.0 (1.0 being a perfect match.)
  """
  x_overlap = max(0,
                  min(bbox1.right, bbox2.right) - max(bbox1.left, bbox2.left))
  y_overlap = max(0,
                  min(bbox1.bottom, bbox2.bottom) - max(bbox1.top, bbox2.top))
  intersection = x_overlap * y_overlap

  area1 = (bbox1.right - bbox1.left) * (bbox1.bottom - bbox1.top)
  area2 = (bbox2.right - bbox2.left) * (bbox2.bottom - bbox2.top)
  union = area1 + area2 - intersection

  return intersection / union