| import csv |
| import json |
| import os |
|
|
| METADATA_DIR = "metadata" |
| OUTPUT_JSONL = "data/test.jsonl" |
|
|
|
|
| def read_csv(path): |
| with open(path, "r", encoding="utf-8") as f: |
| return list(csv.DictReader(f)) |
|
|
|
|
| def build_metadata(): |
| entries = [] |
|
|
| |
| path1 = os.path.join(METADATA_DIR, "Task_Color_Var1_metadata.csv") |
| if os.path.exists(path1): |
| for row in read_csv(path1): |
| id_val = row["id"].strip() |
| entries.append({ |
| "id": f"id_{id_val}", |
| "prompt": row.get("prompt", "").strip(), |
| |
| "color_1": row.get("color_1", ""), |
| "hex_val_1": row.get("hex_val_1", ""), |
| "task": 1, |
| "ground_truth": f"Task_Color_Var1/id_{id_val}.png", |
| }) |
| else: |
| print(f"Warning: {path1} not found, skipping Task_Color_Var1") |
|
|
| |
| path2 = os.path.join(METADATA_DIR, "Task_Color_Var2_metadata.csv") |
| if os.path.exists(path2): |
| for row in read_csv(path2): |
| id_val = row["id"].strip() |
| entries.append({ |
| "id": f"id_{id_val}", |
| "prompt": row.get("prompt", "").strip(), |
| |
| "color_1": row.get("color_1", ""), |
| "color_2": row.get("color_2", ""), |
| "hex_val_1": row.get("hex_val_1", ""), |
| "hex_val_2": row.get("hex_val_2", ""), |
| "direction": row.get("direction", ""), |
| "task": 2, |
| "ground_truth": f"Task_Color_Var2/id_{id_val}.png", |
| }) |
| else: |
| print(f"Warning: {path2} not found, skipping Task_Color_Var2") |
|
|
| |
| path3 = os.path.join(METADATA_DIR, "Task_Geometric_metadata.csv") |
| if os.path.exists(path3): |
| for row in read_csv(path3): |
| id_val = row["id"].strip() |
| entries.append({ |
| "id": f"id_{id_val}", |
| "prompt": row.get("prompt", "").strip(), |
| "shape": row.get("shape", ""), |
| "position": row.get("position", ""), |
| "size_ratio": row.get("size_ratio", ""), |
| "center_x": row.get("center_x", ""), |
| "center_y": row.get("center_y", ""), |
| "task": 3, |
| "ground_truth": f"Task_Geometric/id_{id_val}.png", |
| }) |
| else: |
| print(f"Warning: {path3} not found, skipping Task_Geometric") |
|
|
| |
| for mask_type in ["inpainting", "outpainting", "random"]: |
| path4 = os.path.join(METADATA_DIR, f"Task_Image_Mask_{mask_type}_metadata.csv") |
| if not os.path.exists(path4): |
| print(f"Warning: {path4} not found, skipping Task_Image_Mask {mask_type}") |
| continue |
| for row in read_csv(path4): |
| id_val = row["id"].strip() |
| img1 = row.get("image1_path", "").strip().replace('\\', '/') |
| img2 = row.get("image2_path", "").strip().replace('\\', '/') |
| entries.append({ |
| "id": f"id_{id_val}", |
| "prompt": row.get("prompt", "").strip(), |
| "mask_type": mask_type, |
| "image_id": row.get("image_id", ""), |
| "image1_path": img1, |
| "image2_path": img2, |
| "task": 4, |
| "ground_truth": f"Task_Image_Mask/{mask_type}/id_{id_val}.png", |
| }) |
|
|
| os.makedirs(os.path.dirname(OUTPUT_JSONL), exist_ok=True) |
| with open(OUTPUT_JSONL, "w", encoding="utf-8") as f: |
| for entry in entries: |
| f.write(json.dumps(entry, ensure_ascii=False) + "\n") |
|
|
| print(f"Total {len(entries)} entries → {OUTPUT_JSONL}") |
|
|
|
|
| if __name__ == "__main__": |
| build_metadata() |
|
|