栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 统计json文件中的label数目

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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'])'''
                    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/966121.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号