Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
|
| 6 |
+
# Load trained model (placed in same directory as app.py)
|
| 7 |
+
@st.cache_resource # cache so model loads only once
|
| 8 |
+
def load_cnn_model():
|
| 9 |
+
return load_model("mnist_cnn.h5")
|
| 10 |
+
|
| 11 |
+
model = load_cnn_model()
|
| 12 |
+
|
| 13 |
+
st.title("🖊️ Handwritten Digit Recognition")
|
| 14 |
+
st.write("Upload an image of a digit (0–9) and the model will predict it.")
|
| 15 |
+
|
| 16 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
|
| 17 |
+
|
| 18 |
+
if uploaded_file is not None:
|
| 19 |
+
# Convert to grayscale and resize
|
| 20 |
+
img = Image.open(uploaded_file).convert('L')
|
| 21 |
+
img = img.resize((28,28))
|
| 22 |
+
|
| 23 |
+
# Preprocess
|
| 24 |
+
img_array = np.array(img) / 255.0
|
| 25 |
+
img_array = img_array.reshape(1,28,28,1)
|
| 26 |
+
|
| 27 |
+
# Predict
|
| 28 |
+
pred = model.predict(img_array)
|
| 29 |
+
pred_label = np.argmax(pred)
|
| 30 |
+
|
| 31 |
+
# Show results
|
| 32 |
+
st.image(img, caption=f"Predicted Digit: {pred_label}", width=150)
|
| 33 |
+
st.write("Prediction Probabilities:", pred)
|