for d in [train_img_dir, val_img_dir, train_lbl_dir, val_lbl_dir]: os.makedirs(d, exist_ok=True) # 建立目標目錄(已存在則略過)
files = sorted(f for f in os.listdir(src_dir) if f.endswith(".jpg")) split = int(len(files) * 0.8) # 前 80% 為訓練集,後 20% 為驗證集
for i, f in enumerate(files): img_dst = train_img_dir if i < split else val_img_dir lbl_dst = train_lbl_dir if i < split else val_lbl_dir shutil.copy(os.path.join(src_dir, f), os.path.join(img_dst, f)) # 複製圖片 label_f = f.replace(".jpg", ".txt") label_src = os.path.join(src_dir, label_f) if os.path.exists(label_src): shutil.copy(label_src, os.path.join(lbl_dst, label_f)) # 同步複製標籤
img = cv2.imread(image_path) if img isNone: print(f"❌ 無法讀取圖片:{image_path}") exit() ifnot os.path.exists(label_path): print(f"❌ 找不到標籤檔:{label_path}") exit() h, w = img.shape[:2]
with open(label_path, "r") as f: for line in f: parts = line.strip().split() class_id = int(parts[0]) cx, cy, bw, bh = float(parts[1]), float(parts[2]), float(parts[3]), float(parts[4])
for fname in os.listdir(label_dir): ifnot fname.endswith(".txt"): # 跳過非標籤檔 continue with open(os.path.join(label_dir, fname), "r") as f: for line in f: class_id = int(line.strip().split()[0]) # 每行第一個數字為 class_id class_count[class_id] = class_count.get(class_id, 0) + 1
for cls, count in sorted(class_count.items()): print(f"Class {cls}: {count} 個標註")
圖:批次掃描標籤目錄統計各類別的標註數量並輸出結果
💡 class ID 對應的類別名稱記錄在 LabelImg 產生的 classes.txt(與圖片存放於同一目錄),可對照查詢。