| import csv |
| import os |
| import math |
|
|
| IMG_SIZE = 512 |
|
|
| SHAPES = { |
| "circle": ("circle", "diameter"), |
| "square": ("axis-aligned square", "side length"), |
| "equilateral triangle": ("upright equilateral triangle", "side length"), |
| } |
|
|
| SIZE_RATIOS = [0.2, 0.4, 0.6, 0.8, 1.0] |
|
|
| |
| |
| |
| |
| |
| |
| |
| POSITIONS_DEFAULT = { |
| "left": "with its bounding box aligned with the left edge, vertically centered", |
| "top": "with its bounding box aligned with the top edge, horizontally centered", |
| "right": "with its bounding box aligned with the right edge, vertically centered", |
| "bottom": "with its bounding box aligned with the bottom edge, horizontally centered", |
| "center": "with its bounding box centered in the image", |
| } |
|
|
| POSITIONS_TRIANGLE = { |
| "left": "with its bounding box aligned with the left edge, vertically centered", |
| "top": "with its bounding box aligned with the top edge, horizontally centered", |
| "right": "with its bounding box aligned with the right edge, vertically centered", |
| "bottom": "with its bounding box aligned with the bottom edge, horizontally centered", |
| "center": "with its bounding box centered in the image", |
| } |
|
|
|
|
| def get_bounding_half(shape, size_ratio): |
| """Return (half_w, half_h) of the shape's bounding box.""" |
| side = size_ratio * IMG_SIZE |
| if shape == "circle": |
| r = side / 2 |
| return r, r |
| elif shape == "square": |
| return side / 2, side / 2 |
| elif shape == "equilateral triangle": |
| h = side * math.sqrt(3) / 2 |
| return side / 2, h / 2 |
|
|
|
|
| def get_center(position, half_w, half_h): |
| """Return (cx, cy) for the given position.""" |
| mid = IMG_SIZE / 2 |
| if position == "left": |
| return half_w, mid |
| elif position == "top": |
| return mid, half_h |
| elif position == "right": |
| return IMG_SIZE - half_w, mid |
| elif position == "bottom": |
| return mid, IMG_SIZE - half_h |
| elif position == "center": |
| return mid, mid |
|
|
|
|
| def is_out_of_bounds(cx, cy, half_w, half_h): |
| return (cx - half_w < 0 or cx + half_w > IMG_SIZE or |
| cy - half_h < 0 or cy + half_h > IMG_SIZE) |
|
|
|
|
| def generate_metadata(prompt_file, output_file): |
| with open(prompt_file, "r", encoding="utf-8") as f: |
| prompts = list(csv.DictReader(f)) |
|
|
| rows = [] |
| id_counter = 1 |
| skipped = 0 |
|
|
| for prompt in prompts: |
| prompt_text = prompt["prompt"] |
| for shape_key, (shape_desc, size_desc) in SHAPES.items(): |
| for size_ratio in SIZE_RATIOS: |
| half_w, half_h = get_bounding_half(shape_key, size_ratio) |
| for pos_key in POSITIONS_DEFAULT: |
| pos_desc = (POSITIONS_TRIANGLE if shape_key == "equilateral triangle" else POSITIONS_DEFAULT)[pos_key] |
| cx, cy = get_center(pos_key, half_w, half_h) |
| if is_out_of_bounds(cx, cy, half_w, half_h): |
| skipped += 1 |
| continue |
|
|
| filled = (prompt_text |
| .replace("<shape>", shape_desc) |
| .replace("<size_desc>", size_desc) |
| .replace("<size>", f"{int(size_ratio * 100)}%") |
| .replace("<position>", pos_desc)) |
|
|
| rows.append({ |
| "id": id_counter, |
| "prompt": filled, |
| "shape": shape_key, |
| "position": pos_key, |
| "size_ratio": size_ratio, |
| "center_x": round(cx), |
| "center_y": round(cy), |
| "task": 3, |
| }) |
| 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", "shape", "position", "size_ratio", "center_x", "center_y", "task"]) |
| writer.writeheader() |
| writer.writerows(rows) |
|
|
| print(f"Generation complete: {output_file} ({len(rows)} rows, {skipped} skipped as out-of-bounds)") |
|
|
|
|
| generate_metadata("raw_config/Task_Geometric.csv", "metadata/Task_Geometric_metadata.csv") |
|
|