1
votes

I am writing a code on extracting the bounding boxes from an image and then compute IoU with ground truth annotations. My bounding box co-ordinates for both predicted and ground truth are in the form of a list [xmin,ymin,xmax,ymax]. I have written a for loop which extract the bounding information from a dictionary and then stores it in a list.

box = v7wpoint.boxidtobox[answer_boxid]
for key in box:
    #    print key
    xmin_gt = box.get('x')
    ymin_gt = box.get('y')
    height = box.get('height')
    width = box.get('width')
    final_bb = []
    xmax_gt = xmin_gt+width
    ymax_gt = ymin_gt+height
    bb_gt = [xmin_gt,ymin_gt,xmax_gt,ymax_gt]
    final_bb.append(bb_gt)

    rect_gt = np.array([[xmin_gt,ymin_gt],[xmin_gt,ymax_gt],[xmax_gt,ymax_gt],[xmax_gt,ymin_gt],[xmin_gt,ymin_gt]])
    plt.plot(rect_gt[:,0],rect_gt[:,1],'g',markersize=4)
    print (box.get('x'),box.get('y'),box.get('height'),box.get('width'),box.get('name'))

At the end of this for loop I am getting only one bounding box information - final_bb([xmin,ymin,xmax,ymax]). I need a list which contains a list of all bounding box co-ordinates. Something like this

1. [xmin,ymin,xmax,ymax]
2. [xmin,ymin,xmax,ymax]
3. [xmin,ymin,xmax,ymax]
.
.
.
N. [xmin,ymin,xmax,ymax]

I know this is a very trivial question but I am new to python and facing difficulty in manipulating data structures in python.

1
What's the purpose of the for loop? It looks like it's extracting several keys from box, over and over, as many times as there are keys. The loop suite doesn't depend on the value of key, so what does doing the operations repeatedly accomplish? - mwchase
'box' is a dictionary which contains all the ground truth annotation. I am extracting only specific keys from that dictionary. The operations in this for loop is computing the co-ordinates of the box and then plotting it on the image. The other operation is to store the co-ordinates as a list. - Ka93
That doesn't answer the question of, why do the operations once per key, when the behavior doesn't depend on the identity of the key in question. Put another way, you could replace the entire loop body with a function of box, that makes no reference to key, so why does it need to happen a bunch of times? - mwchase

1 Answers

0
votes

Move the assignment final_bb = [] to before the loop, otherwise the result list will get overwritten on each iteration.

Not including full code, because the sample as given still doesn't really make sense to me.