Skip to content

Computing Bounding Boxes

Dmitri Soshnikov edited this page Nov 28, 2018 · 1 revision

Computing bounding boxes for RetinaNet training

This example assumes there are masks for image files in c:\data\ directory, and we want to computed bounding box coordinates and save them to CSV file, to be used later for RetinaNet training.

Show images with bounding boxes

(mp.get_files('c:/data')
 | mp.as_field('filename')
 | mp.apply('filename','img',lambda f: cv2.imread(f))
 | mp.apply('img','bbox',mp.utils.image.calc_bounding_box)
 | mp.apply(['img','bbox'],None,lambda x: cv2.rectangle(x[0],(x[1][1],x[1][0]),(x[1][3],x[1][2]),(255,0,0),3))
 | take(16)
 | mp.select_field('img')
 | pexec(mp.utils.image.show_images)
)

Save bounding boxes to CSV

(mp.get_files('c:/data')
 | mp.as_field('filename')
 | mp.apply('filename','img',lambda f: cv2.imread(f))
 | mp.apply('img','bbox',mp.utils.image.calc_bounding_box)
 | mp.apply(['filename','bbox'],'result',lambda x: [x[0],x[1][1],x[1][0],x[1][3],x[1][2]])
 | mp.select_field('result')
 | mp.write_csv('c:/data/result.csv'))