File size: 1,049 Bytes
bc5332d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddaa08a
bc5332d
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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("metadata.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")