Upload 6 files
Browse files- HandWritingDigitRecognition.py +79 -0
- model training.ipynb +183 -0
- model.py +51 -0
- model/model.h5 +3 -0
- model/model_weights.h5 +3 -0
- requirements.txt +4 -0
HandWritingDigitRecognition.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import ImageGrab
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tkinter as tk
|
| 5 |
+
|
| 6 |
+
# Load the models that you have already trained
|
| 7 |
+
model = tf.keras.models.load_model("./model/model.h5")
|
| 8 |
+
model.load_weights("./model/model_weights.h5")
|
| 9 |
+
|
| 10 |
+
# Create the tkinter window
|
| 11 |
+
root = tk.Tk()
|
| 12 |
+
root.title("Handwritten Digit Recognition")
|
| 13 |
+
|
| 14 |
+
# Create the main canvas with black background color
|
| 15 |
+
canvas = tk.Canvas(root, width=280, height=250, bg='black')
|
| 16 |
+
canvas.pack()
|
| 17 |
+
|
| 18 |
+
screen = tk.Label(root, text="Draw a number", font=("Helvetica", 24))
|
| 19 |
+
screen.pack()
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Function to handle drawing on the canvas
|
| 23 |
+
def start_draw(event):
|
| 24 |
+
global last_x, last_y
|
| 25 |
+
last_x, last_y = event.x, event.y
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def draw(event):
|
| 29 |
+
global last_x, last_y
|
| 30 |
+
x, y = event.x, event.y
|
| 31 |
+
canvas.create_line((last_x, last_y, x, y), fill="white", width=10)
|
| 32 |
+
last_x, last_y = x, y
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# Function to predict the drawn digit
|
| 36 |
+
def predict_digit():
|
| 37 |
+
x = root.winfo_rootx() + canvas.winfo_x()
|
| 38 |
+
y = root.winfo_rooty() + canvas.winfo_y()
|
| 39 |
+
x1 = x + canvas.winfo_width()
|
| 40 |
+
y1 = y + canvas.winfo_height()
|
| 41 |
+
|
| 42 |
+
# There is a buggy area here
|
| 43 |
+
# When I try to capture the canvas image sometimes it captures wrong places.
|
| 44 |
+
# So I tried to fix it manually but, it still doesn't work perfect:(
|
| 45 |
+
img = ImageGrab.grab((x+31, y+38, x1, y1))
|
| 46 |
+
|
| 47 |
+
# See the captured image
|
| 48 |
+
# img.show()
|
| 49 |
+
|
| 50 |
+
img = img.convert('L')
|
| 51 |
+
img = img.resize((28, 28))
|
| 52 |
+
img_array = np.array(img)
|
| 53 |
+
img_array = img_array.reshape(1, 28, 28, 1) / 255.0 # Normalize input
|
| 54 |
+
prediction = model.predict(img_array)
|
| 55 |
+
|
| 56 |
+
# Hold the best prediction
|
| 57 |
+
predicted_digit = np.argmax(prediction)
|
| 58 |
+
screen.config(text="Predicted digit: " + str(predicted_digit))
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# Function to clear the canvas
|
| 62 |
+
def clear_canvas():
|
| 63 |
+
canvas.delete("all")
|
| 64 |
+
screen.config(text="Draw a number")
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# Bind mouse events
|
| 68 |
+
canvas.bind("<Button-1>", start_draw)
|
| 69 |
+
canvas.bind("<B1-Motion>", draw)
|
| 70 |
+
|
| 71 |
+
# Predict button
|
| 72 |
+
predict_button = tk.Button(root, text="Predict", command=predict_digit)
|
| 73 |
+
predict_button.pack()
|
| 74 |
+
|
| 75 |
+
reset_button = tk.Button(root, text="Clear", command=clear_canvas)
|
| 76 |
+
reset_button.pack()
|
| 77 |
+
|
| 78 |
+
# Start the main loop
|
| 79 |
+
root.mainloop()
|
model training.ipynb
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nbformat": 4,
|
| 3 |
+
"nbformat_minor": 0,
|
| 4 |
+
"metadata": {
|
| 5 |
+
"colab": {
|
| 6 |
+
"provenance": []
|
| 7 |
+
},
|
| 8 |
+
"kernelspec": {
|
| 9 |
+
"name": "python3",
|
| 10 |
+
"display_name": "Python 3"
|
| 11 |
+
},
|
| 12 |
+
"language_info": {
|
| 13 |
+
"name": "python"
|
| 14 |
+
}
|
| 15 |
+
},
|
| 16 |
+
"cells": [
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"execution_count": 2,
|
| 20 |
+
"metadata": {
|
| 21 |
+
"colab": {
|
| 22 |
+
"base_uri": "https://localhost:8080/"
|
| 23 |
+
},
|
| 24 |
+
"id": "1vuQoR42AF_N",
|
| 25 |
+
"outputId": "5c473da6-0bf9-42ac-8e7a-993d4522623f"
|
| 26 |
+
},
|
| 27 |
+
"outputs": [
|
| 28 |
+
{
|
| 29 |
+
"output_type": "stream",
|
| 30 |
+
"name": "stdout",
|
| 31 |
+
"text": [
|
| 32 |
+
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n",
|
| 33 |
+
"11490434/11490434 [==============================] - 0s 0us/step\n",
|
| 34 |
+
"Epoch 1/10\n",
|
| 35 |
+
"468/468 [==============================] - 100s 210ms/step - loss: 0.3633 - accuracy: 0.8851 - val_loss: 0.0526 - val_accuracy: 0.9829\n",
|
| 36 |
+
"Epoch 2/10\n",
|
| 37 |
+
"468/468 [==============================] - 96s 204ms/step - loss: 0.1228 - accuracy: 0.9621 - val_loss: 0.0363 - val_accuracy: 0.9879\n",
|
| 38 |
+
"Epoch 3/10\n",
|
| 39 |
+
"468/468 [==============================] - 98s 209ms/step - loss: 0.0898 - accuracy: 0.9730 - val_loss: 0.0298 - val_accuracy: 0.9896\n",
|
| 40 |
+
"Epoch 4/10\n",
|
| 41 |
+
"468/468 [==============================] - 99s 210ms/step - loss: 0.0719 - accuracy: 0.9786 - val_loss: 0.0198 - val_accuracy: 0.9935\n",
|
| 42 |
+
"Epoch 5/10\n",
|
| 43 |
+
"468/468 [==============================] - 100s 213ms/step - loss: 0.0680 - accuracy: 0.9791 - val_loss: 0.0193 - val_accuracy: 0.9938\n",
|
| 44 |
+
"Epoch 6/10\n",
|
| 45 |
+
"468/468 [==============================] - 97s 207ms/step - loss: 0.0590 - accuracy: 0.9816 - val_loss: 0.0175 - val_accuracy: 0.9942\n",
|
| 46 |
+
"Epoch 7/10\n",
|
| 47 |
+
"468/468 [==============================] - 101s 216ms/step - loss: 0.0547 - accuracy: 0.9830 - val_loss: 0.0181 - val_accuracy: 0.9941\n",
|
| 48 |
+
"Epoch 8/10\n",
|
| 49 |
+
"468/468 [==============================] - 101s 215ms/step - loss: 0.0511 - accuracy: 0.9845 - val_loss: 0.0155 - val_accuracy: 0.9946\n",
|
| 50 |
+
"Epoch 9/10\n",
|
| 51 |
+
"468/468 [==============================] - 100s 214ms/step - loss: 0.0476 - accuracy: 0.9858 - val_loss: 0.0188 - val_accuracy: 0.9937\n",
|
| 52 |
+
"Epoch 10/10\n",
|
| 53 |
+
"468/468 [==============================] - 100s 213ms/step - loss: 0.0444 - accuracy: 0.9858 - val_loss: 0.0182 - val_accuracy: 0.9948\n"
|
| 54 |
+
]
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"output_type": "execute_result",
|
| 58 |
+
"data": {
|
| 59 |
+
"text/plain": [
|
| 60 |
+
"<keras.src.callbacks.History at 0x7ef01c2c8520>"
|
| 61 |
+
]
|
| 62 |
+
},
|
| 63 |
+
"metadata": {},
|
| 64 |
+
"execution_count": 2
|
| 65 |
+
}
|
| 66 |
+
],
|
| 67 |
+
"source": [
|
| 68 |
+
"import tensorflow as tf\n",
|
| 69 |
+
"import numpy as np\n",
|
| 70 |
+
"\n",
|
| 71 |
+
"# Load the pre prepared data from TensofFlow\n",
|
| 72 |
+
"(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()\n",
|
| 73 |
+
"\n",
|
| 74 |
+
"# Preprocess the data\n",
|
| 75 |
+
"x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)\n",
|
| 76 |
+
"x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)\n",
|
| 77 |
+
"x_train = x_train.astype('float32')\n",
|
| 78 |
+
"x_test = x_test.astype('float32')\n",
|
| 79 |
+
"x_train /= 255\n",
|
| 80 |
+
"x_test /= 255\n",
|
| 81 |
+
"\n",
|
| 82 |
+
"# Define a CNN model with data augmentation\n",
|
| 83 |
+
"model = tf.keras.models.Sequential([\n",
|
| 84 |
+
" tf.keras.layers.Conv2D(64, (3, 3), activation=\"relu\", input_shape=(28, 28, 1)),\n",
|
| 85 |
+
" tf.keras.layers.MaxPooling2D(2, 2),\n",
|
| 86 |
+
" tf.keras.layers.Conv2D(64, (3, 3), activation=\"relu\"),\n",
|
| 87 |
+
" tf.keras.layers.MaxPooling2D(2, 2),\n",
|
| 88 |
+
" tf.keras.layers.Flatten(),\n",
|
| 89 |
+
" tf.keras.layers.Dense(512, activation=\"relu\"),\n",
|
| 90 |
+
" # Dropout to avoid from the overfitting\n",
|
| 91 |
+
" tf.keras.layers.Dropout(0.5),\n",
|
| 92 |
+
"\n",
|
| 93 |
+
" # There are 10 different (0-9) output.\n",
|
| 94 |
+
" # So use 10 units dense layer with softmax activation\n",
|
| 95 |
+
" tf.keras.layers.Dense(10, activation=\"softmax\"),\n",
|
| 96 |
+
"])\n",
|
| 97 |
+
"\n",
|
| 98 |
+
"# Compile the model\n",
|
| 99 |
+
"model.compile(optimizer='adam',\n",
|
| 100 |
+
" loss='sparse_categorical_crossentropy',\n",
|
| 101 |
+
" metrics=['accuracy'])\n",
|
| 102 |
+
"\n",
|
| 103 |
+
"# Data Augmentation\n",
|
| 104 |
+
"datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n",
|
| 105 |
+
" rotation_range=10,\n",
|
| 106 |
+
" width_shift_range=0.1,\n",
|
| 107 |
+
" height_shift_range=0.1,\n",
|
| 108 |
+
" shear_range=0.1,\n",
|
| 109 |
+
" zoom_range=0.1\n",
|
| 110 |
+
")\n",
|
| 111 |
+
"\n",
|
| 112 |
+
"datagen.fit(x_train)\n",
|
| 113 |
+
"\n",
|
| 114 |
+
"# Train the model with data augmentation\n",
|
| 115 |
+
"model.fit(datagen.flow(x_train, y_train, batch_size=128),\n",
|
| 116 |
+
" steps_per_epoch=len(x_train) / 128,\n",
|
| 117 |
+
" epochs=10,\n",
|
| 118 |
+
" validation_data=(x_test, y_test))\n"
|
| 119 |
+
]
|
| 120 |
+
},
|
| 121 |
+
{
|
| 122 |
+
"cell_type": "code",
|
| 123 |
+
"source": [
|
| 124 |
+
"loss, acc = model.evaluate(x=x_test, y=y_test)\n",
|
| 125 |
+
"print(\"Loss:\", loss)\n",
|
| 126 |
+
"print(\"Accuracy:\", acc)"
|
| 127 |
+
],
|
| 128 |
+
"metadata": {
|
| 129 |
+
"colab": {
|
| 130 |
+
"base_uri": "https://localhost:8080/"
|
| 131 |
+
},
|
| 132 |
+
"id": "3O5b1ShcIE7-",
|
| 133 |
+
"outputId": "6ef12f6b-eaa2-4c85-97da-db2ee7d81b6e"
|
| 134 |
+
},
|
| 135 |
+
"execution_count": 3,
|
| 136 |
+
"outputs": [
|
| 137 |
+
{
|
| 138 |
+
"output_type": "stream",
|
| 139 |
+
"name": "stdout",
|
| 140 |
+
"text": [
|
| 141 |
+
"313/313 [==============================] - 4s 13ms/step - loss: 0.0182 - accuracy: 0.9948\n",
|
| 142 |
+
"Loss: 0.018189573660492897\n",
|
| 143 |
+
"Accuracy: 0.9947999715805054\n"
|
| 144 |
+
]
|
| 145 |
+
}
|
| 146 |
+
]
|
| 147 |
+
},
|
| 148 |
+
{
|
| 149 |
+
"cell_type": "markdown",
|
| 150 |
+
"source": [
|
| 151 |
+
"# Save the model and model weights to a avaliable path\n"
|
| 152 |
+
],
|
| 153 |
+
"metadata": {
|
| 154 |
+
"id": "2pNqtozRN5wj"
|
| 155 |
+
}
|
| 156 |
+
},
|
| 157 |
+
{
|
| 158 |
+
"cell_type": "code",
|
| 159 |
+
"source": [
|
| 160 |
+
"model.save(\"/content/model/model.h5\")\n",
|
| 161 |
+
"model.save_weights(\"/content/model/model_weights.h5\")"
|
| 162 |
+
],
|
| 163 |
+
"metadata": {
|
| 164 |
+
"colab": {
|
| 165 |
+
"base_uri": "https://localhost:8080/"
|
| 166 |
+
},
|
| 167 |
+
"id": "7Fp3_wYEKMF-",
|
| 168 |
+
"outputId": "14213539-b8bf-4774-8916-5a0ea28b23a2"
|
| 169 |
+
},
|
| 170 |
+
"execution_count": 4,
|
| 171 |
+
"outputs": [
|
| 172 |
+
{
|
| 173 |
+
"output_type": "stream",
|
| 174 |
+
"name": "stderr",
|
| 175 |
+
"text": [
|
| 176 |
+
"/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.\n",
|
| 177 |
+
" saving_api.save_model(\n"
|
| 178 |
+
]
|
| 179 |
+
}
|
| 180 |
+
]
|
| 181 |
+
}
|
| 182 |
+
]
|
| 183 |
+
}
|
model.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
# Load the pre prepared data from TensofFlow
|
| 5 |
+
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
|
| 6 |
+
|
| 7 |
+
# Preprocess the data
|
| 8 |
+
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
|
| 9 |
+
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
|
| 10 |
+
x_train = x_train.astype('float32')
|
| 11 |
+
x_test = x_test.astype('float32')
|
| 12 |
+
x_train /= 255
|
| 13 |
+
x_test /= 255
|
| 14 |
+
|
| 15 |
+
# Define a CNN model with data augmentation
|
| 16 |
+
model = tf.keras.models.Sequential([
|
| 17 |
+
tf.keras.layers.Conv2D(64, (3, 3), activation="relu", input_shape=(28, 28, 1)),
|
| 18 |
+
tf.keras.layers.MaxPooling2D(2, 2),
|
| 19 |
+
tf.keras.layers.Conv2D(64, (3, 3), activation="relu"),
|
| 20 |
+
tf.keras.layers.MaxPooling2D(2, 2),
|
| 21 |
+
tf.keras.layers.Flatten(),
|
| 22 |
+
tf.keras.layers.Dense(512, activation="relu"),
|
| 23 |
+
# Dropout to avoid from the overfitting
|
| 24 |
+
tf.keras.layers.Dropout(0.5),
|
| 25 |
+
|
| 26 |
+
# There are 10 different (0-9) output.
|
| 27 |
+
# So use 10 units dense layer with softmax activation
|
| 28 |
+
tf.keras.layers.Dense(10, activation="softmax"),
|
| 29 |
+
])
|
| 30 |
+
|
| 31 |
+
# Compile the model
|
| 32 |
+
model.compile(optimizer='adam',
|
| 33 |
+
loss='sparse_categorical_crossentropy',
|
| 34 |
+
metrics=['accuracy'])
|
| 35 |
+
|
| 36 |
+
# Data Augmentation
|
| 37 |
+
datagen = tf.keras.preprocessing.image.ImageDataGenerator(
|
| 38 |
+
rotation_range=10,
|
| 39 |
+
width_shift_range=0.1,
|
| 40 |
+
height_shift_range=0.1,
|
| 41 |
+
shear_range=0.1,
|
| 42 |
+
zoom_range=0.1
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
datagen.fit(x_train)
|
| 46 |
+
|
| 47 |
+
# Train the model with data augmentation
|
| 48 |
+
model.fit(datagen.flow(x_train, y_train, batch_size=128),
|
| 49 |
+
steps_per_epoch=len(x_train) / 128,
|
| 50 |
+
epochs=10,
|
| 51 |
+
validation_data=(x_test, y_test))
|
model/model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6ae84744a8bc0e7eabf0dd277f89c23dc561c937deb12b13ac8cd0c13a33fed8
|
| 3 |
+
size 10393504
|
model/model_weights.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:240bd369c26bf4659c7d53de65e8e1f2177b369843187521a795999527ff1587
|
| 3 |
+
size 3472256
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
PIL
|
| 2 |
+
tensorflow
|
| 3 |
+
numpytkinter
|
| 4 |
+
numpy
|