json文件格式
{
"annotations": [
{
"filename": "train_images\\00002.jpg",
"box": {
"xmin": 0,
"ymin": 275.4,
"xmax": 116.2,
"ymax": 394.5
},
"label": "Car",
"truncated": 0,
"occluded": 0
},
{
"filename": "train_images\\00002.jpg",
"box": {
"xmin": 222.5,
"ymin": 275.3,
"xmax": 306.9,
"ymax": 328.6
},
"label": "LightTruck",
"truncated": 0,
"occluded": 0
},
{
"filename": "train_images\\00002.jpg",
"box": {
"xmin": 211.5,
"ymin": 279.7,
"xmax": 268.2,
"ymax": 303.9
},
"label": "Car",
"truncated": 0,
"occluded": 1
},
{
"filename": "train_images\\03000.jpg",
"box": {
"xmin": 0,
"ymin": 330.7,
"xmax": 93.1,
"ymax": 363.4
},
"label": "Car",
"truncated": 0,
"occluded": 0
}
]
}
import os
import json
json_file = './train.json'
with open(json_file) as f:
data = json.load(f)
print(type(data)) # 字典格式
data = data['annotations']
# 创建一个字典用来计数,不存在的就赋值1,存在的就加一
label_counts = {}
for item in data:
label = item['label']
try:
label_counts[label] += 1
except:
label_counts[label] = 1
print(label_counts)
'''{'Car': 9193, 'LightTruck': 389, 'LargeBus': 1129, 'van': 1427, 'Truck': 1631, 'Pedestrian': 1809, 'TricycleClosed': 184, 'Pickup': 326, 'Motorcycle': 295, 'HeavyTruck': 1570, 'MotorCyclist': 3552, 'EngineTruck': 257, 'Machineshop': 79, 'BiCyclist': 308, 'TricycleOpenMotor': 280, 'Bike': 54, 'TricycleOpenHuman': 36, 'OtherCar': 21, 'MediumBus': 24, 'PersonSitting': 14, 'CampusBus': 2, 'MMcar': 16}'''
# 所包含的种类:
print(label_counts.keys())
'''dict_keys(['Car', 'LightTruck', 'LargeBus', 'van', 'Truck', 'Pedestrian', 'TricycleClosed', 'Pickup', 'Motorcycle', 'HeavyTruck', 'MotorCyclist', 'EngineTruck', 'Machineshop', 'BiCyclist', 'TricycleOpenMotor', 'Bike', 'TricycleOpenHuman', 'OtherCar', 'MediumBus', 'PersonSitting', 'CampusBus', 'MMcar'])'''