import os import shutil # 설정 SRC_ROOT = 'E:/whisky data/labels' # 각 위스키명 폴더가 모여 있는 상위 폴더 DST_IMG = 'C:/Users/a0108/OneDrive/문서/DGIST/2025 여름 인턴/whisky data/dataset/images/train' # 복사할 이미지 폴더 DST_LABEL = 'C:/Users/a0108/OneDrive/문서/DGIST/2025 여름 인턴/whisky data/dataset/labels/train' # 복사할 라벨(.txt) 폴더 FRAME_STEP = 5 # 몇 프레임마다 하나씩 뽑을지 MAX_COUNT = 350 # 클래스당 최대 샘플 수 os.makedirs(DST_IMG, exist_ok=True) os.makedirs(DST_LABEL, exist_ok=True) whiskey_list = ['ardbeg 5', 'ardbeg 10', 'balvenie 12', 'balvenie 14', 'benriach 12', 'benromach 10', 'dalmore 12', 'dalmore 15', 'glenallachie 10cs', 'glenallachie 15', 'glendronach 12', 'glendronach 15', 'glenfiddich 12', 'glenfiddich 15', 'glengrant 15', 'glenlivet 12', 'glenlivet 15', 'kione tiger', 'macallan 12', 'monkey shoulder'] for whiskey_name in sorted(os.listdir(SRC_ROOT)): src_dir = os.path.join(SRC_ROOT, whiskey_name) if not os.path.isdir(src_dir): continue # 이미지(.jpg/.png)와 레이블(.txt) 파일만 추려서 정렬 imgs = sorted(f for f in os.listdir(src_dir) if f.lower().endswith(('.jpg','.png'))) labels = sorted(f for f in os.listdir(src_dir) if f.lower().endswith('.txt')) # 안전을 위해 베이스 이름이 매칭되는 페어만 사용 bases = [os.path.splitext(f)[0] for f in imgs] bases = [b for b in bases if b+'.txt' in labels] count = 0 for idx, base in enumerate(bases): if idx % FRAME_STEP != 0: continue if count >= MAX_COUNT: break class_num = whiskey_list.index(whiskey_name) + 1 # 새 파일명: whiskeyname_0001.jpg, whiskeyname_0001.txt ... new_id = f"{count+1:04d}" new_base = f"{class_num}_{new_id}" src_img = os.path.join(src_dir, base + os.path.splitext(imgs[0])[1]) src_label = os.path.join(src_dir, base + '.txt') dst_img = os.path.join(DST_IMG, new_base + os.path.splitext(src_img)[1]) dst_lbl = os.path.join(DST_LABEL, new_base + '.txt') shutil.copy2(src_img, dst_img) shutil.copy2(src_label, dst_lbl) count += 1 print(f"{whiskey_name}: {count} samples copied.")