Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,8 @@
|
|
| 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,35 +13,56 @@
|
|
| 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 |
-
#
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
import cv2
|
| 45 |
from streamlit_drawable_canvas import st_canvas
|
| 46 |
from keras.models import load_model
|
|
@@ -90,17 +112,30 @@ with col2:
|
|
| 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 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
col3, col4 = st.columns([1, 1])
|
| 101 |
with col3:
|
| 102 |
-
st.image(
|
|
|
|
| 103 |
with col4:
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# from keras.models import load_model
|
| 5 |
# import numpy as np
|
| 6 |
|
| 7 |
+
# # Sidebar controls
|
| 8 |
+
# st.sidebar.title("Canvas Settings")
|
| 9 |
# drawing_mode = st.sidebar.selectbox("Drawing tool:", ("freedraw", "line", "rect", "circle", "transform"))
|
| 10 |
# stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 10)
|
| 11 |
# stroke_color = st.sidebar.color_picker("Stroke color hex: ", "#000000") # black
|
|
|
|
| 13 |
# bg_image = st.sidebar.file_uploader("Background image:", type=["png", "jpg"])
|
| 14 |
# realtime_update = st.sidebar.checkbox("Update in realtime", True)
|
| 15 |
|
| 16 |
+
# # Load model with caching
|
| 17 |
# @st.cache_resource
|
| 18 |
# def load_mnist_model():
|
| 19 |
# return load_model("mnist_model.keras")
|
| 20 |
|
| 21 |
# model = load_mnist_model()
|
| 22 |
|
| 23 |
+
# st.title("🖌️ Mindist: Draw a Number, Predict Instantly")
|
| 24 |
+
|
| 25 |
+
# # Create a two-column layout
|
| 26 |
+
# col1, col2 = st.columns([1, 1])
|
| 27 |
+
|
| 28 |
+
# with col1:
|
| 29 |
+
# st.subheader("Draw Here 👇")
|
| 30 |
+
# canvas_result = st_canvas(
|
| 31 |
+
# fill_color="rgba(255, 165, 0, 0.3)",
|
| 32 |
+
# stroke_width=stroke_width,
|
| 33 |
+
# stroke_color=stroke_color,
|
| 34 |
+
# background_color=bg_color,
|
| 35 |
+
# update_streamlit=realtime_update,
|
| 36 |
+
# height=280,
|
| 37 |
+
# width=280,
|
| 38 |
+
# drawing_mode=drawing_mode,
|
| 39 |
+
# key="canvas",
|
| 40 |
+
# )
|
| 41 |
+
|
| 42 |
+
# with col2:
|
| 43 |
+
# if canvas_result.image_data is not None:
|
| 44 |
+
# st.subheader("Original Drawing")
|
| 45 |
+
# st.image(canvas_result.image_data, use_column_width=True)
|
| 46 |
|
| 47 |
+
# # Below the two columns: Show preprocessing and prediction
|
| 48 |
# if canvas_result.image_data is not None:
|
| 49 |
+
# st.markdown("---")
|
| 50 |
+
# st.subheader("Preprocessed Image & Prediction")
|
| 51 |
+
|
| 52 |
# img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
| 53 |
+
# img = 255 - img # Invert colors
|
| 54 |
# img_resized = cv2.resize(img, (28, 28))
|
| 55 |
# img_normalized = img_resized / 255.0
|
| 56 |
# final_img = img_normalized.reshape(1, 28, 28, 1)
|
| 57 |
+
|
| 58 |
+
# col3, col4 = st.columns([1, 1])
|
| 59 |
+
# with col3:
|
| 60 |
+
# st.image(img_resized, caption="28x28 Preprocessed", clamp=True, channels="GRAY")
|
| 61 |
+
# with col4:
|
| 62 |
+
# prediction = model.predict(final_img)
|
| 63 |
+
# predicted_digit = np.argmax(prediction)
|
| 64 |
+
# st.markdown(f"### 🧠 Predicted Digit: **{predicted_digit}**")
|
| 65 |
+
import streamlit as st
|
| 66 |
import cv2
|
| 67 |
from streamlit_drawable_canvas import st_canvas
|
| 68 |
from keras.models import load_model
|
|
|
|
| 112 |
if canvas_result.image_data is not None:
|
| 113 |
st.markdown("---")
|
| 114 |
st.subheader("Preprocessed Image & Prediction")
|
| 115 |
+
|
| 116 |
img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
| 117 |
img = 255 - img # Invert colors
|
| 118 |
+
_, thresh_img = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY)
|
| 119 |
+
|
| 120 |
+
# Find contours of digits
|
| 121 |
+
contours, _ = cv2.findContours(thresh_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 122 |
+
contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0]) # Sort left-to-right
|
| 123 |
+
|
| 124 |
+
predictions = []
|
| 125 |
+
|
| 126 |
col3, col4 = st.columns([1, 1])
|
| 127 |
with col3:
|
| 128 |
+
st.image(img, caption="Thresholded Image", clamp=True, channels="GRAY")
|
| 129 |
+
|
| 130 |
with col4:
|
| 131 |
+
for cnt in contours:
|
| 132 |
+
x, y, w, h = cv2.boundingRect(cnt)
|
| 133 |
+
if w > 5 and h > 5: # Filter out noise/small contours
|
| 134 |
+
digit_roi = thresh_img[y:y+h, x:x+w]
|
| 135 |
+
digit_resized = cv2.resize(digit_roi, (28, 28))
|
| 136 |
+
digit_normalized = digit_resized / 255.0
|
| 137 |
+
input_img = digit_normalized.reshape(1, 28, 28, 1)
|
| 138 |
+
pred = np.argmax(model.predict(input_img))
|
| 139 |
+
predictions.append(str(pred))
|
| 140 |
+
|
| 141 |
+
st.markdown(f"### 🧠 Predicted Digits: **{''.join(predictions) if predictions else 'No digits found'}**")
|