Upload 787antitheft_195.py
Browse files- 787antitheft_195.py +51 -0
787antitheft_195.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""787antitheft.195
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1RuQfAM5faBjQTkTWdhahfka6eF7S0MGu
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import os
|
| 11 |
+
import cv2
|
| 12 |
+
import numpy as np
|
| 13 |
+
import matplotlib.pyplot as plt
|
| 14 |
+
import tensorflow as tf
|
| 15 |
+
|
| 16 |
+
mnist = tf.keras.datasets.mnist
|
| 17 |
+
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
| 18 |
+
|
| 19 |
+
x_train = tf.keras.utils.normalize(x_train, axis=1)
|
| 20 |
+
x_test = tf.keras.utils.normalize(x_test, axis=1)
|
| 21 |
+
|
| 22 |
+
model = tf.keras.models.Sequential()
|
| 23 |
+
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
|
| 24 |
+
model.add(tf.keras.layers.Dense(128, activation='relu'))
|
| 25 |
+
model.add(tf.keras.layers.Dense(128, activation='relu'))
|
| 26 |
+
model.add(tf.keras.layers.Dense(10, activation='softmax'))
|
| 27 |
+
|
| 28 |
+
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
| 29 |
+
|
| 30 |
+
model.fit(x_train, y_train, epochs=3)
|
| 31 |
+
|
| 32 |
+
model.save('handwritten.model')
|
| 33 |
+
|
| 34 |
+
model = tf.keras.models.load_model('handwritten.model')
|
| 35 |
+
|
| 36 |
+
loss, accimaguracy = model.evaluate(x_test, y_test)
|
| 37 |
+
|
| 38 |
+
image_number = 1
|
| 39 |
+
while os.path.isfile(f"digits/digit{image_number}.png"):
|
| 40 |
+
try:
|
| 41 |
+
img = cv2.imread(f"digit/digits{image_number}.png")[:,:,0]
|
| 42 |
+
img = np.invert(np.array([img]))
|
| 43 |
+
prediction = model.predict(img)
|
| 44 |
+
print(f"This digit is probably a {np.argmax(prediction)}")
|
| 45 |
+
plt.imshow(img[0], cmap=plt.cm.binary)
|
| 46 |
+
plt.show()
|
| 47 |
+
except:
|
| 48 |
+
print("Error!")
|
| 49 |
+
finally:
|
| 50 |
+
image_number += 1
|
| 51 |
+
|