我有一个类似的json,取自vgg图片注释器。
"5213c8b86f225.image.jpg40172": {
"filename": "5213c8b86f225.image.jpg",
"regions": [{(...)},
{(...)}]
}
我想将此json转换为掩码rcnn读取的格式:
"5213c8b86f225.image.jpg40172":{
"filename":"24631331976_defa3bb61f_k.jpg",
"regions":{
"0":{(...)},
"1":{(...)},
(...)}
}
我尝试了以下代码:
with open(val_json, 'r+') as f:
vgg_json = json.load(f)
mask_json =vgg_json
for key in vgg_json:
i=0
for shape_attr in vgg_json[key]['regions']:
mask_json[key]['regions'][str(i)]=shape_attr
i+=1
f.seek(0)
json.dump(mask_json, f, indent=4)
f.truncate()
它不起作用,并给我以下错误:
TypeError: list indices must be integers or slices, not str
整个json文件在这里: https://github.com/fabioaraujopt/maskrcnn_custom/blob/master/via_region_data.json
对不起,“嘿”只是我的考验! 现在,真实值已更改!
答案 0 :(得分:0)
您需要复制每个子词典,但将regions
替换为字典
with open(val_json) as f:
vgg_json = json.load(f)
mask_json = {
k: dict(v, regions=dict(enumerate(v['regions']))
for k, v in vgg_json.items()
}
with open(val_json, 'w') as f:
json.dump(mask_json, f, indent=4)
答案 1 :(得分:0)
所以,我想这是当您尝试使用字符串键将值分配给列表(//redirect.php
<?php
header('Location: localFile.php');
?>
//localFile.php
<?php
echo "good!";
?>
)时发生的。正如Python有用地指出的那样,您不能这样做。这是您的代码,经过几行修改后可以执行我认为您想做的事情:
mask_json[key]['regions']['hey']=shape_attr