File size: 2,632 Bytes
82d6f66
 
28a2b6f
342992b
 
 
 
 
 
 
 
 
5988410
342992b
 
 
82d6f66
 
bd4a019
82d6f66
 
65b4a39
82d6f66
5f9a6f6
 
342992b
 
 
ff78f6b
 
342992b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194ee29
 
342992b
 
 
 
 
 
0a81201
b4e3f01
0a81201
 
 
 
 
 
 
 
 
 
7fb8137
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

import streamlit as st
from PIL import Image
import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Convolution2D
from keras.layers.convolutional import MaxPooling2D
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from tensorflow.keras.utils import img_to_array, load_img
from keras import backend as K
from subprocess import check_output
from sklearn import preprocessing

"""
# AI_ML
"""

uploaded_file = st.file_uploader("Choose a picture", type=["png","jpg","jpeg"])

if uploaded_file is not None:
  st.image(Image.open(uploaded_file),width=250)
  
  

train = pd.read_csv("sign_mnist_train.csv").values
test = pd.read_csv("sign_mnist_test.csv").values

trainX = train[:, 1:].reshape(train.shape[0], 1, 28, 28).astype('float32')
X_train = trainX / 255.0
y_train = train[:, 0]

testX = test[:, 1:].reshape(test.shape[0], 1, 28, 28).astype('float32')
X_test = testX / 255.0
y_test = test[:, 0]

lb = preprocessing.LabelBinarizer()
y_train = lb.fit_transform(y_train)
y_test = lb.fit_transform(y_test)


model = Sequential()

try:
    if K.backend() == 'theano':
        K.set_image_data_format('channels_first')
    else:
        K.set_image_data_format('channels_last')
except AttributeError:
    if K._BACKEND == 'theano':
        K.set_image_dim_ordering('th')
    else:
        K.set_image_dim_ordering('tf')

model.add(Convolution2D(30, 5, 5, padding='same', input_shape=(1, 28, 28), activation='relu'))
model.add(MaxPooling2D(pool_size=(1, 1)))
model.add(Convolution2D(15, 3, 3, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), padding='same'))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(24, activation='relu'))
#model.add(Dense(50, activation='relu'))
model.add(Dense(24, activation='softmax'))

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

model.fit(X_train, y_train, epochs=20, batch_size=128)
score = model.evaluate(X_test, y_test, batch_size=128)
model.summary()



if uploaded_file is not None:

    img = load_img(uploaded_file, grayscale=True, target_size=(28, 28))
    img = img_to_array(img)
    img = img.reshape(1, 1, 28, 28)
    img = img.astype('float32')
    img = img / 255.0
    
    predict_x = model.predict(img)
    classes_x = np.argmax(predict_x,axis=1)
    
    sign = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y']
    st.write("Prediction : " + sign[classes_x[0]])