TypeError: string indices must be integers. It always shows that TypeError: string indices must be integers. What is the problem?
Question:
TypeError: string indices must be integers. It always shows that TypeError: string indices must be integers. What is the problem?
IMG_DIR = 'default/*.png'
JSON = 'annotations/default.json'
Type: <class 'dict'>
{'info': {}, 'categories': {'label': {'labels': [{'name': 'keypoints', 'parent': '', 'attributes': []}], 'attributes': ['occluded']}, 'points': {'items': []}}, 'items': [{'id': '1024_2048_eduart_420210919_151508', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [326.46, 438.63], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [499.41, 452.61], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [430.61, 454.51], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [37.47, 467.43], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 0}, 'image': {'path': '1024_2048_eduart_420210919_151508.png', 'size': [512, 512]}, 'media': {'path': '1024_2048_eduart_420210919_151508.png'}}, {'id': '1536_1024_imranparkhaus_rub_32', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [234.13, 427.51], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 1}, 'image': {'path': '1536_1024_imranparkhaus_rub_32.png', 'size': [512, 512]}, 'media': {'path': '1536_1024_imranparkhaus_rub_32.png'}}, {'id': '1536_1536_eduart_5642', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [151.31, 437.21], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 2}, 'image': {'path': '1536_1536_eduart_5642.png', 'size': [512, 512]}, 'media': {'path': '1536_1536_eduart_5642.png'}}, {'id': '1536_2560_evgenii_NL_3_120200916_154505', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [51.14, 399.35], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [458.94, 104.43], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 3}, 'image': {'path': '1536_2560_evgenii_NL_3_120200916_154505.png', 'size': [512, 512]}, 'media': {'path': '1536_2560_evgenii_NL_3_120200916_154505.png'}}, {'id': '2048_1024_imran20200801_171936', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [298.43, 411.32], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 4}, 'image': {'path': '2048_1024_imran20200801_171936.png', 'size': [512, 512]}, 'media': {'path': '2048_1024_imran20200801_171936.png'}}, {'id': '2048_1024_imran20200801_184730', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [480.6, 451.42], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 5}, 'image': {'path': '2048_1024_imran20200801_184730.png', 'size': [512, 512]}, 'media': {'path': '2048_1024_imran20200801_184730.png'}}, {'id': '2048_1536_imran20200801_171936', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [4.51, 353.86], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [320.5, 270.6], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [395.73, 240.58], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [169.28, 344.01], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 6}, 'image': {'path': '2048_1536_imran20200801_171936.png', 'size': [512, 512]}, 'media': {'path': '2048_1536_imran20200801_171936.png'}}, {'id': '512_1024_eduart_420210722_170544', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [506.42, 188.26], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [195.07, 50.01], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [68.4, 42.31], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [176.4, 222.6], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [349.96, 240.88], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 7}, 'image': {'path': '512_1024_eduart_420210722_170544.png', 'size': [512, 512]}, 'media': {'path': '512_1024_eduart_420210722_170544.png'}}, {'id': '512_1024_eduart_5493', 'annotations': [], 'attr': {'frame': 8}, 'image': {'path': '512_1024_eduart_5493.png', 'size': [512, 512]}, 'media': {'path': '512_1024_eduart_5493.png'}}, {'id': '512_1024_eduart_5840', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [360.2, 476.5], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [355.6, 404.8], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [410.1, 121.3], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 9}, 'image': {'path': '512_1024_eduart_5840.png', 'size': [512, 512]}, 'media': {'path': '512_1024_eduart_5840.png'}}, {'id': '512_2048_eduart_5702', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [336.33, 51.5], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [201.1, 421.4], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [271.4, 115.9], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 10}, 'image': {'path': '512_2048_eduart_5702.png', 'size': [512, 512]}, 'media': {'path': '512_2048_eduart_5702.png'}}, {'id': '512_2560_eduart_5extra_35', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [494.65, 24.64], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [349.52, 295.38], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 11}, 'image': {'path': '512_2560_eduart_5extra_35.png', 'size': [512, 512]}, 'media': {'path': '512_2560_eduart_5extra_35.png'}}, {'id': '512_2560_evgenii_Ext_120201005_142724', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [434.69, 169.45], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [392.64, 212.4], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 12}, 'image': {'path': '512_2560_evgenii_Ext_120201005_142724.png', 'size': [512, 512]}, 'media': {'path': '512_2560_evgenii_Ext_120201005_142724.png'}}, {'id': '512_512_imran20200801_170919', 'annotations': [], 'attr': {'frame': 13}, 'image': {'path': '512_512_imran20200801_170919.png', 'size': [512, 512]}, 'media': {'path': '512_512_imran20200801_170919.png'}}]}
with open(JSON) as infile:
json_data = json.load(infile)
json_dict = {i['path']: i for i in json_data}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_3884/156696908.py in <module>
1 with open(JSON) as infile:
2 json_data = json.load(infile)
----> 3 json_dict = {i['path']: i for i in json_data}
~AppDataLocalTemp/ipykernel_3884/156696908.py in <dictcomp>(.0)
1 with open(JSON) as infile:
2 json_data = json.load(infile)
----> 3 json_dict = {i['path']: i for i in json_data}
TypeError: string indices must be integers
What is the problem here?
Answers:
Well, seems that you are looking for path
for your each value. So you need to be aware of your data frame. Because where your path
is located is in your items
only. That’s why you are facing above error. Please the following code where your path
s are collected.
json_dict = {}
for k,v in json_data.items():
if k == "items":
for i in v:
for k1,v1 in i.items():
if type(v1) == dict: # Seems, the "path" only includes in a dict {'path': '2048_1024_imran20200801_184730.png', 'size': [512, 512]}
for k11,v11 in v1.items():
if k11 == "path":
json_dict[f"{i['id']}--{k1}-path"] = v11
See code output below, you may edit your the key values as you wish by editing json_dict.
TypeError: string indices must be integers. It always shows that TypeError: string indices must be integers. What is the problem?
IMG_DIR = 'default/*.png'
JSON = 'annotations/default.json'
Type: <class 'dict'>
{'info': {}, 'categories': {'label': {'labels': [{'name': 'keypoints', 'parent': '', 'attributes': []}], 'attributes': ['occluded']}, 'points': {'items': []}}, 'items': [{'id': '1024_2048_eduart_420210919_151508', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [326.46, 438.63], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [499.41, 452.61], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [430.61, 454.51], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [37.47, 467.43], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 0}, 'image': {'path': '1024_2048_eduart_420210919_151508.png', 'size': [512, 512]}, 'media': {'path': '1024_2048_eduart_420210919_151508.png'}}, {'id': '1536_1024_imranparkhaus_rub_32', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [234.13, 427.51], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 1}, 'image': {'path': '1536_1024_imranparkhaus_rub_32.png', 'size': [512, 512]}, 'media': {'path': '1536_1024_imranparkhaus_rub_32.png'}}, {'id': '1536_1536_eduart_5642', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [151.31, 437.21], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 2}, 'image': {'path': '1536_1536_eduart_5642.png', 'size': [512, 512]}, 'media': {'path': '1536_1536_eduart_5642.png'}}, {'id': '1536_2560_evgenii_NL_3_120200916_154505', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [51.14, 399.35], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [458.94, 104.43], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 3}, 'image': {'path': '1536_2560_evgenii_NL_3_120200916_154505.png', 'size': [512, 512]}, 'media': {'path': '1536_2560_evgenii_NL_3_120200916_154505.png'}}, {'id': '2048_1024_imran20200801_171936', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [298.43, 411.32], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 4}, 'image': {'path': '2048_1024_imran20200801_171936.png', 'size': [512, 512]}, 'media': {'path': '2048_1024_imran20200801_171936.png'}}, {'id': '2048_1024_imran20200801_184730', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [480.6, 451.42], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 5}, 'image': {'path': '2048_1024_imran20200801_184730.png', 'size': [512, 512]}, 'media': {'path': '2048_1024_imran20200801_184730.png'}}, {'id': '2048_1536_imran20200801_171936', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [4.51, 353.86], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [320.5, 270.6], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [395.73, 240.58], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [169.28, 344.01], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 6}, 'image': {'path': '2048_1536_imran20200801_171936.png', 'size': [512, 512]}, 'media': {'path': '2048_1536_imran20200801_171936.png'}}, {'id': '512_1024_eduart_420210722_170544', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [506.42, 188.26], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [195.07, 50.01], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [68.4, 42.31], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [176.4, 222.6], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [349.96, 240.88], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 7}, 'image': {'path': '512_1024_eduart_420210722_170544.png', 'size': [512, 512]}, 'media': {'path': '512_1024_eduart_420210722_170544.png'}}, {'id': '512_1024_eduart_5493', 'annotations': [], 'attr': {'frame': 8}, 'image': {'path': '512_1024_eduart_5493.png', 'size': [512, 512]}, 'media': {'path': '512_1024_eduart_5493.png'}}, {'id': '512_1024_eduart_5840', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [360.2, 476.5], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [355.6, 404.8], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [410.1, 121.3], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 9}, 'image': {'path': '512_1024_eduart_5840.png', 'size': [512, 512]}, 'media': {'path': '512_1024_eduart_5840.png'}}, {'id': '512_2048_eduart_5702', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [336.33, 51.5], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [201.1, 421.4], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [271.4, 115.9], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 10}, 'image': {'path': '512_2048_eduart_5702.png', 'size': [512, 512]}, 'media': {'path': '512_2048_eduart_5702.png'}}, {'id': '512_2560_eduart_5extra_35', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [494.65, 24.64], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [349.52, 295.38], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 11}, 'image': {'path': '512_2560_eduart_5extra_35.png', 'size': [512, 512]}, 'media': {'path': '512_2560_eduart_5extra_35.png'}}, {'id': '512_2560_evgenii_Ext_120201005_142724', 'annotations': [{'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [434.69, 169.45], 'z_order': 0, 'visibility': [2]}, {'id': 0, 'type': 'points', 'attributes': {'occluded': False}, 'group': 0, 'label_id': 0, 'points': [392.64, 212.4], 'z_order': 0, 'visibility': [2]}], 'attr': {'frame': 12}, 'image': {'path': '512_2560_evgenii_Ext_120201005_142724.png', 'size': [512, 512]}, 'media': {'path': '512_2560_evgenii_Ext_120201005_142724.png'}}, {'id': '512_512_imran20200801_170919', 'annotations': [], 'attr': {'frame': 13}, 'image': {'path': '512_512_imran20200801_170919.png', 'size': [512, 512]}, 'media': {'path': '512_512_imran20200801_170919.png'}}]}
with open(JSON) as infile:
json_data = json.load(infile)
json_dict = {i['path']: i for i in json_data}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_3884/156696908.py in <module>
1 with open(JSON) as infile:
2 json_data = json.load(infile)
----> 3 json_dict = {i['path']: i for i in json_data}
~AppDataLocalTemp/ipykernel_3884/156696908.py in <dictcomp>(.0)
1 with open(JSON) as infile:
2 json_data = json.load(infile)
----> 3 json_dict = {i['path']: i for i in json_data}
TypeError: string indices must be integers
What is the problem here?
Well, seems that you are looking for path
for your each value. So you need to be aware of your data frame. Because where your path
is located is in your items
only. That’s why you are facing above error. Please the following code where your path
s are collected.
json_dict = {}
for k,v in json_data.items():
if k == "items":
for i in v:
for k1,v1 in i.items():
if type(v1) == dict: # Seems, the "path" only includes in a dict {'path': '2048_1024_imran20200801_184730.png', 'size': [512, 512]}
for k11,v11 in v1.items():
if k11 == "path":
json_dict[f"{i['id']}--{k1}-path"] = v11
See code output below, you may edit your the key values as you wish by editing json_dict.