import streamlit as st import cv2 import numpy as np from keras.models import load_model from streamlit_drawable_canvas import st_canvas # Load the trained model model = load_model("model.keras") st.set_page_config(page_title="Digit Recognizer") st.title("✍️ Draw a Digit (0-9)") # Canvas for drawing digits canvas_result = st_canvas( fill_color="black", # Background stroke_width=10, stroke_color="white", background_color="black", height=280, width=280, drawing_mode="freedraw", key="canvas" ) # Preprocess the image for prediction def preprocess(img): img = cv2.resize(img, (28, 28)) # Resize to match model input img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Convert to grayscale img = img / 255.0 # Normalize img = img.reshape(1, 28, 28) # Reshape for model input return img # Predict button if canvas_result.image_data is not None and st.button("Predict"): img = canvas_result.image_data.astype("uint8") input_img = preprocess(img) prediction = model.predict(input_img) digit = np.argmax(prediction) st.subheader(f"🧠 Predicted Digit: {digit}")