The Mirror and the Boxes

Using Vision ModelsMedium+20 XP+20

The Mirror and the Boxes

A team training a street-scene detector wants horizontal flips in its augmentation. A flip moves every object to the other side of the photo, so every bounding box has to move with it. Otherwise the labels point at empty road, which breaks the one rule of augmentation: never change the answer.

The scenes also contain painted road arrows. A mirrored left-turn arrow really is a right-turn arrow, so the team keeps the flip and renames those boxes too.

The image is a grayscale grid, given as a list of rows. Its width WWW is the number of columns.

Boxes are tuples (name, x_min, y_min, x_max, y_max). The coordinates mark pixel edges, not pixel centres. xxx runs from 000 at the image's left edge to WWW at its right edge, and column ccc is the strip from x=cx = cx=c to x=c+1x = c + 1x=c+1. So a box with x_min = 1 and x_max = 3 covers columns 1 and 2. yyy works the same way, measured down from the top.

Task: write flip_with_boxes(image, boxes, mirror_names) that returns a tuple (flipped_image, flipped_boxes):

  • flipped_image is the image mirrored left to right.
  • flipped_boxes is a list with one tuple per input box, in the same order and the same layout, giving where that object is after the flip. Every box must still have x_min < x_max.
  • mirror_names is a dict such as {'arrow_left': 'arrow_right', 'arrow_right': 'arrow_left'}. A box whose name is a key takes the mapped name. Every other name stays as it is.

Examples

image = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
boxes = [('cyclist', 1, 0, 3, 2)]
mirror_names = {}
returns ([[5, 4, 3, 2, 1], [10, 9, 8, 7, 6], [15, 14, 13, 12, 11]], [('cyclist', 2, 0, 4, 2)])
image = [[5, 9, 9, 0], [5, 9, 9, 0]]
boxes = [('arrow_left', 0, 0, 1, 2), ('car', 1, 1, 4, 2)]
mirror_names = {'arrow_left': 'arrow_right', 'arrow_right': 'arrow_left'}
returns ([[0, 9, 9, 5], [0, 9, 9, 5]], [('arrow_right', 3, 0, 4, 2), ('car', 0, 1, 3, 2)])

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run