Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image, ImageOps
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="MNIST Digit Recognition", page_icon="🧮")
|
| 7 |
+
st.title("🧮 MNIST Digit Recognition App")
|
| 8 |
+
st.write("Upload a 28x28 grayscale image of a handwritten digit (0-9) to get the prediction.")
|
| 9 |
+
|
| 10 |
+
# Load the trained model
|
| 11 |
+
@st.cache_resource
|
| 12 |
+
def load_model():
|
| 13 |
+
model = tf.keras.models.load_model("mnist_digit_recognizer.keras")
|
| 14 |
+
return model
|
| 15 |
+
|
| 16 |
+
model = load_model()
|
| 17 |
+
|
| 18 |
+
# File uploader
|
| 19 |
+
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
| 20 |
+
|
| 21 |
+
if uploaded_file is not None:
|
| 22 |
+
# Open image and convert to grayscale
|
| 23 |
+
image = Image.open(uploaded_file).convert("L") # 'L' for grayscale
|
| 24 |
+
# Resize to 28x28 if needed
|
| 25 |
+
image = ImageOps.invert(image.resize((28,28)))
|
| 26 |
+
|
| 27 |
+
# Show image
|
| 28 |
+
st.image(image, caption="Uploaded Image", width=150)
|
| 29 |
+
|
| 30 |
+
# Preprocess image
|
| 31 |
+
img_array = np.array(image) / 255.0
|
| 32 |
+
img_array = img_array.reshape(1, 28, 28, 1)
|
| 33 |
+
|
| 34 |
+
# Predict
|
| 35 |
+
prediction = model.predict(img_array)
|
| 36 |
+
predicted_label = np.argmax(prediction)
|
| 37 |
+
|
| 38 |
+
st.success(f"Predicted Digit: {predicted_label}")
|
| 39 |
+
st.write("Prediction Probabilities:")
|
| 40 |
+
for i, prob in enumerate(prediction[0]):
|
| 41 |
+
st.write(f"{i}: {prob*100:.2f}%")
|