anamjafar6's picture
Create app.py
504f72d verified
raw
history blame
995 Bytes
import streamlit as st
import numpy as np
from PIL import Image
from tensorflow.keras.models import load_model
# Load trained model (placed in same directory as app.py)
@st.cache_resource # cache so model loads only once
def load_cnn_model():
return load_model("mnist_cnn.h5")
model = load_cnn_model()
st.title("๐Ÿ–Š๏ธ Handwritten Digit Recognition")
st.write("Upload an image of a digit (0โ€“9) and the model will predict it.")
uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
if uploaded_file is not None:
# Convert to grayscale and resize
img = Image.open(uploaded_file).convert('L')
img = img.resize((28,28))
# Preprocess
img_array = np.array(img) / 255.0
img_array = img_array.reshape(1,28,28,1)
# Predict
pred = model.predict(img_array)
pred_label = np.argmax(pred)
# Show results
st.image(img, caption=f"Predicted Digit: {pred_label}", width=150)
st.write("Prediction Probabilities:", pred)