Complete the code to calculate the area of a bounding box given its coordinates.
def box_area(x1, y1, x2, y2): return (x2 - x1) [1] (y2 - y1)
The area of a rectangle is width times height, so we multiply the width (x2 - x1) by the height (y2 - y1).
Complete the code to find the coordinates of the intersection box between two bounding boxes.
def intersection_box(boxA, boxB): x_left = max(boxA[0], boxB[0]) y_top = max(boxA[1], boxB[1]) x_right = min(boxA[2], boxB[2]) y_bottom = [1](boxA[3], boxB[3]) return x_left, y_top, x_right, y_bottom
The bottom coordinate of the intersection is the minimum of the two boxes' bottom y-coordinates.
Fix the error in the code that calculates the intersection area of two bounding boxes.
def intersection_area(boxA, boxB): x_left, y_top, x_right, y_bottom = intersection_box(boxA, boxB) if x_right < x_left or y_bottom < y_top: return 0 return (x_right - x_left) [1] (y_bottom - y_top)
The intersection area is width times height, so multiply the differences in x and y.
Fill both blanks to compute the union area of two bounding boxes.
def union_area(boxA, boxB): areaA = box_area(*boxA) areaB = box_area(*boxB) inter_area = intersection_area(boxA, boxB) return areaA [1] areaB [2] inter_area
The union area is the sum of both areas minus the intersection area to avoid double counting.
Fill all three blanks to complete the IoU calculation function.
def iou(boxA, boxB): inter_area = intersection_area(boxA, boxB) union = union_area(boxA, boxB) if union == 0: return 0 return [3](inter_area [1] union [2] 1.0)
IoU is intersection area divided by union area. Multiplying by 1.0 and converting to float ensures a float result.