Skip to content
Advertisement

How can i save the name of objects in python in specific order?

I am trying to save the name of objects as image in specific order. Like if there are seven objects detected in image and their names are [chair, tv, bed, chair ,bed, chair, chair]. I want that it should be saved as [chair.png, chair1.png, chair2.png, chair3.png, bed.png, bed1.png, tv.png]. No matter what objects comes first but its numbers should remains in sequential order respectively. I am trying but is it giving me results like: [bed.png, bed2.png, chair.png, chair1.png, chair3.png, chair4.png, tv.png] . I guess i have not setted the count variable correctly but I am unable to find it out

My code:

JavaScript

Advertisement

Answer

You use the same count for all names – but for every name you should have separated count.

You could create dictionary at start

JavaScript

Inside loop you could check if label exists in dictionary

JavaScript

and you could use it to count and generate filename

JavaScript

Something like this:

JavaScript

You could even use count instead of exists

JavaScript

Frankly I would start names with 0chair0.png, bed0.png, etc. Or I would use 1 for first image – chair1.png instead of chair.png

This way all code would be simpler.

If you use many images then I would use names like chair001.png to keep order – because Python (and system) will sort string "10" before "2", but it will keep order if you use "02"

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement