Spaces:
Sleeping
Sleeping
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 | |
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() |