Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- .gitattributes +1 -0
- app.py +96 -0
- mnist_cnn_model_32x32_balanced_dataset.keras +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
mnist_cnn_model_32x32_balanced_dataset.keras filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from streamlit_drawable_canvas import st_canvas
|
| 7 |
+
|
| 8 |
+
import cv2
|
| 9 |
+
|
| 10 |
+
from keras.models import load_model
|
| 11 |
+
|
| 12 |
+
# --- App Configuration ---
|
| 13 |
+
st.set_page_config(page_title="Handwritten Digit Recognizer", layout="centered")
|
| 14 |
+
st.markdown("""
|
| 15 |
+
<style>
|
| 16 |
+
.stButton>button {background-color: #4b7bec; color: white; border-radius: 8px; padding: 10px;}
|
| 17 |
+
.stButton>button:hover {background-color: #3867d6;}
|
| 18 |
+
</style>
|
| 19 |
+
""", unsafe_allow_html=True)
|
| 20 |
+
|
| 21 |
+
# --- Load Model ---
|
| 22 |
+
@st.cache_resource
|
| 23 |
+
def load_digit_model():
|
| 24 |
+
return load_model("mnist_cnn_model_32x32_balanced_dataset.keras")
|
| 25 |
+
|
| 26 |
+
# --- App Title and Instructions ---
|
| 27 |
+
st.title("✍️ Handwritten Digit Recognizer")
|
| 28 |
+
st.markdown("Draw a digit (0–9) in the box below and click **Predict** to see the result!")
|
| 29 |
+
|
| 30 |
+
with st.expander("ℹ️ How to Use", expanded=False):
|
| 31 |
+
st.markdown("""
|
| 32 |
+
- Draw a digit clearly in the center of the canvas.
|
| 33 |
+
- Use a thick stroke.
|
| 34 |
+
- Click **Predict** to see the result.
|
| 35 |
+
- Use **Clear Canvas** to start over.
|
| 36 |
+
""")
|
| 37 |
+
|
| 38 |
+
# --- Drawing Canvas ---
|
| 39 |
+
st.subheader("Draw Your Digit")
|
| 40 |
+
canvas_result = st_canvas(
|
| 41 |
+
fill_color="rgba(0, 0, 0, 0)",
|
| 42 |
+
stroke_width=20,
|
| 43 |
+
stroke_color="#000000",
|
| 44 |
+
background_color="#FFFFFF",
|
| 45 |
+
update_streamlit=True,
|
| 46 |
+
height=280,
|
| 47 |
+
width=280,
|
| 48 |
+
drawing_mode="freedraw",
|
| 49 |
+
key="canvas",
|
| 50 |
+
display_toolbar=True,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# --- Predict Button ---
|
| 54 |
+
predict_clicked = st.button("🔍 Predict", use_container_width=True, key="predict_button")
|
| 55 |
+
|
| 56 |
+
if predict_clicked and canvas_result.image_data is not None:
|
| 57 |
+
img = cv2.cvtColor(canvas_result.image_data.astype(np.uint8), cv2.COLOR_RGBA2GRAY)
|
| 58 |
+
|
| 59 |
+
if np.all(img == 255):
|
| 60 |
+
st.warning("⚠️ Please draw something before predicting!")
|
| 61 |
+
else:
|
| 62 |
+
img = 255 - img # Inverting the colors to mimic the dataset
|
| 63 |
+
img_resized = cv2.resize(img, (32, 32), interpolation=cv2.INTER_AREA)
|
| 64 |
+
img_normalized = img_resized.astype("float32") / 255.0
|
| 65 |
+
input_img = img_normalized.reshape(1, 32, 32, 1)
|
| 66 |
+
|
| 67 |
+
model = load_digit_model()
|
| 68 |
+
pred_probs = model.predict(input_img)
|
| 69 |
+
pred_class = np.argmax(pred_probs)
|
| 70 |
+
confidence = np.max(pred_probs)
|
| 71 |
+
|
| 72 |
+
st.subheader("Prediction Results")
|
| 73 |
+
col_img, col_result = st.columns([1, 2])
|
| 74 |
+
|
| 75 |
+
with col_img:
|
| 76 |
+
st.image(img_resized, caption="Processed Drawing", width=100, clamp=True)
|
| 77 |
+
|
| 78 |
+
with col_result:
|
| 79 |
+
st.success(f"🧠 Predicted Digit: **{pred_class}**")
|
| 80 |
+
st.info(f"🔍 Confidence: **{confidence * 100:.2f}%**")
|
| 81 |
+
|
| 82 |
+
# --- Plot probabilities ---
|
| 83 |
+
probs_df = pd.DataFrame({
|
| 84 |
+
"Digit": list(range(10)),
|
| 85 |
+
"Probability": pred_probs[0] * 100
|
| 86 |
+
})
|
| 87 |
+
fig = px.bar(probs_df, x="Digit", y="Probability",
|
| 88 |
+
title="Prediction Probabilities",
|
| 89 |
+
color="Probability",
|
| 90 |
+
color_continuous_scale="Blues",
|
| 91 |
+
height=300)
|
| 92 |
+
fig.update_layout(xaxis_title="Digit", yaxis_title="Probability (%)", xaxis=dict(tickmode="linear"))
|
| 93 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 94 |
+
|
| 95 |
+
elif predict_clicked:
|
| 96 |
+
st.warning("⚠️ Please draw something before predicting!")
|
mnist_cnn_model_32x32_balanced_dataset.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c9127a145f3cb5a18fe28d2eac6711e0fa9e21ebea84ec032eb9b655d748b505
|
| 3 |
+
size 3686272
|