text_plain_output_5.png text_plain_output_9.png text_plain_output_4.png image_output_5.png text_plain_output_6.png image_output_7.png text_plain_output_3.png image_output_4.png text_plain_output_7.png image_output_8.png text_plain_output_8.png image_output_6.png text_plain_output_2.png text_plain_output_1.png image_output_3.png image_output_2.png image_output_1.png image_output_9.png
from IPython.display import Image, display

from random import randint, random

from sklearn.model_selection import train_test_split

import cv2

import numpy as np

import os

import os

import random

import os
import cv2
import numpy as np
from random import randint, random
from sklearn.model_selection import train_test_split
def random_color():
    return randint(128, 255)
def random_polygon(image, vertices):
    center = (randint(10, 118), randint(10, 118))
    radius = randint(5, 60)
    color = random_color()
    angle = 2 * np.pi / vertices
    pts = []
    for i in range(vertices):
        x = int(center[0] + radius * np.cos(i * angle))
        y = int(center[1] + radius * np.sin(i * angle))
        pts.append((x, y))
    pts = np.array(pts, np.int32)
    cv2.fillPoly(image, [pts], color)
def random_oval(image):
    thickness = -1
    center = (randint(10, 118), randint(10, 118))
    axes = (randint(5, 60), randint(5, 60))
    angle = randint(0, 360)
    color = random_color()
    cv2.ellipse(image, center, axes, angle, 0, 360, color, thickness)
def random_rectangle(image):
    thickness = -1
    pt1 = (randint(5, 123), randint(5, 123))
    pt2 = (randint(5, 123), randint(5, 123))
    color = random_color()
    cv2.rectangle(image, pt1, pt2, color, thickness)
def random_triangle(image):
    thickness = -1
    pts = np.array([[randint(5, 123), randint(5, 123)], [randint(5, 123), randint(5, 123)], [randint(5, 123), randint(5, 123)]], np.int32)
    color = random_color()
    cv2.fillPoly(image, [pts], color)
def random_star(image, vertices):
    center = (randint(10, 118), randint(10, 118))
    inner_radius = randint(5, 30)
    outer_radius = randint(inner_radius + 1, 60)
    color = random_color()
    angle = 2 * np.pi / vertices
    inner_pts = []
    outer_pts = []
    for i in range(vertices):
        inner_x = int(center[0] + inner_radius * np.cos(i * angle))
        inner_y = int(center[1] + inner_radius * np.sin(i * angle))
        outer_x = int(center[0] + outer_radius * np.cos(i * angle + angle / 2))
        outer_y = int(center[1] + outer_radius * np.sin(i * angle + angle / 2))
        inner_pts.append((inner_x, inner_y))
        outer_pts.append((outer_x, outer_y))
    pts = [pt for pair in zip(inner_pts, outer_pts) for pt in pair]
    pts = np.array(pts, np.int32)
    cv2.fillPoly(image, [pts], color)
def add_salt_pepper_noise(image, prob):
    row, col = image.shape
    sp_image = np.copy(image)
    num_salt = np.ceil(prob * image.size)
    coords = [np.random.randint(0, i - 1, int(num_salt)) for i in image.shape]
    sp_image[tuple(coords)] = 255
    num_pepper = np.ceil(prob * image.size)
    coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in image.shape]
    sp_image[tuple(coords)] = 0
    return sp_image
def generate_images(num_images, shape_func, shape_name):
    for i in range(num_images):
        image = np.zeros((128, 128), dtype=np.uint8)
        shape_func(image)
        image_with_noise = add_salt_pepper_noise(image, 0.01)
        cv2.imwrite(f'images/{shape_name}/{shape_name}_{i:04d}.png', image_with_noise)
def create_directory_structure():
    os.makedirs('images', exist_ok=True)
    for shape in ['oval', 'rectangle', 'triangle', 'poly5', 'poly6', 'poly7', 'star5', 'star8']:
        os.makedirs(f'images/{shape}', exist_ok=True)
def main():
    create_directory_structure()
    np.random.seed(42)
    num_images = 1000
    generate_images(num_images, random_oval, 'oval')
    generate_images(num_images, random_rectangle, 'rectangle')
    generate_images(num_images, random_triangle, 'triangle')
    for sides in [5, 6, 7]:
        generate_images(num_images, lambda img: random_polygon(img, sides), f'poly{sides}')
    generate_images(num_images, lambda img: random_star(img, 5), 'star5')
    generate_images(num_images, lambda img: random_star(img, 8), 'star8')
    X = []
    y = []
    for i, shape in enumerate(['oval', 'rectangle', 'triangle', 'poly5', 'poly6', 'poly7', 'star5', 'star8']):
        for j in range(num_images):
            img = cv2.imread(f'images/{shape}/{shape}_{j:04d}.png')
            X.append(img)
            y.append(i)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    np.save('X_train.npy', X_train)
    np.save('X_test.npy', X_test)
    np.save('y_train.npy', y_train)
    np.save('y_test.npy', y_test)
main()

import os
from IPython.display import Image, display
import random
shapes = ['oval', 'poly5', 'poly6', 'poly7', 'rectangle', 'star5', 'star8', 'triangle']
for shape in shapes:
    print(f'\nSample images for {shape}:')
    files = os.listdir(f'./images/{shape}')
    for i in range(3):
        file = random.choice(files)
        display(Image(f'./images/{shape}/{file}'))