Datasets:
File size: 4,691 Bytes
c83afe9 | 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | 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": "aligned with the left edge, vertically centered",
# "top": "aligned with the top edge, horizontally centered",
# "right": "aligned with the right edge, vertically centered",
# "bottom": "aligned with the bottom edge, horizontally centered",
# "center": "centered in the image",
# }
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")
|