Kolpitor commited on
Commit
342992b
1 Parent(s): 7017963

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -2
app.py CHANGED
@@ -1,6 +1,19 @@
1
 
2
  import streamlit as st
3
  from PIL import Image
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  """
6
  # AI_ML
@@ -8,7 +21,62 @@ from PIL import Image
8
 
9
  uploaded_file = st.file_uploader("Choose a picture", type=["png","jpg","jpeg"])
10
 
11
- st.write(uploaded_file)
12
-
13
  if uploaded_file is not None:
14
  st.image(Image.open(uploaded_file),width=250)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
  import streamlit as st
3
  from PIL import Image
4
+ import pandas as pd
5
+ import numpy as np
6
+ from keras.models import Sequential
7
+ from keras.layers import Dense
8
+ from keras.layers import Dropout
9
+ from keras.layers import Flatten
10
+ from keras.layers.convolutional import Convolution2D
11
+ from keras.layers.convolutional import MaxPooling2D
12
+ from sklearn.preprocessing import LabelEncoder, OneHotEncoder
13
+ from keras.preprocessing.image import img_to_array, load_img
14
+ from keras import backend as K
15
+ from subprocess import check_output
16
+ from sklearn import preprocessing
17
 
18
  """
19
  # AI_ML
 
21
 
22
  uploaded_file = st.file_uploader("Choose a picture", type=["png","jpg","jpeg"])
23
 
 
 
24
  if uploaded_file is not None:
25
  st.image(Image.open(uploaded_file),width=250)
26
+
27
+
28
+
29
+ train = pd.read_csv("./sign_mnist_train.csv").values
30
+ test = pd.read_csv("./sign_mnist_test.csv").values
31
+
32
+ trainX = train[:, 1:].reshape(train.shape[0], 1, 28, 28).astype('float32')
33
+ X_train = trainX / 255.0
34
+ y_train = train[:, 0]
35
+
36
+ testX = test[:, 1:].reshape(test.shape[0], 1, 28, 28).astype('float32')
37
+ X_test = testX / 255.0
38
+ y_test = test[:, 0]
39
+
40
+ lb = preprocessing.LabelBinarizer()
41
+ y_train = lb.fit_transform(y_train)
42
+ y_test = lb.fit_transform(y_test)
43
+
44
+
45
+ model = Sequential()
46
+
47
+ try:
48
+ if K.backend() == 'theano':
49
+ K.set_image_data_format('channels_first')
50
+ else:
51
+ K.set_image_data_format('channels_last')
52
+ except AttributeError:
53
+ if K._BACKEND == 'theano':
54
+ K.set_image_dim_ordering('th')
55
+ else:
56
+ K.set_image_dim_ordering('tf')
57
+
58
+ model.add(Convolution2D(30, 5, 5, padding='same', input_shape=(1, 28, 28), activation='relu'))
59
+ model.add(MaxPooling2D(pool_size=(1, 1)))
60
+ model.add(Convolution2D(15, 3, 3, padding='same', activation='relu'))
61
+ model.add(MaxPooling2D(pool_size=(2, 2), padding='same'))
62
+ model.add(Dropout(0.2))
63
+ model.add(Flatten())
64
+ model.add(Dense(24, activation='relu'))
65
+ #model.add(Dense(50, activation='relu'))
66
+ model.add(Dense(24, activation='softmax'))
67
+
68
+ model.fit(X_train, y_train, epochs=20, batch_size=128)
69
+ score = model.evaluate(X_test, y_test, batch_size=128)
70
+ model.summary()
71
+
72
+
73
+
74
+ img = load_img("o.jpg", grayscale=True, target_size=(28, 28))
75
+ img = img_to_array(img)
76
+ img = img.reshape(1, 1, 28, 28)
77
+ img = img.astype('float32')
78
+ img = img / 255.0
79
+
80
+ predict_x = model.predict(img)
81
+ classes_x = np.argmax(predict_x,axis=1)
82
+ print(classes_x[0])