File size: 1,533 Bytes
0106479
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from PIL import Image
import numpy as np
from tensorflow.keras.models import load_model

# Define a function to make predictions on a single image
def predict_single_image(image, model):
    # Resize and preprocess the image
    img = image.resize((128, 128))
    img = np.array(img) / 255.0  # Normalization
    img = np.expand_dims(img, axis=0)  # Add batch dimension

    # Make prediction using the provided model
    prediction = model.predict(img)

    # Thresholding prediction
    threshold = 0.5
    prediction_class = (prediction > threshold).astype(int)

    # Interpret prediction
    if prediction_class == 1:
        return "With Mask"
    else:
        return "Without Mask"

# Load the model from .h5 file
@st.cache(allow_output_mutation=True)
def load_model_from_h5():
    return load_model('model.h5')

# Streamlit app
def main():
    st.title("Mask Detection App")
    uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])

    if uploaded_file is not None:
        # Load the image
        image = Image.open(uploaded_file)
        st.image(image, caption='Uploaded Image.', use_column_width=True)

        # Button to make prediction
        if st.button('Predict'):
            # Load the model from .h5 file
            model_h5 = load_model_from_h5()
            # Make predictions using the provided model
            prediction = predict_single_image(image, model_h5)
            st.write("Prediction:", prediction)

if __name__ == '__main__':
    main()