Spaces:
Sleeping
Sleeping
Commit
·
0eedd3d
1
Parent(s):
4562789
initail commit
Browse files- app.py +97 -0
- mnist_model.h5 +3 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from tensorflow import keras
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
from streamlit_drawable_canvas import st_canvas
|
| 8 |
+
|
| 9 |
+
# Set page config
|
| 10 |
+
st.set_page_config(page_title="MNIST Digit Recognition", page_icon="✏️", layout="centered")
|
| 11 |
+
|
| 12 |
+
# Load the saved model
|
| 13 |
+
@st.cache_resource()
|
| 14 |
+
def load_model():
|
| 15 |
+
return tf.keras.models.load_model('mnist_model.h5',
|
| 16 |
+
custom_objects={'InputLayer': keras.layers.InputLayer},
|
| 17 |
+
compile=False)
|
| 18 |
+
|
| 19 |
+
model = load_model()
|
| 20 |
+
|
| 21 |
+
# Custom CSS
|
| 22 |
+
st.markdown("""
|
| 23 |
+
<style>
|
| 24 |
+
.big-font {
|
| 25 |
+
font-size:30px !important;
|
| 26 |
+
font-weight: bold;
|
| 27 |
+
color: #1E90FF;
|
| 28 |
+
}
|
| 29 |
+
.result {
|
| 30 |
+
font-size: 24px;
|
| 31 |
+
font-weight: bold;
|
| 32 |
+
color: #32CD32;
|
| 33 |
+
}
|
| 34 |
+
.footer {
|
| 35 |
+
font-size: 14px;
|
| 36 |
+
text-align: center;
|
| 37 |
+
padding: 20px;
|
| 38 |
+
}
|
| 39 |
+
</style>
|
| 40 |
+
""", unsafe_allow_html=True)
|
| 41 |
+
|
| 42 |
+
# Create a Streamlit app
|
| 43 |
+
st.markdown("<p class='big-font'>MNIST Digit Recognition</p>", unsafe_allow_html=True)
|
| 44 |
+
st.write("Draw a digit or upload an image to see the model's prediction!")
|
| 45 |
+
|
| 46 |
+
# Add option to choose between drawing and uploading
|
| 47 |
+
option = st.radio("Choose input method:", ('Draw', 'Upload'))
|
| 48 |
+
|
| 49 |
+
if option == 'Draw':
|
| 50 |
+
# Create a canvas component
|
| 51 |
+
canvas_result = st_canvas(
|
| 52 |
+
fill_color="rgba(255, 255, 255, 0.3)",
|
| 53 |
+
stroke_width=20,
|
| 54 |
+
stroke_color="#FFFFFF",
|
| 55 |
+
background_color="#000000",
|
| 56 |
+
height=280,
|
| 57 |
+
width=280,
|
| 58 |
+
drawing_mode="freedraw",
|
| 59 |
+
key="canvas",
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
if canvas_result.image_data is not None:
|
| 63 |
+
image = Image.fromarray(canvas_result.image_data.astype('uint8'))
|
| 64 |
+
else:
|
| 65 |
+
image = None
|
| 66 |
+
|
| 67 |
+
else: # Upload option
|
| 68 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 69 |
+
if uploaded_file is not None:
|
| 70 |
+
image = Image.open(io.BytesIO(uploaded_file.read()))
|
| 71 |
+
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
| 72 |
+
else:
|
| 73 |
+
st.write("Please upload an image.")
|
| 74 |
+
image = None
|
| 75 |
+
|
| 76 |
+
# Add a button to make a prediction
|
| 77 |
+
if st.button('Predict', key='predict_button'):
|
| 78 |
+
if image is not None:
|
| 79 |
+
# Preprocess the image
|
| 80 |
+
image = image.convert('L') # Convert to grayscale
|
| 81 |
+
image = image.resize((28, 28))
|
| 82 |
+
image_array = np.array(image) / 255.0 # Normalize
|
| 83 |
+
image_array = image_array.reshape(1, 28, 28, 1).astype('float32')
|
| 84 |
+
|
| 85 |
+
# Make a prediction
|
| 86 |
+
with st.spinner('Predicting...'):
|
| 87 |
+
prediction = model.predict(image_array)
|
| 88 |
+
predicted_digit = np.argmax(prediction)
|
| 89 |
+
|
| 90 |
+
# Display the results
|
| 91 |
+
st.markdown(f"<p class='result'>Predicted Digit: {predicted_digit}</p>", unsafe_allow_html=True)
|
| 92 |
+
#st.balloons()
|
| 93 |
+
else:
|
| 94 |
+
st.warning("Please draw or upload an image before predicting.")
|
| 95 |
+
|
| 96 |
+
st.markdown("---")
|
| 97 |
+
st.markdown("<div class='footer'>Created with ❤️ using Streamlit and TensorFlow<br>by <a href='https://github.com/joshsalako' target='_blank'>Joshua Salako</a></div>", unsafe_allow_html=True)
|
mnist_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:232d5e06c32fb15fb9a19d5e192b848062a1634e0ba59c5cf12b136af21c78e5
|
| 3 |
+
size 2741032
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.22.0
|
| 2 |
+
tensorflow==2.12.0
|
| 3 |
+
numpy==1.23.5
|
| 4 |
+
Pillow==9.5.0
|
| 5 |
+
streamlit-drawable-canvas==0.9.2
|