读取一个MSCOCO格式的json文件,统计数据集中的boundingbox尺寸分布,把height、width信息保存到两个list中去,然后绘制统计图,可视化尺寸分布。
还可以自己加一些别的功能
import json
import matplotlib.pyplot as plt
def bbox_distribution(json_file, small_h, small_w):
with open(json_file, 'r') as f:
data = json.load(f)
bbox_widths = []
bbox_heights = []
for annotation in data['annotations']:
bbox = annotation['bbox']
bbox_widths.append(bbox[2])
bbox_heights.append(bbox[3])
# 如果height或者width小于阈值,把annotation信息保存到一个txt文件
if bbox[2] < small_w or bbox[3] < small_h:
with open('small.txt', 'a') as f:
f.write(str(annotation) + '\n')
return bbox_widths, bbox_heights
def plot_distribution(widths, heights):
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.hist(widths, bins=40, color='blue', edgecolor

最低0.47元/天 解锁文章
4590






