Loading textDatasetGenerator.py 0 → 100644 +94 −0 Original line number Diff line number Diff line import matplotlib.pyplot as plt import numpy as np from PIL import Image, ImageDraw, ImageFont import random # Number of points influence the resolution of your chosen text in the dataset num_points = 10000 np.random.seed(42) # Type chosen text text = "NN" # Add the custom text size font_size = 600 # Change path to your chosen font font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" ###################################################################### # Define the coordinates of the rectangle's corners x_min, y_min = -1, -1 x_max, y_max = 1, 1 # Create a figure and axis fig, ax = plt.subplots() # Remove the rectangle outline and background ax.axis('off') # Set explicit x-axis and y-axis limits ax.set_xlim(x_min, x_max) ax.set_ylim(y_min, y_max) # Generate random points within the rectangle random_x = np.random.uniform(x_min, x_max, num_points) random_y = np.random.uniform(y_min, y_max, num_points) # Create a blank image to render the "DNN" text image_width = 1000 image_height = 1000 image = Image.new("L", (image_width, image_height), color=255) # White background draw = ImageDraw.Draw(image) font = ImageFont.truetype(font_path, font_size) text_width, text_height = draw.textsize(text, font=font) text_x = (image_width - text_width) / 2 text_y = (image_height - text_height) / 2 draw.text((text_x, text_y), text, fill=0, font=font) # Fill with black (0) # Convert the image to a numpy array text_image = np.array(image) # Initialize labels list labels = [] # Check if each random point is inside the text boundaries for x, y in zip(random_x, random_y): # Convert x and y to image coordinates image_x = int((x - x_min) / (x_max - x_min) * image_width) image_y = int((y - y_min) / (y_max - y_min) * image_height) # Check if the pixel in the image is black (inside text) if text_image[image_y, image_x] == 0: labels.append(1) else: labels.append(-1) # Filter the random_y array based on labels inside_text_y = random_y[np.array(labels) == 1] inside_text_y = inside_text_y[:1000] # Filter the random_x array based on labels inside_text_x = random_x[np.array(labels) == 1] inside_text_x = inside_text_x[:1000] # Filter the random_y array based on labels outside_text_y = random_y[np.array(labels) == -1] # Filter the random_x array based on labels outside_text_x = random_x[np.array(labels) == -1] # Plot the points inside the text plt.scatter(inside_text_x, inside_text_y, c='g', marker='.', label='Class A') plt.scatter(outside_text_x[:len(inside_text_x)], outside_text_y[:len(inside_text_y)], c='r', marker='.', label='Class B') np.save("logo_dataset_classA", np.column_stack((inside_text_x, inside_text_y))) np.save("logo_dataset_classB", np.column_stack((outside_text_x[:len(inside_text_x)], outside_text_y[:len(inside_text_y)]))) # Show the legend #plt.legend() # Show the plot plt.show() Loading
textDatasetGenerator.py 0 → 100644 +94 −0 Original line number Diff line number Diff line import matplotlib.pyplot as plt import numpy as np from PIL import Image, ImageDraw, ImageFont import random # Number of points influence the resolution of your chosen text in the dataset num_points = 10000 np.random.seed(42) # Type chosen text text = "NN" # Add the custom text size font_size = 600 # Change path to your chosen font font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" ###################################################################### # Define the coordinates of the rectangle's corners x_min, y_min = -1, -1 x_max, y_max = 1, 1 # Create a figure and axis fig, ax = plt.subplots() # Remove the rectangle outline and background ax.axis('off') # Set explicit x-axis and y-axis limits ax.set_xlim(x_min, x_max) ax.set_ylim(y_min, y_max) # Generate random points within the rectangle random_x = np.random.uniform(x_min, x_max, num_points) random_y = np.random.uniform(y_min, y_max, num_points) # Create a blank image to render the "DNN" text image_width = 1000 image_height = 1000 image = Image.new("L", (image_width, image_height), color=255) # White background draw = ImageDraw.Draw(image) font = ImageFont.truetype(font_path, font_size) text_width, text_height = draw.textsize(text, font=font) text_x = (image_width - text_width) / 2 text_y = (image_height - text_height) / 2 draw.text((text_x, text_y), text, fill=0, font=font) # Fill with black (0) # Convert the image to a numpy array text_image = np.array(image) # Initialize labels list labels = [] # Check if each random point is inside the text boundaries for x, y in zip(random_x, random_y): # Convert x and y to image coordinates image_x = int((x - x_min) / (x_max - x_min) * image_width) image_y = int((y - y_min) / (y_max - y_min) * image_height) # Check if the pixel in the image is black (inside text) if text_image[image_y, image_x] == 0: labels.append(1) else: labels.append(-1) # Filter the random_y array based on labels inside_text_y = random_y[np.array(labels) == 1] inside_text_y = inside_text_y[:1000] # Filter the random_x array based on labels inside_text_x = random_x[np.array(labels) == 1] inside_text_x = inside_text_x[:1000] # Filter the random_y array based on labels outside_text_y = random_y[np.array(labels) == -1] # Filter the random_x array based on labels outside_text_x = random_x[np.array(labels) == -1] # Plot the points inside the text plt.scatter(inside_text_x, inside_text_y, c='g', marker='.', label='Class A') plt.scatter(outside_text_x[:len(inside_text_x)], outside_text_y[:len(inside_text_y)], c='r', marker='.', label='Class B') np.save("logo_dataset_classA", np.column_stack((inside_text_x, inside_text_y))) np.save("logo_dataset_classB", np.column_stack((outside_text_x[:len(inside_text_x)], outside_text_y[:len(inside_text_y)]))) # Show the legend #plt.legend() # Show the plot plt.show()