File size: 1,027 Bytes
a536c76
8028ab5
 
 
 
 
 
 
 
d6cc3d8
8028ab5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import streamlit as st
import numpy as np
import joblib
from PIL import Image, ImageOps

st.title('Handwritten Digit Recognizer')

# Load the model
try:
    model = joblib.load('src/digit_rf_model.joblib')
except Exception as e:
    st.error(f"Error loading model: {e}")

uploaded_file = st.file_uploader("Upload a digit image (28x28 grayscale)", type=["png", "jpg", "jpeg"])

def preprocess_image(img):
    # Convert to grayscale, resize to 28x28, flatten
    img = ImageOps.grayscale(img)
    img = img.resize((28, 28))
    arr = np.array(img).reshape(1, -1)
    return arr

if uploaded_file is not None:
    try:
        image = Image.open(uploaded_file)
        st.image(image, caption='Uploaded Image', use_column_width=True)
        input_data = preprocess_image(image)
        if st.button('Predict Digit'):
            prediction = model.predict(input_data)
            st.success(f'Predicted Digit: {int(prediction[0])}')
    except Exception as e:
        st.error(f"Error processing image or making prediction: {e}")