Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
@@ -12,31 +55,52 @@ 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_model.keras")
|
| 18 |
|
| 19 |
model = load_mnist_model()
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
|
|
|
| 33 |
if canvas_result.image_data is not None:
|
| 34 |
-
st.
|
|
|
|
|
|
|
| 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 |
-
|
| 41 |
-
|
| 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_model.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))
|
| 43 |
import streamlit as st
|
| 44 |
import cv2
|
| 45 |
from streamlit_drawable_canvas import st_canvas
|
| 46 |
from keras.models import load_model
|
| 47 |
import numpy as np
|
| 48 |
|
| 49 |
+
# Sidebar controls
|
| 50 |
+
st.sidebar.title("Canvas Settings")
|
| 51 |
drawing_mode = st.sidebar.selectbox("Drawing tool:", ("freedraw", "line", "rect", "circle", "transform"))
|
| 52 |
stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 10)
|
| 53 |
stroke_color = st.sidebar.color_picker("Stroke color hex: ", "#000000") # black
|
|
|
|
| 55 |
bg_image = st.sidebar.file_uploader("Background image:", type=["png", "jpg"])
|
| 56 |
realtime_update = st.sidebar.checkbox("Update in realtime", True)
|
| 57 |
|
| 58 |
+
# Load model with caching
|
| 59 |
@st.cache_resource
|
| 60 |
def load_mnist_model():
|
| 61 |
return load_model("mnist_model.keras")
|
| 62 |
|
| 63 |
model = load_mnist_model()
|
| 64 |
|
| 65 |
+
st.title("🖌️ Mindist: Draw a Number, Predict Instantly")
|
| 66 |
+
|
| 67 |
+
# Create a two-column layout
|
| 68 |
+
col1, col2 = st.columns([1, 1])
|
| 69 |
+
|
| 70 |
+
with col1:
|
| 71 |
+
st.subheader("Draw Here 👇")
|
| 72 |
+
canvas_result = st_canvas(
|
| 73 |
+
fill_color="rgba(255, 165, 0, 0.3)",
|
| 74 |
+
stroke_width=stroke_width,
|
| 75 |
+
stroke_color=stroke_color,
|
| 76 |
+
background_color=bg_color,
|
| 77 |
+
update_streamlit=realtime_update,
|
| 78 |
+
height=280,
|
| 79 |
+
width=280,
|
| 80 |
+
drawing_mode=drawing_mode,
|
| 81 |
+
key="canvas",
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
with col2:
|
| 85 |
+
if canvas_result.image_data is not None:
|
| 86 |
+
st.subheader("Original Drawing")
|
| 87 |
+
st.image(canvas_result.image_data, use_column_width=True)
|
| 88 |
|
| 89 |
+
# Below the two columns: Show preprocessing and prediction
|
| 90 |
if canvas_result.image_data is not None:
|
| 91 |
+
st.markdown("---")
|
| 92 |
+
st.subheader("Preprocessed Image & Prediction")
|
| 93 |
+
|
| 94 |
img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
| 95 |
+
img = 255 - img # Invert colors
|
| 96 |
img_resized = cv2.resize(img, (28, 28))
|
| 97 |
img_normalized = img_resized / 255.0
|
| 98 |
final_img = img_normalized.reshape(1, 28, 28, 1)
|
| 99 |
+
|
| 100 |
+
col3, col4 = st.columns([1, 1])
|
| 101 |
+
with col3:
|
| 102 |
+
st.image(img_resized, caption="28x28 Preprocessed", clamp=True, channels="GRAY")
|
| 103 |
+
with col4:
|
| 104 |
+
prediction = model.predict(final_img)
|
| 105 |
+
predicted_digit = np.argmax(prediction)
|
| 106 |
+
st.markdown(f"### 🧠 Predicted Digit: **{predicted_digit}**")
|