File size: 6,060 Bytes
aa3591b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a231ba0
 
 
 
aa3591b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras import layers
import matplotlib.pyplot as plt
import gradio as gr
import numpy as np
import pandas as pd
from PIL import Image as im
import PIL 
#%matplotlib inline
num_classes = 10
input_shape = (28, 28, 1)

objt=tf.keras.datasets.mnist
(X_train, y_train), (X_test,y_test)=objt.load_data()


# X_train = X_train.astype("float32") / 255
# X_test = X_test.astype("float32") / 255
# # Make sure images have shape (28, 28, 1)
# X_train = np.expand_dims(X_train, -1)
# X_test = np.expand_dims(X_test, -1)
# print("x_train shape:", X_train.shape)
# print(X_train.shape[0], "train samples")
# print(X_test.shape[0], "test samples")


# # convert class vectors to binary class matrices
# y_train = keras.utils.to_categorical(y_train, num_classes)
# y_test = keras.utils.to_categorical(y_test, num_classes)

# X_new=np.concatenate((X_train, X_test))
# y_new=np.concatenate((y_train, y_test))
# print(X_train.shape)
# print(X_new.shape)
# print(y_new.shape)

# print(y_train)

# model = keras.Sequential(
#     [
#         keras.Input(shape=input_shape),
#         layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
#         layers.MaxPooling2D(pool_size=(2, 2)),
#         layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
#         layers.MaxPooling2D(pool_size=(2, 2)),
#         layers.Flatten(),
#         layers.Dropout(0.5),
#         layers.Dense(num_classes, activation="softmax"),
#     ]
# )

# model.summary()

# batch_size = 128
# epochs = 15

# model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])

# model.fit(X_new, y_new, batch_size=batch_size, epochs=epochs, validation_split=0.1)
# model.save("keras_digit_test_include.h5")

# score = model.evaluate(X_test, y_test, verbose=0)
# print("Test loss:", score[0])
# print("Test accuracy:", score[1])

# loaded_model = keras.models.load_model('keras_digit_accurate.h5')
# score = loaded_model.evaluate(X_test, y_test, verbose=0)
# print("Test loss:", score[0])
# print("Test accuracy:", score[1])

#................................................................................................


# for i in range(9):
#     plt.subplot(330+1+i)
#     plt.imshow(X_train[i])
#     plt.show()

# X_train=X_train/255.0
# X_test=X_test/255.0

# model=tf.keras.models.Sequential([Flatten(input_shape=(28,28)),
                                        
#                                         Dense(650,activation='relu'),
                                        
#                                         Dense(450,activation='relu'),
                                        
#                                         Dense(250,activation='relu'),
                                        
#                                         Dense(150,activation='relu'),
                                        
#                                         Dense(10,activation=tf.nn.softmax)])

# model.compile(optimizer='adam',
#               loss='sparse_categorical_crossentropy',
#               metrics=['accuracy'])
# model.fit(X_train,y_train, epochs=10)
# model.save("keras_digit_temp.h5")
# test=X_test[0].reshape(-1,28,28)
# predicted=model.predict(test)
# print(predicted)

#count=0

def predict_digit(img):
    if img is not None:

        loaded_model = keras.models.load_model('keras_digit_test_include.h5')

        
        #img_data = im.fromarray(img)
        #img_data.save(f"image1.jpg")
        #count=count+1
        print(img)
        img_3d=img['background'].reshape(-1,28,28)
        print(img_3d)
        img_resized=img/255.0
        pred_prob=loaded_model.predict(img_resized)

        pred_prob=pred_prob*100

        print((pred_prob))
        

        simple = pd.DataFrame(
        {
        "a": ["0", "1", "2", "3", "4", "5", "6", "7", "8","9"],
        "b": pred_prob[0], 
        }
        )

        predicted_val=np.argmax(pred_prob)
        return int(predicted_val), gr.BarPlot.update(
            simple,
            x="a",
            y="b",
            x_title="Digits",
            y_title="Identification Probabilities",
            title="Identification Probability",
            tooltip=["a", "b"],
            vertical=False,
            y_lim=[0, 100],
        )
        
    else:
        simple_empty = pd.DataFrame(
        {
        "a": ["0", "1", "2", "3", "4", "5", "6", "7", "8","9"],
        "b": [0,0,0,0,0,0,0,0,0,0],
        }
        )

        return " ", gr.BarPlot.update(
            simple_empty,
            x="a",
            y="b",
            x_title="Digits",
            y_title="Identification Probabilities",
            title="Identification Probability",
            tooltip=["a", "b"],
            vertical=False,
            y_lim=[0, 100],
            
        )
        

# iface=gr.Interface(prdict_digit, inputs='sketchpad', outputs=['label', gr.Slider(0,100, label='Probably 0'), gr.Slider(0,100, label='Probably 1')] ).launch()

# iface.launch(debug='true')

css='''
#title_head{
text-align: center;
text-weight: bold;
text-size:30px;
}
#name_head{
text-align: center;
}
'''

with gr.Blocks(css=css) as demo:
    with gr.Row():
        with gr.Column():
            gr.Markdown("<h1>Digit Identifier</h1>", elem_id='title_head')
            gr.Markdown("<h2>Cyperts Project</h2>", elem_id="name_head")
    with gr.Row():
        with gr.Column():
            with gr.Row():
                skch=gr.Sketchpad()
            with gr.Row():
                with gr.Column():
                    clear=gr.ClearButton(skch)
                with gr.Column():
                    btn=gr.Button("Identify")
                    
        with gr.Column():
            gr.Markdown("Identified digit")
            label=gr.Label("")
            gr.Markdown("Other possible values")
            bar = gr.BarPlot()
    btn.click(predict_digit,inputs=skch,outputs=[label,bar])

           
        
    
demo.launch(debug=True)