I have JSON file with the structure like this. This JSON file is the annotation data from my dataset, and I want to used for my YOLO object detection.
{
"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.
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
.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.