统计labelme标注中各个类别的数量,生成柱状图和txt文件
import os
import json
import glob
from collections import Counter
import matplotlib.pyplot as plt
# === 输入目录(包含json标注文件) ===
input_dir = r"D:\plant_seg_datasets\20250524\image" # 包含 LabelMe 的 JSON 文件
output_dir = os.path.dirname(input_dir) # 输出目录(20250524)
# === 输出路径 ===
output_plot_path = os.path.join(output_dir, "label_stats.png")
output_txt_path = os.path.join(output_dir, "label_stats.txt")
# === 统计每个类别在标注中的出现次数 ===
label_counter = Counter()
for json_path in glob.glob(os.path.join(input_dir, "*.json")):
with open(json_path, "r", encoding="utf-8") as f:
data = json.load(f)
for shape in data.get("shapes", []):
label = shape.get("label")
if label:
label_counter[label] += 1
# === 按数量从高到低排序 ===
label_counter = dict(sorted(label_counter.items(), key=lambda x: x[1], reverse=True))
# === 保存统计文本文件 ===
with open(output_txt_path, "w", encoding="utf-8") as f:
for label, count in label_counter.items():
f.write(f"{label}: {count}\n")
# === 绘制条形图 ===
plt.figure(figsize=(12, 6))
plt.bar(label_counter.keys(), label_counter.values(), color='skyblue')
plt.xlabel("Class Name")
plt.ylabel("Count")
plt.title("Label Count per Class in LabelMe Annotations")
plt.xticks(rotation=90)
plt.tight_layout()
plt.grid(axis='y', linestyle='--', alpha=0.7)
# === 保存图像 ===
plt.savefig(output_plot_path, dpi=300)
print(f"✅ 类别统计图已保存至: {output_plot_path}")
print(f"✅ 类别统计表已保存至: {output_txt_path}")