使用python对于labelme画框统计
对于文件夹下所有json文件统计总框数和各个标签的数量
案例一,文件目录如下:
import json
from os import listdir
path = "C:\\Users\\Administrator\\Desktop\\航海画圈\\dataf6\\" # .json文件目录
file_list = listdir(path) # 所有的.json文件
file_index = [] # 准备存放文件索引,为了按顺序读文件
print(file_list)
print(file_list[1].split('.')[1])
for i in range(len(file_list)):
# 把文件名里的数字提出来排序
if(file_list[i].split('.')[1]=='json'):
idx = file_list[i].split('.')[0].split("000")[1]
file_index.append(int(idx))
print(file_index)
file_index = sorted(file_index)
total_label = [] # 统计所有文件的label标签数量
for file in file_index:
if(file<=100):
filename = path + 'DJI_0347_0000' + str(file) + '.json'
else:
filename = path + 'DJI_0347_000' + str(file) + '.json'
with open(filename, 'r') as f:
label_list = json.load(f)
# print(label_list)
t_dict = {} # 每个文件的label标签数量
for i, label_dict in enumerate(label_list['shapes']):
# print(label_dict['label'])
if label_dict['label'] not in t_dict:
t_dict[label_dict['label']] = 1
else:
t_dict[label_dict['label']] += 1
print(filename, "label统计:", t_dict)
total_label.append(t_dict)
# print(total_label)
# 统计所有文件的label标签数量,算是一个整合吧
temp = {}
for i, labels in enumerate(total_label):
# print(labels)
for j, label in enumerate(labels):
if label not in temp:
temp[label] = labels[label]
else:
temp[label] += labels[label]
sum = 0
for value in temp.values():
if isinstance(value, int):
sum += value
print(sum)
print(temp)
运行结果如下:
[‘DJI_0347_000060.jpg’, ‘DJI_0347_000060.json’, ‘DJI_0347_000120.jpg’, ‘DJI_0347_000120.json’, ‘DJI_0347_000180.jpg’, ‘DJI_0347_000180.json’, ‘DJI_0347_000240.jpg’, ‘DJI_0347_000240.json’, ‘DJI_0347_000300.jpg’, ‘DJI_0347_000300.json’, ‘DJI_0347_000360.jpg’, ‘DJI_0347_000360.json’]
json
[60, 120, 180, 240, 300, 360]
C:\Users\Administrator\Desktop\航海画圈\dataf6\DJI_0347_000060.json label统计: {‘obstacle’: 4, ‘land’: 1, ‘sea’: 2, ‘sky’: 1, ‘colseship’: 4}
C:\Users\Administrator\Desktop\航海画圈\dataf6\DJI_0347_000120.json label统计: {‘obstacle’: 3, ‘sea’: 3, ‘sky’: 1}
C:\Users\Administrator\Desktop\航海画圈\dataf6\DJI_0347_000180.json label统计: {‘obstacle’: 4, ‘sea’: 2, ‘land’: 1, ‘sky’: 1}
C:\Users\Administrator\Desktop\航海画圈\dataf6\DJI_0347_000240.json label统计: {‘obstacle’: 3, ‘land’: 1, ‘sea’: 2, ‘sky’: 1}
C:\Users\Administrator\Desktop\航海画圈\dataf6\DJI_0347_000300.json label统计: {‘obstacle’: 4, ‘land’: 1, ‘sea’: 2, ‘sky’: 1}
C:\Users\Administrator\Desktop\航海画圈\dataf6\DJI_0347_000360.json label统计: {‘obstacle’: 4, ‘land’: 1, ‘sea’: 2, ‘sky’: 1}
50
{‘obstacle’: 22, ‘land’: 5, ‘sea’: 13, ‘sky’: 6, ‘colseship’: 4}
案例二,文目录如下:
import json
from os import listdir
path = "C:\\Users\\Administrator\\Desktop\\航海画圈\\第二次\\DJI_0416\\" # .json文件目录
file_list = listdir(path) # 所有的.json文件
file_index = [] # 准备存放文件索引,为了按顺序读文件
print(file_list)
print(file_list[1].split('.')[1])
for i in range(len(file_list)):
# 把文件名里的数字提出来排序
if(file_list[i].split('.')[1]=='json'):
idx = file_list[i].split('.')[0].split("16_0")[1]
file_index.append(int(idx))
print(file_index)
file_index = sorted(file_index)
total_label = [] # 统计所有文件的label标签数量
for file in file_index:
if(file<100):
filename = path + 'DJI_0416_0000' + str(file) + '.json'
elif(file>1000 and file<10000):
filename = path + 'DJI_0416_00' + str(file) + '.json'
elif(file>=100 and file<=1000):
filename = path + 'DJI_0416_000' + str(file) + '.json'
else:
filename = path + 'DJI_0416_0' + str(file) + '.json'
with open(filename, 'r') as f:
label_list = json.load(f)
# print(label_list)
t_dict = {} # 每个文件的label标签数量
for i, label_dict in enumerate(label_list['shapes']):
# print(label_dict['label'])
if label_dict['label'] not in t_dict:
t_dict[label_dict['label']] = 1
else:
t_dict[label_dict['label']] += 1
print(filename, "label统计:", t_dict)
total_label.append(t_dict)
# print(total_label)
# 统计所有文件的label标签数量,算是一个整合吧
temp = {}
for i, labels in enumerate(total_label):
# print(labels)
for j, label in enumerate(labels):
if label not in temp:
temp[label] = labels[label]
else:
temp[label] += labels[label]
sum = 0
for value in temp.values():
if isinstance(value, int):
sum += value
print(sum) # 输出结果为 6
print(temp)
结果部分展示如下: