-
Notifications
You must be signed in to change notification settings - Fork 0
/
handwritten_digits_recognition.py
55 lines (38 loc) · 1.57 KB
/
handwritten_digits_recognition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
import cv2
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
print("Made By Soumya Brajwasi and Paras Joshi")
train_new_model = True
if train_new_model:
mnist = tf.keras.datasets.mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = tf.keras.utils.normalize(X_train, axis=1)
X_test = tf.keras.utils.normalize(X_test, axis=1)
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(units=128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(units=128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(units=10, activation=tf.nn.softmax))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=3)
val_loss, val_acc = model.evaluate(X_test, y_test)
print(val_loss)
print(val_acc)
model.save('handwritten_digits.keras')
else:
model = tf.keras.models.load_model('handwritten_digits.model')
image_number = 1
while os.path.isfile('digits/digit{}.png'.format(image_number)):
try:
img = cv2.imread('digits/digit{}.png'.format(image_number))[:,:,0]
img = np.invert(np.array([img]))
prediction = model.predict(img)
print("The number is probably a {}".format(np.argmax(prediction)))
plt.imshow(img[0], cmap=plt.cm.binary)
plt.show()
image_number += 1
except:
print("Error reading image! Proceeding with next image...")
image_number += 1