Convert bbox dictionary into list with relative coordinates
If you want to convert a python dictionary with the keys top, left, widht, height
into a list in the format [x1, y1, x2, y2]
Where x1, y1 are the relative coordinates of the top left corner of the bounding box and x2, y2 are the relative coordinates of the bottom right corner of the bounding box you can use the following function :
def bbox_dict_to_list(bbox_dict, image_size):
h = bbox_dict.get('height')
l = bbox_dict.get('left')
t = bbox_dict.get('top')
w = bbox_dict.get('width')
img_w, img_h = image_size
x1 = l/img_w
y1 = t/img_h
x2 = (l+w)/img_w
y2 = (t+h)/img_h
return [x1, y1, x2, y2]
You must pass as arguments the bbox dictionary, and the image size as a tuple -> (image_width, image_height)
Example
bbox = {"top":634,"left":523,"height":103,"width":145}
bbox_dict_to_list(bbox, (1280, 720))
>> [0.40859375, 0.8805555555, 0.521875, 1.02361111111]
You can change the return order to suit your needs