File size: 2,738 Bytes
2535e42
 
 
 
89c0dfe
2535e42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89c0dfe
2535e42
 
 
 
 
 
 
 
 
 
 
89c0dfe
2535e42
 
 
 
 
 
 
 
 
 
 
89c0dfe
2535e42
89c0dfe
2535e42
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import csv
import os

IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".webp", ".bmp"}
RAW_IMAGE_DIR = "data/Task_Image_Mask_raw_image"
MASK_TYPES = ["inpainting", "outpainting", "random"]


def scan_image_pairs(mask_type):
    """Return sorted list of (image_path, mask_path, image_id) for a given mask type."""
    images_dir = os.path.join(RAW_IMAGE_DIR, "images")
    mask_dir = os.path.join(RAW_IMAGE_DIR, f"{mask_type}_mask")
    if not os.path.exists(images_dir):
        return []
    pairs = []
    for fname in sorted(os.listdir(images_dir)):
        if os.path.splitext(fname)[1].lower() not in IMAGE_EXTENSIONS:
            continue
        image_id = os.path.splitext(fname)[0]
        image_path = os.path.join(images_dir, fname)
        mask_path = os.path.join(mask_dir, f"{image_id}.png")
        if not os.path.exists(mask_path):
            print(f"Warning: mask not found for {fname} ({mask_type}), skipping.")
            continue
        
        DATA_ROOT = "data"
        rel_image_path = os.path.relpath(image_path, DATA_ROOT)
        rel_mask_path = os.path.relpath(mask_path, DATA_ROOT)
        
        pairs.append((rel_image_path, rel_mask_path, image_id))
        # pairs.append((image_path, mask_path, image_id))
    return pairs


def generate_metadata(prompt_file, mask_type, output_file):
    with open(prompt_file, "r", encoding="utf-8") as f:
        prompts = list(csv.DictReader(f))

    pairs = scan_image_pairs(mask_type)
    if not pairs:
        print(f"Warning: no image-mask pairs found for {mask_type}, skipping.")
        return

    rows = []
    id_counter = 1
    for image1_path, image2_path, image_id in pairs:
        for prompt in prompts:
            rows.append({
                "id": id_counter,
                "prompt": prompt["prompt"],
                "mask_type": mask_type,
                "task": 4,
                "image_id": image_id,
                "image1_path": image1_path,
                "image2_path": image2_path,
            })
            id_counter += 1

    output_dir = os.path.dirname(output_file)
    if output_dir and not os.path.exists(output_dir):
        os.makedirs(output_dir, exist_ok=True)

    with open(output_file, "w", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=["id", "prompt", "mask_type", "task", "image_id", "image1_path", "image2_path"])
        writer.writeheader()
        writer.writerows(rows)

    print(f"Generation complete: {output_file} ({len(rows)} rows)")




if __name__ == "__main__":
    for mask_type in MASK_TYPES:
        generate_metadata(
            "raw_config/Task_Image_Mask.csv",
            mask_type,
            f"metadata/Task_Image_Mask_{mask_type}_metadata.csv"
        )