Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
from streamlit_drawable_canvas import st_canvas
|
| 4 |
+
from keras.models import load_model
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
drawing_mode = st.sidebar.selectbox("Drawing tool:", ("freedraw", "line", "rect", "circle", "transform"))
|
| 9 |
+
stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 10)
|
| 10 |
+
stroke_color = st.sidebar.color_picker("Stroke color hex: ", "#000000") # black
|
| 11 |
+
bg_color = st.sidebar.color_picker("Background color hex: ", "#FFFFFF") # white
|
| 12 |
+
bg_image = st.sidebar.file_uploader("Background image:", type=["png", "jpg"])
|
| 13 |
+
realtime_update = st.sidebar.checkbox("Update in realtime", True)
|
| 14 |
+
|
| 15 |
+
@st.cache_resource
|
| 16 |
+
def load_mnist_model():
|
| 17 |
+
return load_model("mnist.keras")
|
| 18 |
+
|
| 19 |
+
model = load_mnist_model()
|
| 20 |
+
|
| 21 |
+
canvas_result = st_canvas(
|
| 22 |
+
fill_color="rgba(255, 165, 0, 0.3)",
|
| 23 |
+
stroke_width=stroke_width,
|
| 24 |
+
stroke_color=stroke_color,
|
| 25 |
+
background_color=bg_color,
|
| 26 |
+
update_streamlit=realtime_update,
|
| 27 |
+
height=280,
|
| 28 |
+
width=280,
|
| 29 |
+
drawing_mode=drawing_mode,
|
| 30 |
+
key="canvas",
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
if canvas_result.image_data is not None:
|
| 34 |
+
st.image(canvas_result.image_data, caption="Original Drawing")
|
| 35 |
+
img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
| 36 |
+
img = 255 - img
|
| 37 |
+
img_resized = cv2.resize(img, (28, 28))
|
| 38 |
+
img_normalized = img_resized / 255.0
|
| 39 |
+
final_img = img_normalized.reshape(1, 28, 28, 1)
|
| 40 |
+
st.image(img_resized, caption="Preprocessed (28x28)")
|
| 41 |
+
prediction = model.predict(final_img)
|
| 42 |
+
st.write("Prediction:", np.argmax(prediction))
|