File size: 5,960 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 | import matplotlib.pyplot as plt
import numpy as np
import random
import os
import json
import argparse
def create_images(num_images, num_objects_list , file):
# Ensure the output directory exists
output_dir = os.path.join(os.getcwd(), "data")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
data = []
append = (file != 1)
for num_objects in num_objects_list:
dpi = 100 # Dots per inch
width_inches = 1 # 100 pixels
height_inches = num_objects + 1 # 100 pixels per row
for img_index in range(num_images):
shapes1, shapes2 = [], []
fig, ax = plt.subplots(figsize=(width_inches, height_inches), dpi=dpi)
for i in range (num_objects):
y = num_objects - i - 1
x = 0.5
radius = 0.1
shapes = ['circle', 'triangle', 'rectangle', 'pentagon']
#colours = ['red', 'green', 'blue']
# Add circles to the plot
shape_type = random.choice(shapes)
shapes1.append(shape_type)
if shape_type == 'circle':
shape = plt.Circle((x, y), radius, color="blue")
elif shape_type == 'triangle':
triangle = np.array([[x, y + radius], [x - radius, y - radius], [x + radius, y - radius]])
shape = plt.Polygon(triangle, color="blue")
elif shape_type == 'rectangle':
shape = plt.Rectangle((x - radius, y - radius), 2 * radius, 2 * radius, color="blue")
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="blue")
ax.add_patch(shape)
# if i < num_objects -1:
ax.plot([0, 1], [y - 0.5, y - 0.5], color='black')
ax.set_xlim(0, 1)
ax.set_ylim(-0.6, num_objects - 0.5)
ax.set_aspect('equal')
ax.axis('off')
image_filename = f"first{file}.png"
image_path = os.path.join(output_dir, image_filename)
plt.savefig(image_path, bbox_inches='tight', dpi=dpi)
plt.close(fig)
fig, ax = plt.subplots(figsize=(width_inches, height_inches), dpi=dpi)
for i in range (num_objects):
y = num_objects - i - 1
x = 0.5
radius = 0.1
shapes = ['circle', 'triangle', 'rectangle', 'pentagon']
#colours = ['red', 'green', 'blue']
# Add circles to the plot
shape_type = random.choice(shapes)
shapes2.append(shape_type)
if shape_type == 'circle':
shape = plt.Circle((x, y), radius, color="blue")
elif shape_type == 'triangle':
triangle = np.array([[x, y - radius], [x - radius, y + radius], [x + radius, y + radius]])
shape = plt.Polygon(triangle, color="blue")
elif shape_type == 'rectangle':
shape = plt.Rectangle((x - radius, y - radius), 2 * radius, 2 * radius, color="blue")
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="blue")
ax.add_patch(shape)
# if i < num_objects -1:
ax.plot([0, 1], [y - 0.5, y - 0.5], color='black')
ax.set_xlim(0, 1)
ax.set_ylim(-0.6, num_objects - 0.5)
ax.set_aspect('equal')
ax.axis('off')
image_filename = f"second{file}.png"
file += 1
image_path = os.path.join(output_dir, image_filename)
plt.savefig(image_path, bbox_inches='tight', dpi=dpi)
plt.close(fig)
answer = 0
for i in range(num_objects):
if(shapes1[i] == shapes2[i]):
answer += 1
# Store the number of objects in the image_data dictionary
gold_output = {
"id": image_filename,
"Gold_output": answer,
"Rows" : num_objects
}
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)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Create a grid of circles and triangles.')
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 number of rows',
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)
create_images(args.num_images, num_object_list , file)
|