Violin / scripts /generate_metadata_Task_Image_Mask.py
Obedience-Violin's picture
rename files and data
89c0dfe
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"
)