File size: 8,803 Bytes
02f1218 | 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | import matplotlib.pyplot as plt
import cv2
import PIL
from PIL import Image
import random
import numpy as np
import os
import json
import argparse
def generate_shapes(num_images, num_objects_list , file):
# Set up the plot
output_dir = os.path.join(os.getcwd(), "data")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
data = []
append = (file != 1)
for n in num_objects_list:
max_obj = 2*n
for img_index in range(num_images):
"""fig, ax = plt.subplots()
ax.set_aspect('equal')
plt.axis('off')
# Adjust plot limits to prevent cutoff
plt.xlim(-0.1, 1.1)
plt.ylim(-0.1, 1.1)
# Generate random positions for the shapes
positions = []
radius = 0.05
shapes = ['circle', 'triangle', 'rectangle', 'pentagon']
colours = ['red', 'green', 'blue', 'yellow', 'orange', 'black']
for i in range(n):
while True:
x, y = random.uniform(0.1, 0.9), random.uniform(0.1, 0.9)
# Ensure shapes don't overlap
if all(((x - px) ** 2 + (y - py) ** 2) ** 0.5 > 2 * radius for px, py in positions):
positions.append((x, y))
break
# Choose one odd-colored shape
#odd_index = random.randint(0, n - 1)
# Draw shapes and label them
for i, (x, y) in enumerate(positions):
color = random.choice(colours)
shape_type = random.choice(shapes)
if shape_type == 'circle':
shape = plt.Circle((x, y), radius, color=color)
elif shape_type == 'triangle':
triangle = np.array([[x, y + radius], [x - radius, y - radius], [x + radius, y - radius]])
shape = plt.Polygon(triangle, color=color)
elif shape_type == 'rectangle':
shape = plt.Rectangle((x - radius, y - radius), 2 * radius, 2 * radius, color=color)
elif shape_type == 'pentagon':
pentagon = np.array([
[x, y + radius], [x + 0.95 * radius, y + 0.31 * radius],
[x + 0.59 * radius, y - 0.81 * radius], [x - 0.59 * radius, y - 0.81 * radius],
[x - 0.95 * radius, y + 0.31 * radius]
])
shape = plt.Polygon(pentagon, color=color)
ax.add_patch(shape)"""
#ax.text(x, y, str(i + 1), color='white', ha='center', va='center', fontsize=8, weight='bold')
# Save the plot to a file
fig, ax = plt.subplots()
ax.set_aspect('equal')
plt.axis('off')
# Adjust plot limits to prevent cutoff
plt.xlim(-203.5, 203.5)
plt.ylim(-203.5, 203.5)
# Generate random positions for the shapes
positions = []
radius = 3.5
shapes = ['circle', 'triangle', 'rectangle', 'pentagon']
colours = ['red', 'green', 'blue', 'yellow', 'orange', 'black']
for i in range(max_obj):
while True:
x, y = random.uniform(-200, -3.5), random.uniform(-200, 200)
# Ensure shapes don't overlap
if all(((x - px) ** 2 + (y - py) ** 2) ** 0.5 > 2 * radius for px, py in positions):
positions.append((x, y))
break
# Choose one odd-colored shape
#odd_index = random.randint(0, n - 1)
# Draw shapes and label them
color, shape_type = [], []
plt.axvline(x = 0, color = 'blue')
for i, (x, y) in enumerate(positions):
color.append(random.choice(colours))
shape_type.append(random.choice(shapes))
if shape_type[-1] == 'circle':
shape = plt.Circle((x, y), radius, color=color[-1])
elif shape_type[-1] == 'triangle':
triangle = np.array([[x, y + radius], [x - radius, y - radius], [x + radius, y - radius]])
shape = plt.Polygon(triangle, color=color[-1])
elif shape_type[-1] == 'rectangle':
shape = plt.Rectangle((x - radius, y - radius), 2 * radius, 2 * radius, color=color[-1])
elif shape_type[-1] == 'pentagon':
pentagon = np.array([
[x, y + radius], [x + 0.95 * radius, y + 0.31 * radius],
[x + 0.59 * radius, y - 0.81 * radius], [x - 0.59 * radius, y - 0.81 * radius],
[x - 0.95 * radius, y + 0.31 * radius]
])
shape = plt.Polygon(pentagon, color=color[-1])
ax.add_patch(shape)
filename = f"first{file}.png"
filepath = os.path.join(output_dir, filename)
plt.savefig(filepath, bbox_inches='tight')
plt.close()
fig, ax = plt.subplots()
ax.set_aspect('equal')
plt.axis('off')
# Adjust plot limits to prevent cutoff
plt.xlim(-203.5, 203.5)
plt.ylim(-203.5, 203.5)
for i in range(n):
x = list(positions[i])
x[0] = -x[0]
positions[i] = tuple(x)
plt.axvline(x = 0, color = 'blue')
for i, (x, y) in enumerate(positions):
if shape_type[i] == 'circle':
shape = plt.Circle((x, y), radius, color=color[i])
elif shape_type[i] == 'triangle':
triangle = np.array([[x, y + radius], [x - radius, y - radius], [x + radius, y - radius]])
shape = plt.Polygon(triangle, color=color[i])
elif shape_type[i] == 'rectangle':
shape = plt.Rectangle((x - radius, y - radius), 2 * radius, 2 * radius, color=color[i])
elif shape_type[i] == 'pentagon':
pentagon = np.array([
[x, y + radius], [x + 0.95 * radius, y + 0.31 * radius],
[x + 0.59 * radius, y - 0.81 * radius], [x - 0.59 * radius, y - 0.81 * radius],
[x - 0.95 * radius, y + 0.31 * radius]
])
shape = plt.Polygon(pentagon, color=color[i])
ax.add_patch(shape)
filename = f"second{file}.png"
file += 1
filepath = os.path.join(output_dir, filename)
plt.savefig(filepath, bbox_inches='tight')
plt.close()
gold_answer = n
"""if choice == 0:
img = cv2.imread(filepath)
img_mirror = np.flip(img, axis=1)
cv2.imwrite(os.path.join(output_dir, f"second{file-1}.png"), img_mirror)
gold_answer = "Yes"
else:
angles = [90, 180, 270, 360]
angle = random.choice(angles)
img = Image.open(filepath)
rotated_image = img.rotate(angle, PIL.Image.BILINEAR)
rotated_image.save(os.path.join(output_dir, f"second{file-1}.png"), "PNG")
gold_answer = "No"
"""
gold_output = {
"id": filename,
"mirror_image": gold_answer,
"num_objects": n
}
data.append(gold_output)
if append:
with open(os.path.join(os.getcwd() , "data.json"), "r") as f:
old_data = json.load(f)
old_data.extend(data)
with open(os.path.join(os.getcwd() , "data.json"), "w") as f:
json.dump(old_data, f, indent=2)
else:
with open(os.path.join(os.getcwd() , "data.json"), "w") as f:
json.dump(data, f, indent=2)
# # Example usage
# generate_shapes(10, 'shapes.png')
if __name__ == "__main__":
# Parse command line arguments
parser = argparse.ArgumentParser(description='Create an image of different objects')
parser.add_argument(
'--num_images',
type=int,
help='Number of images to generate',
default=1
)
parser.add_argument(
'--num_sizes',
nargs='*',
type=int,
help='List of count of objects',
default=[5]
)
parser.add_argument(
'--file',
type=int,
help='Starting file number',
default=1
)
args = parser.parse_args()
file = args.file
num_object_list = args.num_sizes
append = (file != 1)
generate_shapes(args.num_images, num_object_list , file)
|