File size: 4,378 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 | import matplotlib.pyplot as plt
import random
import os
import json
import argparse
def generate_circles(num_circles, min_size_diff=0.05):
# Create a figure
global file
fig, ax = plt.subplots(figsize=(num_circles * 3, 5))
# Data to store the circles' information
circles_info = []
# X-axis positions to ensure the circles are aligned horizontally
x_position = 0
# Store previously generated sizes to ensure minimum difference between sizes
radii = [0]
for i in range(1, num_circles + 1):
# Generate a random radius ensuring no two are too close in size
while True:
radius = random.uniform(0.3, 2) # Random size for the circle
if all(abs(radius - r) >= min_size_diff for r in radii):
radii.append(radius)
break
x_position += radius + radii[-2] + 0.5 # X-position for the circle
y = 2 # Y-position for alignment (all circles in a line)
# Plot the circle
circle = plt.Circle((x_position, y), radius, color='black', fill=True)
ax.add_patch(circle)
# Label the circle
ax.text(x_position, y, f'{i}', ha='center', va='center', fontsize=24, color='white')
# Store the circle's label and size
circles_info.append({'label': i , 'size': radius})
# Set limits and hide axes
ax.set_xlim(0, x_position + 2)
ax.set_ylim(0, 4)
ax.set_aspect('equal')
ax.axis('off')
image_filename = f"{file}.png"
image_path = os.path.join(output_dir, image_filename)
plt.savefig(image_path, bbox_inches='tight')
plt.close(fig)
return circles_info
def sort_circles(circles_info):
# Sort the circles based on their size
sorted_circles = sorted(circles_info, key=lambda x: x['size'])
# Print the gold output
# print("Gold output (sorted by size):")
# for circle in sorted_circles:
# print(f"{circle['label']}: Size {circle['size']:.2f}")
return sorted_circles
# def main():
# num_circles = 5 # Specify the number of circles to generate
# circles_info = generate_circles(num_circles) # Generate the circles and store their info
# # Sort the circles by size and get the gold output
# gold_output = sort_circles(circles_info)
# return gold_output
# Optionally, you can save this output to a file or use it for further processing
if __name__ == "__main__":
output_dir = os.path.join(os.getcwd(), "data")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# file = 1
# num_images = int(sys.argv[1])
# num_object_list = sys.argv[2:]
# num_object_list = [int(num_objects) for num_objects in num_object_list]
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='*',
help='List of n values',
type=int,
default=[5 , 3]
)
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
num_images = args.num_images
append = (file != 1)
data = []
# main()/
for num_circles in num_object_list:
for _ in range(num_images):
circles_info = generate_circles(num_circles)
output = sort_circles(circles_info)
# Store the number of objects in the image_data dictionary
gold_output = [circle['label'] for circle in output]
gold_output = {
"id": f'{file}.png',
"Gold_output": gold_output,
"Rows" : num_circles
}
file += 1
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)
|