Spaces:
Sleeping
Sleeping
Download app.py from Phaneendrabayi/Digit_Recognizer: direct link, hf CLI and curl.
- Browser
- Download file 1.14 kB
-
https://huggingface.co/spaces/Phaneendrabayi/Digit_Recognizer/resolve/main/app.py
- Command line
-
hf download hf://spaces/Phaneendrabayi/Digit_Recognizer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Phaneendrabayi/Digit_Recognizer/resolve/main/app.py
1.14 kB
| 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}") | |