Skip to content

Instantly share code, notes, and snippets.

@burgalon
Created May 16, 2017 10:37
Show Gist options
  • Save burgalon/5fb96c196c5961f4ece75abcdfc0a371 to your computer and use it in GitHub Desktop.
Save burgalon/5fb96c196c5961f4ece75abcdfc0a371 to your computer and use it in GitHub Desktop.
Utilities for bounding box
def transform_predicitions(window_prediction_bbox, xs, ys):
'''
Transform SSD bounding boxes in a sliding window, to absolute coordinates in the original image
'''
r= []
for label, score, xmin, ymin, xmax, ymax in window_prediction_bbox:
label = int(label)
xmin = math.ceil(xmin*box_width)+xs
xmax = math.ceil(xmax*box_width)+xs
ymin = math.floor(ymin*box_height)+ys
ymax = math.floor(ymax*box_height)+ys
r.append((label, score, xmin, ymin, xmax, ymax))
return r
def show_predictions(predictions_bbox, current_axis=None):
'''
Plots multiple bounding boxes with absolute coordinates
'''
if current_axis==None:
current_axis = plt.gca()
for label, score, xmin, ymin, xmax, ymax in predictions_bbox:
label = int(label)
# print('label', label, 'xmin', xmin, 'ymin', ymin, 'xmax', xmax, 'ymax', ymax)
display_txt = '{:0.2f}, {}'.format(score, label)
coords = (xmin, ymin), xmax-xmin+1, ymax-ymin+1
color = colors[label]
current_axis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
display_txt = '{:0.2f}, {}'.format(score, label)
current_axis.text(xmin, ymin, display_txt, bbox={'facecolor':color, 'alpha':0.5})
def overlap(xmin, ymin, xmax, ymax, xmin2, ymin2, xmax2, ymax2):
return xmin < xmax2 and xmax > xmin2 and ymin < ymax2 and ymax > ymin2
def unify_bboxes(predictions_bbox):
'''
Somtimes SSD will predict multiple bounding boxes on top of each.
This is very naive :)
'''
r=[]
for label, score, xmin, ymin, xmax, ymax in predictions_bbox:
found = False
for label2, score2, xmin2, ymin2, xmax2, ymax2 in predictions_bbox:
if label2==label and score2>score and overlap(xmin, ymin, xmax, ymax, xmin2, ymin2, xmax2, ymax2):
found=True
break
if not found:
r.append((label, score, xmin, ymin, xmax, ymax))
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment