0

I have to write some complex data to a file that can be easily iterable later on. I have a list of images and for each image I have a list of objects with coordinates for each one of it.

This is my current data format, a list:

[[[x_coord, y_coord], [widh, height], rotation],["object_name", accuracy]], 
[[...]] ....]

What I want is to have all that attached to an image name. So, when I want to iterate each image, i want to be able to read all the above data for each image.

To better understand the problem, I have a system that takes as input an image and outputs a list as the one above. Instead of the variabile names I have values. Now, I want to run more than 1 image with my system but I don't know how to store the output so that I would know to which image the output corresponds to because I want to share the results. This is an example of what my system outputs from an image.

[[[[711.2923254686244, 509.8931226169361], [152.17577602646008, 18.75684392334363], 0.8387087394488912], ["H", 0.9245172388413373, 0]], [[[707.8748537512387, 415.88163813422705], [142.3154146012468, 18.638623686112567], 0.9886537282165556], ["Negative", 0.9904577591839958, 0]], [[[272.4707519306856, 655.1721772586598], [186.6175797860084, 19.211811300659143], 0.22486986712600301], ["plasmid", 0.941743278503418, 0]], [[[124.15456456296585, 250.43897797079646], [29.55055251880988, 15.06842795688405], -9.258418081903768], ["11", 0.9978194236755371, 0]], [[[94.68372611438528, 251.45963820289163], [35.19040782622253, 26.019563655695396], -6.836103324206618], ["C", 0.9792481660842896, 0]], [[[521.6560694750618, 654.7176179324879], [101.80468126671622, 18.683637160515204], 0.3779982099082056], ["RM", 0.970346314566476, 0]], [[[650.0936701718499, 512.4483476526597], [28.275388840998474, 16.91495785856379], 0.715133060422265], ["9", 0.28899475932121277, 0]], [[[681.2160289988799, 416.6489298203412], [63.57085096105481, 15.692085586024415], 2.5319410047875994], ["Empty cell", 0.9971669316291809, 0]], [[[280.7394181980806, 203.18943001242243], [38.38290734128221, 35.22847492151178], 0.6360230675017051], ["L", 0.9367004632949829, 0]], [[[681.4627260320327, 508.57304371104516], [62.768154091603066, 15.44289448080739], -0.5180496117273432], ["test", 0.908484935760498, 0]], [[[727.4159714754891, 415.9454027063706], [66.49714216688848, 15.303173897880407], -0.8295930881855053], ["positive", 0.9496070688421075, 0]]]

Is there a way to have a list of images and for each image to have another list like the one above?

Or what would be the right way to store this if list of list isn't the right way to do it?

I usually code in C and the concept of lists and dictionaries are pretty new to me and I don't quite understand how they fully work, therefore I don't understand when to use them.

7
  • can you specify more about a) your current data format, b) your desired data format, and possibly c) why is the transformation necessary. You could go with a list of tuples, data frame, dict of images to list of tuples ... hard to say which is best Commented Mar 5, 2018 at 11:55
  • I will update the post shortly. Well, it is necessary because I have the objects and if I do not attach them to an image, I will not be able to know where the objects exists. Commented Mar 5, 2018 at 11:57
  • how are your images stored? Just use a dictionary of images to list of lists Commented Mar 5, 2018 at 11:58
  • What about a dictionary? lookup{'image1': [[...,...],...,[...],...], 'image2': [...], ...} Commented Mar 5, 2018 at 11:59
  • I get the name of the images from a text file. So basically I just need to append a list to each one of them. Commented Mar 5, 2018 at 12:00

3 Answers 3

4

A dictionary is ideal as it can be easily referenced and stored as a json file. If I was doing this project, I would create a dictionary of dictionaries where the first level keys are the names of your image files. An example dictionary you might use would be:

data = {'images/example_image1.png':{'x':0, 'y':0, 'w':50, 'h':75, 'r': 0}, 
'images/example_image2.png':{'x':0, 'y':0, 'w':50, 'h':75, 'r': 0}, 
'images/example_image3.png':{'x':0, 'y':0, 'w':50, 'h':75, 'r': 0}}

If your list of images is a txt file, then this might be achieved with something that looks like this:

data = {}
with open('image_list.txt','r') as file:
    # split the image names into a list
    images = file.read().splitlines()
    for img in images:
        # open and gather the values from the image as a dictionary
        values = {}
        # set all the attributes of the values dictionary for this image
        data[img] = values

Now you can use that dictionary with a for loop to open each image and you will already have all the needed information within the dictionary.

for key, value in data.items():
    with open(key) as img:
        # perform operations using the value dictionary

When you are done editing the data dictionary, you can save it with:

import json
with open('data.json', 'w') as file:
    json.dump(data, file)
Sign up to request clarification or add additional context in comments.

6 Comments

I'm trying it right now and let you know it it worked.
Maybe I misunderstand but the keys for me are just list. Shouldn't I iterate through the list as well?
listofimg = [] for key, value in detections.items(): with open('images.txt') as img: data = {} /* this section is unclear for me because I have a list*/ listofimg.append(data)
The idea is that you would first transform your data into a dictionary and then you can perform operations on that dictionary. Getting from the text file you have to a dictionary would require a sample of the text file you are working with.
I see, so I should first transform this list: [[[x_coord, y_coord], [widh, height], rotation],["object_name", accuracy]], [[...]] ....] into a dictionary first.
|
3

I recommend using collections.namedtuple:

import collections

Image = collections.namedtuple('Image',['size', 'values', 'cooridinates']) #for example
my_image = Image([20, 200], [300,24], [209, 300]) #mock values

then the output will be:

In []: my_image
Out[]: Image(size=[20, 200], values=[300, 24], cooridinates=[209, 300])

Comments

1

If you can use a class for the images:

class img(object):
    __init__(coords=[0, 0], size=[0, 0], etc.):
        self.coords=coords
        self.size=size
        ...

image1=img(coords=[100, 2], size=[50, 100])

Then to retrieve a value:

img1_coords=image1.coords

Comments

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.