Spaces:
Sleeping
Sleeping
first commit
Browse files- app.py +211 -0
- digitmodel.sav +0 -0
- keras_digit_test_include.h5 +3 -0
- requirements.txt +10 -0
app.py
ADDED
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
from tensorflow import keras
|
3 |
+
from tensorflow.keras import Sequential
|
4 |
+
from tensorflow.keras.layers import Dense, Flatten
|
5 |
+
from tensorflow.keras import layers
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
import gradio as gr
|
8 |
+
import numpy as np
|
9 |
+
import pandas as pd
|
10 |
+
from PIL import Image as im
|
11 |
+
import PIL
|
12 |
+
#%matplotlib inline
|
13 |
+
num_classes = 10
|
14 |
+
input_shape = (28, 28, 1)
|
15 |
+
|
16 |
+
objt=tf.keras.datasets.mnist
|
17 |
+
(X_train, y_train), (X_test,y_test)=objt.load_data()
|
18 |
+
|
19 |
+
|
20 |
+
# X_train = X_train.astype("float32") / 255
|
21 |
+
# X_test = X_test.astype("float32") / 255
|
22 |
+
# # Make sure images have shape (28, 28, 1)
|
23 |
+
# X_train = np.expand_dims(X_train, -1)
|
24 |
+
# X_test = np.expand_dims(X_test, -1)
|
25 |
+
# print("x_train shape:", X_train.shape)
|
26 |
+
# print(X_train.shape[0], "train samples")
|
27 |
+
# print(X_test.shape[0], "test samples")
|
28 |
+
|
29 |
+
|
30 |
+
# # convert class vectors to binary class matrices
|
31 |
+
# y_train = keras.utils.to_categorical(y_train, num_classes)
|
32 |
+
# y_test = keras.utils.to_categorical(y_test, num_classes)
|
33 |
+
|
34 |
+
# X_new=np.concatenate((X_train, X_test))
|
35 |
+
# y_new=np.concatenate((y_train, y_test))
|
36 |
+
# print(X_train.shape)
|
37 |
+
# print(X_new.shape)
|
38 |
+
# print(y_new.shape)
|
39 |
+
|
40 |
+
# print(y_train)
|
41 |
+
|
42 |
+
# model = keras.Sequential(
|
43 |
+
# [
|
44 |
+
# keras.Input(shape=input_shape),
|
45 |
+
# layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
|
46 |
+
# layers.MaxPooling2D(pool_size=(2, 2)),
|
47 |
+
# layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
|
48 |
+
# layers.MaxPooling2D(pool_size=(2, 2)),
|
49 |
+
# layers.Flatten(),
|
50 |
+
# layers.Dropout(0.5),
|
51 |
+
# layers.Dense(num_classes, activation="softmax"),
|
52 |
+
# ]
|
53 |
+
# )
|
54 |
+
|
55 |
+
# model.summary()
|
56 |
+
|
57 |
+
# batch_size = 128
|
58 |
+
# epochs = 15
|
59 |
+
|
60 |
+
# model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
|
61 |
+
|
62 |
+
# model.fit(X_new, y_new, batch_size=batch_size, epochs=epochs, validation_split=0.1)
|
63 |
+
# model.save("keras_digit_test_include.h5")
|
64 |
+
|
65 |
+
# score = model.evaluate(X_test, y_test, verbose=0)
|
66 |
+
# print("Test loss:", score[0])
|
67 |
+
# print("Test accuracy:", score[1])
|
68 |
+
|
69 |
+
# loaded_model = keras.models.load_model('keras_digit_accurate.h5')
|
70 |
+
# score = loaded_model.evaluate(X_test, y_test, verbose=0)
|
71 |
+
# print("Test loss:", score[0])
|
72 |
+
# print("Test accuracy:", score[1])
|
73 |
+
|
74 |
+
#................................................................................................
|
75 |
+
|
76 |
+
|
77 |
+
# for i in range(9):
|
78 |
+
# plt.subplot(330+1+i)
|
79 |
+
# plt.imshow(X_train[i])
|
80 |
+
# plt.show()
|
81 |
+
|
82 |
+
# X_train=X_train/255.0
|
83 |
+
# X_test=X_test/255.0
|
84 |
+
|
85 |
+
# model=tf.keras.models.Sequential([Flatten(input_shape=(28,28)),
|
86 |
+
|
87 |
+
# Dense(650,activation='relu'),
|
88 |
+
|
89 |
+
# Dense(450,activation='relu'),
|
90 |
+
|
91 |
+
# Dense(250,activation='relu'),
|
92 |
+
|
93 |
+
# Dense(150,activation='relu'),
|
94 |
+
|
95 |
+
# Dense(10,activation=tf.nn.softmax)])
|
96 |
+
|
97 |
+
# model.compile(optimizer='adam',
|
98 |
+
# loss='sparse_categorical_crossentropy',
|
99 |
+
# metrics=['accuracy'])
|
100 |
+
# model.fit(X_train,y_train, epochs=10)
|
101 |
+
# model.save("keras_digit_temp.h5")
|
102 |
+
# test=X_test[0].reshape(-1,28,28)
|
103 |
+
# predicted=model.predict(test)
|
104 |
+
# print(predicted)
|
105 |
+
|
106 |
+
#count=0
|
107 |
+
|
108 |
+
def predict_digit(img):
|
109 |
+
if img is not None:
|
110 |
+
|
111 |
+
loaded_model = keras.models.load_model('keras_digit_test_include.h5')
|
112 |
+
|
113 |
+
|
114 |
+
#img_data = im.fromarray(img)
|
115 |
+
#img_data.save(f"image1.jpg")
|
116 |
+
#count=count+1
|
117 |
+
img_3d=img.reshape(-1,28,28)
|
118 |
+
img_resized=img_3d/255.0
|
119 |
+
pred_prob=loaded_model.predict(img_resized)
|
120 |
+
|
121 |
+
pred_prob=pred_prob*100
|
122 |
+
|
123 |
+
print((pred_prob))
|
124 |
+
|
125 |
+
|
126 |
+
simple = pd.DataFrame(
|
127 |
+
{
|
128 |
+
"a": ["0", "1", "2", "3", "4", "5", "6", "7", "8","9"],
|
129 |
+
"b": pred_prob[0],
|
130 |
+
}
|
131 |
+
)
|
132 |
+
|
133 |
+
predicted_val=np.argmax(pred_prob)
|
134 |
+
return int(predicted_val), gr.BarPlot.update(
|
135 |
+
simple,
|
136 |
+
x="a",
|
137 |
+
y="b",
|
138 |
+
x_title="Digits",
|
139 |
+
y_title="Identification Probabilities",
|
140 |
+
title="Identification Probability",
|
141 |
+
tooltip=["a", "b"],
|
142 |
+
vertical=False,
|
143 |
+
y_lim=[0, 100],
|
144 |
+
)
|
145 |
+
|
146 |
+
else:
|
147 |
+
simple_empty = pd.DataFrame(
|
148 |
+
{
|
149 |
+
"a": ["0", "1", "2", "3", "4", "5", "6", "7", "8","9"],
|
150 |
+
"b": [0,0,0,0,0,0,0,0,0,0],
|
151 |
+
}
|
152 |
+
)
|
153 |
+
|
154 |
+
return " ", gr.BarPlot.update(
|
155 |
+
simple_empty,
|
156 |
+
x="a",
|
157 |
+
y="b",
|
158 |
+
x_title="Digits",
|
159 |
+
y_title="Identification Probabilities",
|
160 |
+
title="Identification Probability",
|
161 |
+
tooltip=["a", "b"],
|
162 |
+
vertical=False,
|
163 |
+
y_lim=[0, 100],
|
164 |
+
|
165 |
+
)
|
166 |
+
|
167 |
+
|
168 |
+
# iface=gr.Interface(prdict_digit, inputs='sketchpad', outputs=['label', gr.Slider(0,100, label='Probably 0'), gr.Slider(0,100, label='Probably 1')] ).launch()
|
169 |
+
|
170 |
+
# iface.launch(debug='true')
|
171 |
+
|
172 |
+
css='''
|
173 |
+
#title_head{
|
174 |
+
text-align: center;
|
175 |
+
text-weight: bold;
|
176 |
+
text-size:30px;
|
177 |
+
}
|
178 |
+
#name_head{
|
179 |
+
text-align: center;
|
180 |
+
}
|
181 |
+
'''
|
182 |
+
|
183 |
+
with gr.Blocks(css=css) as demo:
|
184 |
+
with gr.Row():
|
185 |
+
with gr.Column():
|
186 |
+
gr.Markdown("<h1>Digit Identifier</h1>", elem_id='title_head')
|
187 |
+
gr.Markdown("<h2>Cyperts Project</h2>", elem_id="name_head")
|
188 |
+
with gr.Row():
|
189 |
+
with gr.Column():
|
190 |
+
with gr.Row():
|
191 |
+
skch=gr.Sketchpad()
|
192 |
+
with gr.Row():
|
193 |
+
with gr.Column():
|
194 |
+
clear=gr.ClearButton(skch)
|
195 |
+
with gr.Column():
|
196 |
+
btn=gr.Button("Identify")
|
197 |
+
|
198 |
+
with gr.Column():
|
199 |
+
gr.Markdown("Identified digit")
|
200 |
+
label=gr.Label("")
|
201 |
+
gr.Markdown("Other possible values")
|
202 |
+
bar = gr.BarPlot()
|
203 |
+
btn.click(predict_digit,inputs=skch,outputs=[label,bar])
|
204 |
+
|
205 |
+
|
206 |
+
|
207 |
+
|
208 |
+
demo.launch(debug=True)
|
209 |
+
|
210 |
+
|
211 |
+
|
digitmodel.sav
ADDED
Binary file (643 kB). View file
|
|
keras_digit_test_include.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8f25136fc7407f27681f3d285251c4e19909874604f220786505dd887607168c
|
3 |
+
size 458272
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow
|
2 |
+
keras
|
3 |
+
|
4 |
+
scikit_learn<1.3.0
|
5 |
+
seaborn==0.12.2
|
6 |
+
|
7 |
+
#torchvision<0.15.2
|
8 |
+
gradio
|
9 |
+
|
10 |
+
#streamlit==1.26.0
|