有一个数据结构,内容如下:
anno_data=[['Item1', 'Rectangle', 'label1', '018', '[276, 22]', '[18.0, 21.0]'],
['Item1', 'Polygon', 'label1', '018', '[[199.0, 179.0], [200.0, 179.0], [201.0, 179.0], [202.0, 179.0], [203.0, 179.0], [204.0, 179.0], [207.0, 211.0], [206.0, 211.0], [205.0, 211.0], [204.0, 211.0], [203.0, 211.0], [202.0, 211.0], [201.0, 211.0], [200.0, 211.0], [199.0, 211.0], [198.0, 211.0], [197.0, 211.0], [195.0, 210.0], [194.0, 209.0], [192.0, 207.0], [191.0, 206.0], [192.0, 206.0]]', ''],
['Item1', 'Point', 'label1', '018', '[380, 171]', ''],
['Item2', 'Rectangle', 'label1', '032', '[67, 80]', '[39.0, 46.0]']]
可以看出这是一个列表列表。在第三个位置的每个列表中都有元素“018”和“032”。您需要参与这些元素,并基于它们创建以下结构
{
"032": {
"filename": "032",
"regions": {
"0": {
"shape_attributes": {
"name": "Rectangle",
"x": 67,
"y": 80,
"width": 39.0,
"height": 46.0
},
"region_attributes": {
"label": "label1"
}
}
}
},
"018": {
"filename": "018",
"regions": {
"0": {
"shape_attributes": {
"name": "Rectangle",
"x": 276,
"y": 22,
"width": 18.0,
"height": 21.0
},
"region_attributes": {
"label": "label1"
}
},
"1": {
"shape_attributes": {
"name": "Point",
"cx": 380,
"cy": 171
},
"region_attributes": {
"label": "label1"
}
}
}
}
}
我的代码
def get_w_h(str_wh):
to_lst = list(eval(str_wh))
return to_lst
def all_x_y(fig, coords):
to_lst = list(eval(coords))
if fig == 'Point':
return [[to_lst[0]], [to_lst[1]]]
else:
x = to_lst[0]
y = to_lst[1]
return x,y
dic=dict()
uniq=set([l1[3] for l1 in anno_data])
for k in uniq:
dic[k]=[]
med_d={}
for i in anno_data:
it=0
dic_sh_att = {}
dic_reg_att={}
if i[3] in dic:
if i[1]=='Rectangle':
dic_sh_att['name'] = i[1]
dic_sh_att['x'] = all_x_y(i[1], i[4])[0]
dic_sh_att['y'] = all_x_y(i[1], i[4])[1]
dic_sh_att['width']=get_w_h(i[5])[1]
dic_sh_att['height']= get_w_h(i[5])[0]
else:
dic_sh_att['name'] = i[1]
dic_sh_att['cx'] = all_x_y(i[1], i[4])[0]
dic_sh_att['cy'] = all_x_y(i[1],i[4])[1]
dic_reg_att["label"] = i[2]
dic_reg_att["class"]=str(it)
med_d[it]={"shape_attributes":dic_sh_att,
"region_attributes": dic_reg_att}
dic[i[3]].append(med_d)
it += 1
但结果不一样。我不知道如何正确地做到这一点。帮助请
我根据您答案中的代码生成结果
Polygon的代码,希望我理解正确。如果有问题,请为问题添加示例结果Polygon。结果:
我自己喝醉了。如果有人有更好的解决方案,我会很高兴