[英]Accessing a single dictionary in a list inside a json file
I have JSON file with the structure like this.我有这样结构的 JSON 文件。 This JSON file is the annotation data from my dataset, and I want to used for my YOLO object detection.这个 JSON 文件是我数据集中的注释数据,我想用于我的 YOLO 对象检测。
{
"description": "",
"tags": [],
"size": {
"height": 720,
"width": 1280
},
"objects": [
{
"id": 768275656,
"classId": 3753641,
"description": "",
"geometryType": "rectangle",
"labelerLogin": "monochloride",
"createdAt": "2021-07-20T00:50:24.512Z",
"updatedAt": "2021-07-20T00:50:24.512Z",
"tags": [],
"classTitle": "II",
"points": {
"exterior": [
[
383,
230
],
[
659,
417
]
],
"interior": []
}
}
]
}
What I want to do is parsing the information from "exterior"
in "objects"
field, which contains the information of bounding box but I haven't succeeded in doing it.我想要做的是解析来自"exterior"
"objects"
字段中"objects"
的信息,其中包含边界框的信息,但我没有成功。
Previously I tried to access the value using this code以前我尝试使用此代码访问该值
x1 = data['objects'][0]['points']['exterior'][0][0]
y1 = data['objects'][0]['points']['exterior'][0][1]
x2 = data['objects'][0]['points']['exterior'][1][0]
y2 = data['objects'][0]['points']['exterior'][1][1]
but the program throws但程序抛出
IndexError: list index out of range
.IndexError: list index out of range
。
Where did I do it wrong, and how to solve it?我哪里做错了,如何解决?
It seems that you were missing a opening curly brace, the following based on your code works just fine:似乎您缺少一个左花括号,以下基于您的代码工作正常:
import json
# open json file
with open('yoloobject.json', 'r') as f:
jsonstring = f.read()
f.seek(0,0)
data = json.load(f)
print('Json String: ', jsonstring)
x1 = data['objects'][0]['points']['exterior'][0][0]
y1 = data['objects'][0]['points']['exterior'][0][1]
x2 = data['objects'][0]['points']['exterior'][1][0]
y2 = data['objects'][0]['points']['exterior'][1][1]
print(f'bounding box: ({x1},{y2}), ({x2},{y2})')
with outputs:输出:
Json String: {
"description": "",
"tags": [],
"size": {
"height": 720,
"width": 1280
},
"objects": [{
"id": 768275656,
"classId": 3753641,
"description": "",
"geometryType": "rectangle",
"labelerLogin": "monochloride",
"createdAt": "2021-07-20T00:50:24.512Z",
"updatedAt": "2021-07-20T00:50:24.512Z",
"tags": [],
"classTitle": "II",
"points": {
"exterior": [
[
383,
230
],
[
659,
417
]
],
"interior": []
}
}
]
}
bounding box: (383,417), (659,417)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.