Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,13 @@ from streamlit_drawable_canvas import st_canvas
|
|
| 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)
|
|
@@ -26,11 +25,11 @@ def load_mnist_model():
|
|
| 26 |
|
| 27 |
model = load_mnist_model()
|
| 28 |
|
| 29 |
-
#
|
| 30 |
col1, col2 = st.columns(2)
|
| 31 |
|
| 32 |
with col1:
|
| 33 |
-
st.subheader("ποΈ Draw a Digit
|
| 34 |
canvas_result = st_canvas(
|
| 35 |
fill_color="rgba(255, 165, 0, 0.3)",
|
| 36 |
stroke_width=stroke_width,
|
|
@@ -45,21 +44,21 @@ with col1:
|
|
| 45 |
|
| 46 |
with col2:
|
| 47 |
if canvas_result.image_data is not None:
|
| 48 |
-
st.image(canvas_result.image_data, caption="
|
| 49 |
|
| 50 |
if st.button("Predict"):
|
|
|
|
| 51 |
img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
| 52 |
-
img = 255 - img # Invert
|
| 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 |
-
|
|
|
|
| 3 |
from keras.models import load_model
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
|
|
|
| 6 |
|
| 7 |
+
# Page setup
|
| 8 |
st.set_page_config(page_title="Digit Recognizer", layout="centered")
|
|
|
|
| 9 |
st.markdown("<h1 style='text-align: center;'>π§ Handwritten Digit Recognizer</h1>", unsafe_allow_html=True)
|
| 10 |
st.markdown("---")
|
| 11 |
|
| 12 |
+
# Sidebar settings
|
| 13 |
st.sidebar.header("π Drawing Settings")
|
| 14 |
drawing_mode = st.sidebar.selectbox("Drawing tool:", ("freedraw", "line", "rect", "circle", "transform"))
|
| 15 |
stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 10)
|
|
|
|
| 25 |
|
| 26 |
model = load_mnist_model()
|
| 27 |
|
| 28 |
+
# Layout columns
|
| 29 |
col1, col2 = st.columns(2)
|
| 30 |
|
| 31 |
with col1:
|
| 32 |
+
st.subheader("ποΈ Draw a Digit")
|
| 33 |
canvas_result = st_canvas(
|
| 34 |
fill_color="rgba(255, 165, 0, 0.3)",
|
| 35 |
stroke_width=stroke_width,
|
|
|
|
| 44 |
|
| 45 |
with col2:
|
| 46 |
if canvas_result.image_data is not None:
|
| 47 |
+
st.image(canvas_result.image_data, caption="Your Drawing")
|
| 48 |
|
| 49 |
if st.button("Predict"):
|
| 50 |
+
# Preprocess image
|
| 51 |
img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
| 52 |
+
img = 255 - img # Invert
|
| 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
|
| 58 |
prediction = model.predict(img_reshaped)
|
| 59 |
+
predicted_digit = int(np.argmax(prediction))
|
| 60 |
|
| 61 |
st.success(f"β
Predicted Digit: **{predicted_digit}**")
|
| 62 |
|
|
|
|
| 63 |
st.subheader("π Confidence Scores")
|
| 64 |
st.bar_chart(prediction[0])
|
|
|