Spaces:
Sleeping
Sleeping
| import os | |
| import re | |
| import glob | |
| from dotenv import load_dotenv | |
| from datasets import load_dataset | |
| from PIL import Image | |
| # ===================================================================== | |
| # [μ€μ λΆλΆ] | |
| # ===================================================================== | |
| # ν ν° | |
| load_dotenv() | |
| HF_TOKEN = os.environ.get("HF_TOKEN") | |
| print(f"μ΄κ±° ν ν° : {HF_TOKEN}") | |
| # μμ§ν λ°μ΄ν°μ | |
| DATASET_NAME = "KrushiJethe/fashion_data" | |
| # λ°μ΄ν°μ λ΄μ μ΄λ―Έμ§ λ°μ΄ν°κ° μλ νλλͺ | |
| IMAGE_FIELD_NAME = "image" | |
| # λ°μ΄ν°μ λ΄μ λΌλ²¨ λ°μ΄ν°κ° μλ νλλͺ | |
| LABEL_FIELD_NAME = "articleType" | |
| # μ¬λ¬ λΌλ²¨μ νλμ λν ν΄λμ€λ‘ λ¬Άλ λ§€ν λμ λ리 | |
| CLASS_MAPPING = { | |
| "t-shirt": ["Tshirts", "Tops"], | |
| "sneakers":["Casual Shoes"], | |
| #"umbrella":["Umbrellas"], | |
| "glasses":["Sunglasses"], | |
| "pants":["Jeans"], | |
| } | |
| # ν΄λμ€λ³λ‘ μμ§ν μ΄λ―Έμ§μ μ΅λ κ°μ | |
| NUM_IMAGES_PER_CLASS = 100 | |
| # μ μ₯ν μ΄λ―Έμ§μ ν΄μλ (width, height) | |
| TARGET_RESOLUTION = 256 | |
| # μ΄λ―Έμ§λ₯Ό μ μ₯ν μ΅μμ λλ ν 리λͺ | |
| BASE_SAVE_DIR = "./dataset_output" | |
| # μμ§ν λ°μ΄ν°μ μ split μ΄λ¦ (μ: "train", "validation", "test") | |
| SPLIT_NAME = "train" | |
| # 컨ν μ΄λλ₯Ό μ€νν μνμμλ 컨ν μ΄λμ μΊμ μ μ₯λ¨ | |
| # μΊμ νμΈ -> ls -lah ~/.cache/huggingface | |
| # μΊμ μμ -> rm -rf ~/.cache/huggingface | |
| USE_STREAMING = False | |
| # ===================================================================== | |
| # ν΄λμ€ λͺ λͺ κ·μΉ μ μ© | |
| def format_class_name(class_name: str) -> str: | |
| """ | |
| ν΄λμ€λͺ μ μλ¬Έμλ‘ νκ³ λμ΄μ°κΈ°κ° μμ κ²½μ° "-"λ‘ λ체 | |
| """ | |
| return str(class_name).lower().replace("_", "-").replace(" ", "-") | |
| # λ§μ§λ§ μ΄λ―Έμ§μ λ²νΈ + 1 | |
| def get_next_image_index(save_dir: str, formatted_class_name: str) -> int: | |
| """ | |
| μ΄λ―Έμ§λ₯Ό μ¬λ¬ μ°¨λ‘ μ΄μ΄μ μμ§ν μ μλλ‘ λ§μ§λ§ μ΄λ―Έμ§ λ²νΈλ₯Ό νμ | |
| λλ ν 리λ₯Ό μ€μΊνμ¬ κ°μ₯ λμ λ²νΈλ₯Ό μ°Ύμ λ€ +1μ λ°ν | |
| """ | |
| if not os.path.exists(save_dir): | |
| return 1 | |
| # jpgμ jpeg νμ₯μ λͺ¨λ κ²μ | |
| search_pattern_jpg = os.path.join(save_dir, f"hf_{formatted_class_name}_*.jpg") | |
| search_pattern_jpeg = os.path.join(save_dir, f"hf_{formatted_class_name}_*.jpeg") | |
| existing_files = glob.glob(search_pattern_jpg) + glob.glob(search_pattern_jpeg) | |
| max_idx = 0 | |
| # νμΌλͺ μμ μ κ·ννμμ ν΅ν΄ λ²νΈ μΆμΆ (μ: hf_fried-chicken_001.jpg -> 1) | |
| regex = re.compile(rf"hf_{formatted_class_name}_(\d+)\.jpe?g$") | |
| for file_path in existing_files: | |
| basename = os.path.basename(file_path) | |
| match = regex.match(basename) | |
| if match: | |
| idx = int(match.group(1)) | |
| if idx > max_idx: | |
| max_idx = idx | |
| return max_idx + 1 | |
| def collect_hf_images(): | |
| """ | |
| λ©μΈ λ°μ΄ν° μμ§ ν¨μ. | |
| Hugging Face λ°μ΄ν°μ μμ μ€μ μ λ°μνμ¬ μ΄λ―Έμ§λ₯Ό μμ§νκ³ μ μ₯ | |
| """ | |
| label_to_rep_class = {} | |
| for rep_class, labels in CLASS_MAPPING.items(): | |
| for label in labels: | |
| label_to_rep_class[label] = rep_class | |
| print(label_to_rep_class) | |
| # λ°μ΄ν°μ λ³ λ±κ°λ‘ μμ§ | |
| # streaming=True μμ±μ μ¬μ©νλ©΄ μ 체 λ°μ΄ν°μ μ λ©λͺ¨λ¦¬λ λμ€ν¬μ ν λ²μ λ€μ΄λ‘λνμ§ μκ³ | |
| # generator ννλ‘ νλμ©(λ±κ°λ‘) κ°μ Έμ€λ―λ‘ λ©λͺ¨λ¦¬μ λ€νΈμν¬ ν¨μ¨μ±μ΄ κ·Ήλν | |
| print(f"[{DATASET_NAME}] λ°μ΄ν°μ μ€νΈλ¦¬λ° λ‘λ μμ...") | |
| dataset = load_dataset(DATASET_NAME, split=SPLIT_NAME, streaming=USE_STREAMING, token=HF_TOKEN) | |
| # λλ€μΌλ‘ κ°μ Έμ€κΈ° | |
| # random_seed = random.randint(0, 10000) | |
| # dataset = load_dataset(DATASET_NAME, split=SPLIT_NAME, streaming=USE_STREAMING).shuffle(seed=random_seed, buffer_size=1000) | |
| # ν΄λμ€λ³λ‘ ν¬λ§·ν λ ν΄λλͺ κ³Ό, νμ¬κΉμ§ μμ§λ κ°μ, κ·Έλ¦¬κ³ μ μ₯λ μμ λ²νΈλ₯Ό κ΄λ¦¬ν λμ λ리 | |
| class_info = {} | |
| for label in CLASS_MAPPING.keys(): | |
| formatted_name = format_class_name(label) | |
| save_path = os.path.join(BASE_SAVE_DIR, formatted_name) | |
| # [κ·μΉ 1, 4] ν΄λμ€λ₯Ό ν΄λλ‘ κ΄λ¦¬νλ©° ν΄λλͺ μ λ³νλ ν΄λμ€λͺ μ λ°λ₯Έλ€. | |
| os.makedirs(save_path, exist_ok=True) | |
| # μ΄μ΄μ μμ§νκΈ° μν μμ μΈλ±μ€ νμ | |
| start_idx = get_next_image_index(save_path, formatted_name) | |
| class_info[label] = { | |
| "formatted_name": formatted_name, | |
| "save_path": save_path, | |
| "collected_count": 0, | |
| "current_idx": start_idx | |
| } | |
| print("λ°μ΄ν° μμ§μ μμν©λλ€...") | |
| # μ€νΈλ¦¬λ° λ°μ΄ν° μν | |
| for item in dataset: | |
| print("1. λ°μ΄ν°μ λ‘λ μμ...") | |
| # λͺ¨λ ν΄λμ€κ° λͺ©ν μμ§λμ μ±μ λμ§ νμΈ | |
| if all(info["collected_count"] >= NUM_IMAGES_PER_CLASS for info in class_info.values()): | |
| print("λͺ¨λ ν΄λμ€μ μ΄λ―Έμ§ μμ§μ΄ μλ£λμμ΅λλ€.") | |
| break | |
| print("2. λ°μ΄ν°μ λΌλ²¨ μμ΄ν κΊΌλ΄κΈ°...") | |
| current_label = item.get(LABEL_FIELD_NAME) | |
| print(current_label) | |
| # νμ¬ λ½ν λΌλ²¨μ΄ μ μν λ§€ν λμ λ리μ μ‘΄μ¬νλμ§ νμΈ | |
| if current_label in label_to_rep_class: | |
| rep_class = label_to_rep_class[current_label] | |
| target_info = class_info[rep_class] | |
| print("4. μ΄λ―Έμ§ μ ν¨μ± κ²μ¬...") | |
| # μ΄λ―Έ λͺ©ν κ°μλ₯Ό μ±μ΄ ν΄λμ€λΌλ©΄ μ€ν΅ | |
| if target_info["collected_count"] >= NUM_IMAGES_PER_CLASS: | |
| continue | |
| # μ΄λ―Έμ§ μ ν¨μ± μ²΄ν¬ | |
| image = item.get(IMAGE_FIELD_NAME) | |
| if image is None: | |
| continue | |
| print("5. μ΄λ―Έμ§ λ³ν...") | |
| try: | |
| # μ΄λ―Έμ§λ₯Ό jpg/jpegλ‘λ§ μ·¨κΈνκΈ° μν΄ RGB λͺ¨λλ‘ λ³ν (μν μ±λ λ± μ κ±°) | |
| if image.mode != "RGB": | |
| image = image.convert("RGB") | |
| #μ΄λ―Έμ§ ν΄μλκ° μ΅μ 256pxλ§ μμ§ | |
| if image.width < TARGET_RESOLUTION or image.height < TARGET_RESOLUTION: | |
| continue | |
| print("6. ν΄λμ€ λͺ λͺ κ·μΉμ λ°λΌ...") | |
| # [κ·μΉ 3, 4] μ΄λ―Έμ§ λͺ λͺ κ·μΉ (hf_[ν΄λμ€λͺ ]_[3μ리μ«μ].jpg) | |
| # {:03d}λ₯Ό ν΅ν΄ 3μ리 μ«μλ‘ λ§μΆκ³ λΉμ리λ 0μΌλ‘ μ±μ | |
| file_name = f"hf_{target_info['formatted_name']}_{target_info['current_idx']:03d}.jpg" | |
| file_path = os.path.join(target_info["save_path"], file_name) | |
| print("7. μ΄λ―Έμ§ μ μ₯...") | |
| image.save(file_path, "JPEG", quality=95) | |
| # μΉ΄μ΄νΈ λ° μΈλ±μ€ μ¦κ° | |
| target_info["collected_count"] += 1 | |
| target_info["current_idx"] += 1 | |
| print(f"Saved: {file_path} ({target_info['collected_count']}/{NUM_IMAGES_PER_CLASS})") | |
| except Exception as e: | |
| # μ€λ₯ λ°μ μ μ€ν¬λ¦½νΈκ° λ©μΆμ§ μλλ‘ μμΈ μ²λ¦¬ | |
| print(f"μ΄λ―Έμ§ μ μ₯ μ€ μ€λ₯ λ°μ (Label: {current_label}): {e}") | |
| if __name__ == "__main__": | |
| collect_hf_images() |