0

I have a base64 encoded tsv file containing image information. I read it and decoded it. Here I am pasting the information of one image. The format is as follows:

{'199476': array([[194.04953 , 184.43268 , 639.      , 425.29    ],
       [318.4676  ,  15.762019, 421.1574  , 253.30766 ],
       [219.09093 , 258.60388 , 452.2419  , 384.9459  ],
       [  0.      ,  47.438347, 480.05542 , 425.29    ],
       [  0.      ,   0.      , 363.19708 , 279.19672 ],
       [148.79245 ,   0.      , 639.      , 210.85545 ],
       [  0.      ,   0.      , 258.4733  , 334.14972 ],
       [158.05794 , 320.6399  , 501.2573  , 425.29    ],
       [452.9518  ,   0.      , 639.      , 165.6061  ],
       [  0.      , 255.72142 , 407.57358 , 425.29    ],
       [  0.      ,  22.864079, 140.49962 , 183.17285 ],
       [361.452   , 212.55576 , 639.      , 388.3693  ],
       [554.8864  ,   0.      , 639.      , 101.53771 ],
       [404.05536 , 185.98639 , 578.6646  , 228.58595 ],
       [  0.      , 140.13982 , 123.02028 , 159.72864 ],
       [244.72183 , 250.01173 , 433.06476 , 369.9772  ],
       [128.43852 , 236.70012 , 473.5753  , 369.5989  ],
       [ 52.357124,  39.874092,  75.0104  ,  59.43568 ],
       [144.01859 , 125.3886  , 227.6077  , 204.53206 ],
       [236.9453  ,   0.      , 338.77094 , 123.54699 ],
       [543.54865 , 229.0633  , 627.2213  , 327.51517 ],
       [ 30.98246 , 158.72491 , 326.61508 , 277.84668 ],
       [338.70795 ,  80.93436 , 405.304   , 245.34258 ],
       [151.49304 , 129.96037 , 177.9043  , 200.56523 ],
       [ 94.5418  , 197.5486  , 316.87244 , 253.77058 ],
       [354.94272 ,   0.      , 464.82605 , 226.65717 ],
       [298.1035  , 156.23262 , 315.1587  , 191.82831 ],
       [414.6097  , 133.66512 , 639.      , 222.94992 ],
       [ 95.302864,  92.23008 , 126.67215 , 141.62634 ],
       [184.92436 , 133.2275  , 211.72473 , 199.64825 ],
       [446.2884  , 203.46916 , 631.4623  , 366.25217 ],
       [ 73.46329 , 189.02185 , 332.6924  , 204.93848 ],
       [575.9691  , 204.70457 , 639.      , 244.41171 ],
       [  9.975321, 115.68157 ,  88.33198 , 147.83711 ]], dtype=float32)}

The code I used is given below:

if __name__ == '__main__':
in_data = {}
with open(infile, "r+b") as tsv_in_file:
    reader = csv.DictReader(codecs.iterdecode(tsv_in_file,'utf-8'), delimiter='\t', fieldnames = FIELDNAMES)
    #print(reader)
    for item in reader:
        for field in ['boxes']:
            item[field] = np.frombuffer(base64.b64decode(item[field]), dtype=np.float32).reshape((int(item['num_boxes']),-1))
            in_data[item['image_id']]= item['boxes']
        break


print (in_data)

Now I am trying to save the values after applying formulas on array elements like this

    for item in reader:
        for field in ['boxes']:
            item[field] = np.frombuffer(base64.b64decode(item[field]), dtype=np.float32).reshape((int(item['num_boxes']),-1))
            in_data[item['image_id']] = (((item['boxes'][1][0])-(item['boxes'][0][0]))/(item['boxes'][0][2]))#[0]#[0]#[j]#[j]#[((i+1)-(j))]
            in_data[item['image_id']] = (((item['boxes'][1][1])-(item['boxes'][0][1]))/(item['boxes'][0][3]))
            in_data[item['image_id']] = ((item['boxes'][1][2])/(item['boxes'][0][2]))
            in_data[item['image_id']] = ((item['boxes'][1][3])/(item['boxes'][0][3]))
            in_data[item['image_id']]=(((item['boxes'][1][2])*(item['boxes'][1][3]))/((item['boxes'][0][2])*(item['boxes'][0][3])))
print (in_data)

But it only saves the last result. I want to save all of its answer as a 1st list containing 5 columns. How can I do that?

Also, I did it for 1st and 2nd list. I want to do it for 1st and 3rd list and upto to number of lists. I have hardcoded the indexes values. I need a loop but I am unable to apply loop here. Please help me with that

1 Answer 1

1

you're always rewriting your result. try one of those

append version

in_data[item['image_id']] = []
in_data[item['image_id']].append(your_code_for_column_0)
in_data[item['image_id']].append(your_code_for_column_1)
...

assigning version

in_data[item['image_id']] = [0]*5
in_data[item['image_id']][0] = your_code_for_column_0
in_data[item['image_id']][1] = your_code_for_column_1
...
Sign up to request clarification or add additional context in comments.

5 Comments

it gives a key error KeyError Traceback (most recent call last) <ipython-input-5-3b85e6db192e> in <module>() 28 item[field] = np.frombuffer(base64.b64decode(item[field]), dtype=np.float32).reshape((int(item['num_boxes']),-1)) 29 #for j in item[field]: ---> 30 in_data[item['image_id']][0] = (((item['boxes'][1][0])- KeyError: '199476'
thanks but it didn't work too. ---> 32 in_data[item['image_id']][0] = (((item['boxes'][1][0])-(item['boxes'][0][0]))/(item['boxes'][0][2])) 33 in_data[item['image_id']][1] = (((item['boxes'][1][1])-(item['boxes'][0][1]))/(item['boxes'][0][3])) 34 in_data[item['image_id']][2] = ((item['boxes'][1][2])/(item['boxes'][0][2])) IndexError: list assignment index out of range
i knew i should have stayed in bed. sorry again, i think this time it's ok
It gives the same error but this time on second line. in_data[item['image_id']][1] = (((item['boxes'][1][1])-(item['boxes'][0][1]))/(item['boxes'][0][3])). I think I should "append" do you know where to put append
i don't understand why it doesn't work but today is a weird day. i edited

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.