Hand Written
Hand Written
September 2, 2024
print(x_train.shape, y_train.shape)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
1
[3]: batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,␣
↪3),activation='relu',input_shape=input_shape))
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.
↪optimizers.Adadelta(),metrics=['accuracy'])
↪y_test))
model.save('mnist.h5')
print("Saving the model as mnist.h5")
Epoch 1/10
469/469 [==============================] - 179s 377ms/step - loss: 2.2703 -
accuracy: 0.1565 - val_loss: 2.2130 - val_accuracy: 0.3143
Epoch 2/10
469/469 [==============================] - 168s 359ms/step - loss: 2.1812 -
accuracy: 0.2974 - val_loss: 2.1024 - val_accuracy: 0.5759
Epoch 3/10
469/469 [==============================] - 164s 351ms/step - loss: 2.0640 -
accuracy: 0.4385 - val_loss: 1.9522 - val_accuracy: 0.6809
Epoch 4/10
469/469 [==============================] - 160s 341ms/step - loss: 1.9062 -
accuracy: 0.5353 - val_loss: 1.7496 - val_accuracy: 0.7299
Epoch 5/10
469/469 [==============================] - 177s 378ms/step - loss: 1.7020 -
accuracy: 0.6000 - val_loss: 1.5032 - val_accuracy: 0.7661
Epoch 6/10
469/469 [==============================] - 171s 363ms/step - loss: 1.4795 -
accuracy: 0.6421 - val_loss: 1.2504 - val_accuracy: 0.7886
Epoch 7/10
469/469 [==============================] - 182s 388ms/step - loss: 1.2751 -
accuracy: 0.6701 - val_loss: 1.0358 - val_accuracy: 0.8079
2
Epoch 8/10
469/469 [==============================] - 163s 348ms/step - loss: 1.1103 -
accuracy: 0.6978 - val_loss: 0.8739 - val_accuracy: 0.8236
Epoch 9/10
469/469 [==============================] - 164s 349ms/step - loss: 0.9877 -
accuracy: 0.7176 - val_loss: 0.7580 - val_accuracy: 0.8350
Epoch 10/10
469/469 [==============================] - 164s 351ms/step - loss: 0.8941 -
accuracy: 0.7386 - val_loss: 0.6750 - val_accuracy: 0.8457
The model has successfully trained
C:\Users\nasvi\anaconda3\Lib\site-packages\keras\src\engine\training.py:3079:
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')`.
saving_api.save_model(
Saving the model as mnist.h5
model = load_model('mnist.h5')
def predict_digit(img):
#resize image to 28x28 pixels
img = img.resize((28,28))
#convert rgb to grayscale
img = img.convert('L')
img = np.array(img)
#reshaping to support our model input and normalizing
img = img.reshape(1,28,28,1)
img = img/255.0
#predicting the class
res = model.predict([img])[0]
return np.argmax(res), max(res)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
# Creating elements
3
self.canvas = tk.Canvas(self, width=300, height=300, bg = "white",␣
↪cursor="cross")
self.label = tk.Label(self, text="Thinking..", font=("Helvetica", 48))
self.classify_btn = tk.Button(self, text = "Recognise", command = ␣
↪ self.classify_handwriting)
self.button_clear = tk.Button(self, text = "Clear", command = self.
↪clear_all)
# Grid structure
self.canvas.grid(row=0, column=0, pady=2, sticky=W, )
self.label.grid(row=0, column=1,pady=2, padx=2)
self.classify_btn.grid(row=1, column=1, pady=2, padx=2)
self.button_clear.grid(row=1, column=0, pady=2)
#self.canvas.bind("<Motion>", self.start_pos)
self.canvas.bind("<B1-Motion>", self.draw_lines)
def clear_all(self):
self.canvas.delete("all")
def classify_handwriting(self):
HWND = self.canvas.winfo_id() # get the handle of the canvas
rect = win32gui.GetWindowRect(HWND) # get the coordinate of the canvas
im = ImageGrab.grab(rect)
app = App()
mainloop()
4
1/1 [==============================] - 0s 35ms/step
1/1 [==============================] - 0s 38ms/step
1/1 [==============================] - 0s 36ms/step
[ ]:
[ ]:
[ ]:
[ ]:
[ ]: