[英]How to handle JSON nested lists with python
我正在訓練掩碼 rcnn 上的數據集。 我已經在 labelIMG 工具 ( https://github.com/tzutalin/labelImg ) 上注釋了大約 1500 張圖像。
長話短說,我需要從 JSON 文件中的分段列表中獲取 x 和 y 坐標的值。
如何使用 Python 編程訪問列表? 或者有沒有其他方法可以在掩碼 Rcnn 上使用 .xml 注釋。
這是從 VOC PASCAL 轉換為 COCO 的數據集。 Xml 被轉換為 JSON 語法。
代碼
import json
import codecs
data = json.load(codecs.open('example.json', 'r', 'utf-8-sig'))
for i in data['annotations']:
print(data['annotations'][0]) #want to output segmentation values in JSON files
JSON 文件
{
"images": [
{
"file_name": "out538.png",
"height": 720,
"id": 20180000001,
"width": 1280
},
{
"file_name": "3 0751.jpg",
"height": 720,
"id": 20180000002,
"width": 1280
}
],
"type": "instances",
"annotations": [
{
"segmentation": [
[
935,
372,
935,
554,
1195,
554,
1195,
372
]
],
"area": 47320,
"iscrowd": 0,
"ignore": 0,
"image_id": 20180000001,
"bbox": [
935,
372,
260,
182
],
"category_id": 1,
"id": 1
},
{
"segmentation": [
[
743,
317,
743,
480,
962,
480,
962,
317
]
],
"area": 35697,
"iscrowd": 0,
"ignore": 0,
"image_id": 20180000001,
"bbox": [
743,
317,
219,
163
],
"category_id": 1,
"id": 2
}
],
"categories": [
{
"supercategory": "none",
"id": 1,
"name": "bike"
},
{
"supercategory": "none",
"id": 2,
"name": "Bike"
}
]
}
我想要分段列表的值:例如 935, 372, 935, 554, 1195, 554, 1195, 372 但我得到的只是錯誤“列表索引必須是整數或切片,而不是字典”
JSON 是 ...dicts 的 dict 的 dict 所以你需要正確的鍵來導航到段。
annotations[0]['segmentation']
應該給你名單
您的for i in data['annotations']:
循環中的i
變量將是一個字典,因為annotations
是一個字典列表。 為了訪問segmentation
列表,您需要執行以下操作:
for annotation in data['annotations']:
segmentation = annotation['segmentation']
actual_segment_data = segmentation[0]
最后一行代碼是必需的,因為segmentation
是列表中的列表。
這應該返回以下內容: [935, 372, 935, 554, 1195, 554, 1195, 372]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.