Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,15 @@ from streamlit_drawable_canvas import st_canvas
|
|
| 3 |
from keras.models import load_model
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
drawing_mode = st.sidebar.selectbox("Drawing tool:", ("freedraw", "line", "rect", "circle", "transform"))
|
| 8 |
stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 10)
|
| 9 |
stroke_color = st.sidebar.color_picker("Stroke color hex: ", "#000000") # black
|
|
@@ -11,31 +19,47 @@ bg_color = st.sidebar.color_picker("Background color hex: ", "#FFFFFF") # white
|
|
| 11 |
bg_image = st.sidebar.file_uploader("Background image:", type=["png", "jpg"])
|
| 12 |
realtime_update = st.sidebar.checkbox("Update in realtime", True)
|
| 13 |
|
|
|
|
| 14 |
@st.cache_resource
|
| 15 |
def load_mnist_model():
|
| 16 |
return load_model("digit_recognition.keras")
|
| 17 |
|
| 18 |
model = load_mnist_model()
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from keras.models import load_model
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
|
| 8 |
+
st.set_page_config(page_title="Digit Recognizer", layout="centered")
|
| 9 |
+
|
| 10 |
+
st.markdown("<h1 style='text-align: center;'>🧠 Handwritten Digit Recognizer</h1>", unsafe_allow_html=True)
|
| 11 |
+
st.markdown("---")
|
| 12 |
+
|
| 13 |
+
# Sidebar
|
| 14 |
+
st.sidebar.header("🛠 Drawing Settings")
|
| 15 |
drawing_mode = st.sidebar.selectbox("Drawing tool:", ("freedraw", "line", "rect", "circle", "transform"))
|
| 16 |
stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 10)
|
| 17 |
stroke_color = st.sidebar.color_picker("Stroke color hex: ", "#000000") # black
|
|
|
|
| 19 |
bg_image = st.sidebar.file_uploader("Background image:", type=["png", "jpg"])
|
| 20 |
realtime_update = st.sidebar.checkbox("Update in realtime", True)
|
| 21 |
|
| 22 |
+
# Load model
|
| 23 |
@st.cache_resource
|
| 24 |
def load_mnist_model():
|
| 25 |
return load_model("digit_recognition.keras")
|
| 26 |
|
| 27 |
model = load_mnist_model()
|
| 28 |
|
| 29 |
+
# Canvas and Prediction
|
| 30 |
+
col1, col2 = st.columns(2)
|
| 31 |
+
|
| 32 |
+
with col1:
|
| 33 |
+
st.subheader("🖌️ Draw a Digit Below")
|
| 34 |
+
canvas_result = st_canvas(
|
| 35 |
+
fill_color="rgba(255, 165, 0, 0.3)",
|
| 36 |
+
stroke_width=stroke_width,
|
| 37 |
+
stroke_color=stroke_color,
|
| 38 |
+
background_color=bg_color,
|
| 39 |
+
update_streamlit=realtime_update,
|
| 40 |
+
height=280,
|
| 41 |
+
width=280,
|
| 42 |
+
drawing_mode=drawing_mode,
|
| 43 |
+
key="canvas"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
with col2:
|
| 47 |
+
if canvas_result.image_data is not None:
|
| 48 |
+
st.image(canvas_result.image_data, caption="🖼️ Your Drawing")
|
| 49 |
+
|
| 50 |
+
if st.button("Predict"):
|
| 51 |
+
img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
| 52 |
+
img = 255 - img # Invert colors
|
| 53 |
+
img_resized = cv2.resize(img, (28, 28))
|
| 54 |
+
img_normalized = img_resized / 255.0
|
| 55 |
+
img_reshaped = img_normalized.reshape((1, 28, 28))
|
| 56 |
+
|
| 57 |
+
prediction = model.predict(img_reshaped)
|
| 58 |
+
predicted_digit = np.argmax(prediction)
|
| 59 |
+
|
| 60 |
+
st.success(f"✅ Predicted Digit: **{predicted_digit}**")
|
| 61 |
+
|
| 62 |
+
# Show probabilities
|
| 63 |
+
st.subheader("📊 Confidence Scores")
|
| 64 |
+
st.bar_chart(prediction[0])
|
| 65 |
+
|