我必须将一些复杂的数据写入文件中,以便以后可以轻松地进行迭代。我有一个图像列表,对于每一个图像,我有一个对象列表,其中每个对象都有坐标。
这是我目前的数据格式,一个列表:
[[[x_coord, y_coord], [widh, height], rotation],["object_name", accuracy]],
[[...]] ....]
我想要的是把所有的东西都附在一个形象的名字上。因此,当我想迭代每个图像时,我希望能够读取每个图像的所有上述数据。
为了更好地理解这个问题,我有一个系统,它以图像作为输入,并将列表输出为上面的列表。而不是变量名,我有值。现在,我想用我的系统运行一个以上的映像,但是我不知道如何存储输出,以便知道输出对应于哪个映像,因为我想共享结果。这是我的系统从图像中输出的一个例子。
[711.2923254686244,509.8931226169361,152.17577602646008,18.75684392334363,18.75684392334363],"H",18.75684392334363,0],[[707.8748537512387,415.88163813422705,142.3154146012468,1244,吸附],“阴性”,居间,0],[[272.4707519306856,655.1721772586598,186.6175797860084,Negative,],“质粒”,H,0],[124.15456456296585,250.43897797079646,29.55055251880988,15.06842795688405,-9.258418081903768,"11",0.9978194236755371,0],[94.68372611438528,251.45963820289163,35.19040782622253,26.019563655695396,-26.019563655695396],"C",26.019563655695396,0],[521.6560694750618,654.7176179324879,101.80468126671622,C.12,0.3779982099082056],"RM",0.970346314566476,0],[650.0936701718499,512.4483476526597,28.275388840998474,16.91495785856379,0.7151330422265],"9",0.288994759321277,0],[[681.2160289988799,416.6489298203412,63.57085096105481,15.692085586024415,2.5319410047875994],“空细胞”,2.5319410047875994,0],[280.7394181980806,203.18943001242243,2.5319410047875994,吸附,0],[[681.4627260320327,508.57304371104516,62.768154091603066,15.442894480739,-0.5180496117273432],“测试”,0.908484935760498,0],[727.4159714754891,415.9454027063706,66.49714216688848,15.30317389880407,[727.4159714754891,415.9454027063706,66.49714216688848,15.3031738978407,-0.8295930881855053],“肯定”,0.9496070688421075,0]]
是否有一种方法可以得到一个图像列表,并且每个图像都有另一个列表(如上面的列表)?
或者,如果列表不是正确的方法,那么存储这个列表的正确方法是什么呢?
我通常用C编写代码,列表和字典的概念对我来说是很新的,我不太明白它们是如何完全工作的,因此我不知道什么时候使用它们。
发布于 2018-03-05 12:03:21
字典是理想的,因为它可以很容易地引用和存储为一个json文件。如果我正在做这个项目,我将创建一个字典字典,其中的第一级键是您的图像文件的名称。您可能使用的示例字典如下:
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}}
如果您的图像列表是一个txt文件,那么这可以通过如下所示的内容来实现:
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
现在,您可以使用带有for循环的字典来打开每个图像,并且字典中已经有了所需的所有信息。
for key, value in data.items():
with open(key) as img:
# perform operations using the value dictionary
编辑完数据字典后,可以使用以下方法保存它:
import json
with open('data.json', 'w') as file:
json.dump(data, file)
发布于 2018-03-05 12:03:39
我建议使用collections.namedtuple
import collections
Image = collections.namedtuple('Image',['size', 'values', 'cooridinates']) #for example
my_image = Image([20, 200], [300,24], [209, 300]) #mock values
然后输出将是:
In []: my_image
Out[]: Image(size=[20, 200], values=[300, 24], cooridinates=[209, 300])
发布于 2018-03-05 12:31:21
如果可以使用类来处理图像:
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])
然后检索一个值:
img1_coords=image1.coords
https://stackoverflow.com/questions/49109903
复制相似问题