File size: 3,324 Bytes
235bed0
 
 
 
 
 
a481416
235bed0
 
 
06308c8
235bed0
 
 
 
 
 
 
 
 
 
 
06308c8
235bed0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06308c8
235bed0
 
 
06308c8
235bed0
 
06308c8
235bed0
 
 
 
 
06308c8
235bed0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import streamlit as st
import tensorflow as tf
import numpy as np
from PIL import Image
import io
import logging

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Load the pre-trained MNIST model
@st.cache_resource
def load_model():
    try:
        model = tf.keras.models.load_model('mnist_cnn.h5')
        logger.info("MNIST model loaded successfully")
        return model
    except Exception as e:
        logger.error(f"Error loading model: {e}")
        st.error("Failed to load the model. Please check the model file.")
        return None

# Preprocess the uploaded image
def preprocess_image(image):
    try:
        # Convert to grayscale
        img = image.convert('L')
        # Resize to 28x28 (MNIST model input size)
        img = img.resize((28, 28), Image.Resampling.LANCZOS)
        # Convert to numpy array and normalize
        img_array = np.array(img)
        # Ensure the image is inverted if necessary (MNIST expects white digits on black background)
        img_array = 255 - img_array  # Invert colors
        img_array = img_array / 255.0  # Normalize to [0, 1]
        # Reshape for model input (1, 28, 28, 1)
        img_array = img_array.reshape(1, 28, 28, 1)
        logger.info("Image preprocessed successfully")
        return img_array
    except Exception as e:
        logger.error(f"Error preprocessing image: {e}")
        st.error("Failed to preprocess the image. Please ensure it's a valid image.")
        return None

# Streamlit app
st.title("AutoWeightLogger - Number Detection")
st.write("Upload an image containing a single handwritten digit to detect the number.")

# File uploader
uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])

if uploaded_file is not None:
    try:
        # Display the uploaded image
        image = Image.open(uploaded_file)
        st.image(image, caption="Uploaded Image", use_column_width=True)

        # Preprocess the image
        processed_image = preprocess_image(image)
        if processed_image is None:
            st.stop()

        # Load the model
        model = load_model()
        if model is None:
            st.stop()

        # Make prediction
        with st.spinner("Detecting number..."):
            prediction = model.predict(processed_image)
            predicted_digit = np.argmax(prediction, axis=1)[0]
            confidence = np.max(prediction) * 100

        # Display result
        st.success(f"Detected Number: {predicted_digit}")
        st.write(f"Confidence: {confidence:.2f}%")

        # Provide feedback if confidence is low
        if confidence < 70:
            st.warning("Low confidence in prediction. Please ensure the image contains a clear, single handwritten digit.")

    except Exception as e:
        logger.error(f"Error processing image: {e}")
        st.error("An error occurred while processing the image. Please try again with a different image.")
else:
    st.info("Please upload an image to proceed.")

# Instructions for users
st.markdown("""
### Instructions
1. Upload an image containing a single handwritten digit (0-9).
2. Ensure the digit is clear, centered, and on a plain background for best results.
3. The model expects white digits on a black background, similar to MNIST dataset images.
""")