ghibli-dataset / scripts /save_img.py
pulnip's picture
Upload folder using huggingface_hub
bc5332d verified
raw
history blame
1.32 kB
from datasets import load_dataset, Dataset, Features, Value, Image
import re
import os
import json
import shutil
features = Features({
"image": Image(decode=False),
"caption": Value("string")
})
ds: Dataset = load_dataset("Nechintosh/ghibli", split="train", features=features)
samples = list(ds)
def natural_key(filename: str):
parts = re.split(r'(\d+)', filename)
return [int(p) if p.isdigit() else p.lower() for p in parts]
samples.sort(key=lambda s: natural_key(os.path.basename(s["image"]["path"])))
os.makedirs("data/real", exist_ok=True)
with open("default.jsonl", "w") as f:
for i, sample in enumerate(samples):
caption = sample["caption"]
src_path: str = sample["image"]["path"]
filename = os.path.basename(src_path)
dst_path = os.path.join("data/real", filename)
shutil.copy(src_path, dst_path)
f.write(json.dumps({
"id": f"real-{i:05d}",
"image": dst_path,
"label": "real",
"description": caption,
}) + "\n")
# f.write(json.dumps({
# "id": f"{i:05d}-ai",
# "image": f"data/ai_gen/fake_{i:05d}.jpg",
# "label": "ai",
# "description": caption,
# "pair_id": f"{i:05d}",
# "seed": 42
# }) + "\n")