leyuzak/Digit-Recognizer-Computer-Vision
0
1import streamlit as st
2import numpy as np
3import tensorflow as tf
4from PIL import Image, ImageOps
5from io import BytesIO
6
7st.set_page_config(page_title="MNIST", page_icon="🧠")
8
9@st.cache_resource
10def load_model():
11 return tf.keras.models.load_model("mnist_cnn.h5", compile=False)
12
13model = load_model()
14
15st.title("🧠 MNIST Digit Recognizer")
16
17file = st.file_uploader("Upload PNG/JPG", type=["png","jpg","jpeg"])
18
19def preprocess(img):
20 img = img.convert("L").resize((28,28))
21 arr = np.array(img).astype("float32")
22 if arr.mean() > 127:
23 img = ImageOps.invert(img)
24 arr = np.array(img).astype("float32")
25 arr = (arr/255.0).reshape(1,28,28,1)
26 return arr, img
27
28if file:
29 img = Image.open(BytesIO(file.getvalue()))
30 x, pimg = preprocess(img)
31 st.image(pimg, width=180)
32 probs = model.predict(x, verbose=0)[0]
33 pred = int(np.argmax(probs))
34 st.subheader(f"Prediction: {pred}")
35 st.bar_chart(probs)
36 