python 统计json文件中的label数目

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'])'''
                    
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值