How to Create a Neural Network for Recognizing Hand-Drawn Shapes

How to Create a Neural Network for Recognizing Hand-Drawn Shapes

by Naomi A.

In this guide, we will walk through the process of developing a neural network that identifies hand-drawn shapes. The tutorial is broken down into three main sections:

  1. Preprocessing the hand-drawn shapes.
  2. Training a neural network to recognize these shapes.
  3. Creating a simple user interface to predict the drawn shapes.

All code is available on github under the MIT License. (LINK TO REPO) 


 

1. MNIST Maker: Preprocessing Hand-Drawn Shapes

Objective: Convert the images of hand-drawn shapes into a standardized format for machine learning.

Steps:

  1. Load the images from a specified directory.
  2. Convert them to grayscale and resize them to 28x28 pixels.
  3. Normalize the pixel values.
  4. Label the images based on their filenames.
  5. Shuffle and split the data for training, validation, and testing. 
SCRIPT for creating the MNIST Dataset:
import cv2
import numpy as np
import os
import random

# Path to the shapes directory
base_folder = r"C:\yourpathgoeshere"

shapes = ["square", "circle", "triangle", "rhombus"]
labels_dict = {"square": 0, "circle": 1, "triangle": 2, "rhombus": 3}

data = []

#See full code on Github

 

2. Neural Network Training:

Objective: Develop and train a neural network model using TensorFlow to identify the hand-drawn shapes.

Steps:

  1. Load the preprocessed dataset.
  2. Define the neural network architecture.
  3. Compile the model.
  4. Train the model on the training data.
  5. Evaluate the model's performance on the test data.
  6. Save the trained model.
import tensorflow as tf
from tensorflow.keras import layers, models
import numpy as np
import os

print("1. Importing necessary libraries...")

# Load your custom shapes dataset
print("\n2. Loading the custom shapes dataset...")
base_folder = r"C:\yourpathgoeshere"
train_images = np.load(os.path.join(base_folder, 'train_images.npy'))
train_labels = np.load(os.path.join(base_folder, 'train_labels.npy'))
test_images = np.load(os.path.join(base_folder, 'test_images.npy'))
test_labels = np.load(os.path.join(base_folder, 'test_labels.npy'))

#See full code on Github

3. Application UI:

Objective: Develop an interactive application where users can draw shapes and have the neural network predict what they've drawn.

Steps:

  1. Load the trained model.
  2. Set up an interactive drawing canvas using tkinter.
  3. Enable users to draw shapes in the canvas.
  4. Process the canvas drawings for prediction.
  5. Predict the shape using the trained model.
  6. Display the prediction and ask for user feedback.
 
import tkinter as tk
from tkinter import ttk, simpledialog, messagebox
from PIL import Image
import numpy as np
import cv2
import tensorflow as tf
from PIL import ImageDraw
from ttkthemes import ThemedTk

# Load the trained model
print("Loading the trained model...")
model = tf.keras.models.load_model(r'C:\yourpathgoeshere\DrawingRecog_model.h5')

# Initialize variables for drawing
drawing = False
last_x = 0
last_y = 0

#See full code on Github
 

Conclusion

By following the steps in this guide, you've successfully developed a system that can recognize hand-drawn shapes. This application can be further improved by training on more diverse drawings, increasing the model's complexity, or adding more classes/shapes!

 Stay tuned techies!

Comments

Popular posts from this blog

Creating a Webpage - A Beginner's Guide (HTML, CSS, JavaScript)

Teaching Computers to Play Tic Tac Toe: A Reinforcement Learning Approach

Run Queries in SQL Server Using Python!