whiskeyClassification / makeDataset.py
spacenship's picture
Upload folder using huggingface_hub
0f81e55 verified
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.")