Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,18 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import numpy as np
|
| 3 |
-
import cv2
|
| 4 |
-
from PIL import Image
|
| 5 |
-
from tensorflow.keras.models import load_model
|
| 6 |
-
from streamlit_drawable_canvas import st_canvas
|
| 7 |
-
|
| 8 |
-
#
|
| 9 |
st.set_page_config(
|
| 10 |
page_title="Digit Recognition App",
|
| 11 |
page_icon="π’",
|
| 12 |
layout="wide"
|
| 13 |
)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
st.markdown(
|
| 17 |
"""
|
| 18 |
<style>
|
|
@@ -24,27 +24,27 @@ st.markdown(
|
|
| 24 |
unsafe_allow_html=True
|
| 25 |
)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
@st.cache_resource
|
| 29 |
-
def load_cnn_model():
|
| 30 |
-
return load_model("mnist_cnn.h5")
|
| 31 |
|
| 32 |
-
model = load_cnn_model()
|
| 33 |
|
| 34 |
# ---- Preprocessing Helpers ----
|
| 35 |
-
def preprocess_pil_file(file_or_pil_image):
|
| 36 |
if not isinstance(file_or_pil_image, Image.Image):
|
| 37 |
-
img = Image.open(file_or_pil_image)
|
| 38 |
else:
|
| 39 |
-
img = file_or_pil_image
|
| 40 |
|
| 41 |
-
img = img.convert('L').resize((28, 28))
|
| 42 |
-
arr = np.array(img).astype('float32') / 255.0
|
| 43 |
|
| 44 |
if arr.mean() > 0.5: # invert if background is white
|
| 45 |
arr = 1.0 - arr
|
| 46 |
|
| 47 |
-
arr = arr.reshape(1, 28, 28, 1).astype('float32')
|
| 48 |
return arr, Image.fromarray((arr[0, :, :, 0] * 255).astype('uint8'))
|
| 49 |
|
| 50 |
def preprocess_canvas_image(image_data):
|
|
@@ -60,7 +60,7 @@ def preprocess_canvas_image(image_data):
|
|
| 60 |
comp = (rgb * alpha[..., None] + white * (1 - alpha[..., None])).astype('uint8')
|
| 61 |
gray = cv2.cvtColor(comp, cv2.COLOR_RGB2GRAY)
|
| 62 |
else:
|
| 63 |
-
gray = cv2.cvtColor(img_uint8, cv2.COLOR_RGB2GRAY)
|
| 64 |
|
| 65 |
small = cv2.resize(gray, (28, 28), interpolation=cv2.INTER_AREA).astype('float32') / 255.0
|
| 66 |
|
|
@@ -84,8 +84,8 @@ st.sidebar.info(
|
|
| 84 |
"β’ Predictions show digit + confidence & probability bar chart."
|
| 85 |
)
|
| 86 |
st.sidebar.markdown("---")
|
| 87 |
-
st.sidebar.write("π©βπ» **About**: Built with β€οΈ by **Anam Jafar**")
|
| 88 |
-
st.sidebar.write("[π LinkedIn](https://www.linkedin.com/in/anam-
|
| 89 |
|
| 90 |
# ---- File Upload ----
|
| 91 |
uploaded_files = st.file_uploader(
|
|
|
|
| 1 |
+
import streamlit as st # makes the web app
|
| 2 |
+
import numpy as np #handles math & image arrays
|
| 3 |
+
import cv2 #image processing(resize, grayscale)
|
| 4 |
+
from PIL import Image # image handling, works with uploaded images
|
| 5 |
+
from tensorflow.keras.models import load_model #loads the trained mnist model
|
| 6 |
+
from streamlit_drawable_canvas import st_canvas #Imports the drawable canvas component for Streamlit which lets you draw digits inside app
|
| 7 |
+
|
| 8 |
+
#This sets up the web app title, icon, and layout.
|
| 9 |
st.set_page_config(
|
| 10 |
page_title="Digit Recognition App",
|
| 11 |
page_icon="π’",
|
| 12 |
layout="wide"
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# The CSS part changes the background color gradient
|
| 16 |
st.markdown(
|
| 17 |
"""
|
| 18 |
<style>
|
|
|
|
| 24 |
unsafe_allow_html=True
|
| 25 |
)
|
| 26 |
|
| 27 |
+
# This loads the trained model (mnist_cnn.h5) that recognizes digits.
|
| 28 |
+
@st.cache_resource #makes sure the model loads only once, not every time you interact.
|
| 29 |
+
def load_cnn_model():
|
| 30 |
+
return load_model("mnist_cnn.h5") #loads the saved model file mnist_cnn.h5.
|
| 31 |
|
| 32 |
+
model = load_cnn_model() #stores the loaded model in the variable model so you can call it later.
|
| 33 |
|
| 34 |
# ---- Preprocessing Helpers ----
|
| 35 |
+
def preprocess_pil_file(file_or_pil_image): #Defines a function which accepts either a file or a Image.
|
| 36 |
if not isinstance(file_or_pil_image, Image.Image):
|
| 37 |
+
img = Image.open(file_or_pil_image) #If the input is not already a PIL Image object, use Image.open() to load it. Otherwise, use it directly.
|
| 38 |
else:
|
| 39 |
+
img = file_or_pil_image #PIL = Python Imaging Library .Itβs a library that helps you open, edit, and save images in Python.
|
| 40 |
|
| 41 |
+
img = img.convert('L').resize((28, 28)) # Convert to grayscale & resize
|
| 42 |
+
arr = np.array(img).astype('float32') / 255.0 # Normalize (0β1 scale)
|
| 43 |
|
| 44 |
if arr.mean() > 0.5: # invert if background is white
|
| 45 |
arr = 1.0 - arr
|
| 46 |
|
| 47 |
+
arr = arr.reshape(1, 28, 28, 1).astype('float32') #Reshape to fit model input.
|
| 48 |
return arr, Image.fromarray((arr[0, :, :, 0] * 255).astype('uint8'))
|
| 49 |
|
| 50 |
def preprocess_canvas_image(image_data):
|
|
|
|
| 60 |
comp = (rgb * alpha[..., None] + white * (1 - alpha[..., None])).astype('uint8')
|
| 61 |
gray = cv2.cvtColor(comp, cv2.COLOR_RGB2GRAY)
|
| 62 |
else:
|
| 63 |
+
gray = cv2.cvtColor(img_uint8, cv2.COLOR_RGB2GRAY) # convert canvas to grayscale
|
| 64 |
|
| 65 |
small = cv2.resize(gray, (28, 28), interpolation=cv2.INTER_AREA).astype('float32') / 255.0
|
| 66 |
|
|
|
|
| 84 |
"β’ Predictions show digit + confidence & probability bar chart."
|
| 85 |
)
|
| 86 |
st.sidebar.markdown("---")
|
| 87 |
+
st.sidebar.write("π©βπ» **About**: Built with streamlitβ€οΈ by **Anam Jafar**")
|
| 88 |
+
st.sidebar.write("[π LinkedIn](https://www.linkedin.com/in/anam-jafar6/)")
|
| 89 |
|
| 90 |
# ---- File Upload ----
|
| 91 |
uploaded_files = st.file_uploader(
|