|
|
import streamlit as st |
|
|
import numpy as np |
|
|
from PIL import Image |
|
|
from tensorflow.keras.models import load_model |
|
|
|
|
|
|
|
|
@st.cache_resource |
|
|
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: |
|
|
|
|
|
img = Image.open(uploaded_file).convert('L') |
|
|
img = img.resize((28,28)) |
|
|
|
|
|
|
|
|
img_array = np.array(img) / 255.0 |
|
|
img_array = img_array.reshape(1,28,28,1) |
|
|
|
|
|
|
|
|
pred = model.predict(img_array) |
|
|
pred_label = np.argmax(pred) |
|
|
|
|
|
|
|
|
st.image(img, caption=f"Predicted Digit: {pred_label}", width=150) |
|
|
st.write("Prediction Probabilities:", pred) |
|
|
|