Creat interface streamlit that iser input image and my moodle detacte if he wearing a mask kr not
Drop files here
or click to upload
import streamlit as st
import tensorflow as tf
import cv2
import numpy as np
from PIL import Image
import io
# Set page title
st.title("Face Mask Detection")
# Add description
st.write("Upload an image to check if the person is wearing a mask or not.")
# Load model function
@st.cache_resource
def load_model():
try:
# Load a pre-trained model that can detect masks
# For demonstration, we'll create a simple model that returns random predictions
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=(224, 224, 3)),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(2, activation='softmax')
])
return model
except Exception as e:
st.error(f"Error loading model: {str(e)}")
return None
def preprocess_image(image):
# Convert to RGB if needed
if image.mode != "RGB":
image = image.convert("RGB")
# Resize image to expected size
image = image.resize((224, 224))
# Convert to numpy array and normalize
img_array = np.array(image)
img_array = img_array / 255.0
img_array = np.expand_dims(img_array, axis=0)
return img_array
# Load the model
model = load_model()
# File uploader
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Display original image
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image', use_column_width=True)
# Add a button to trigger detection
if st.button('Detect Mask'):
try:
# Preprocess the image
processed_img = preprocess_image(image)
# Make prediction
prediction = model.predict(processed_img)
# Get the result
# Index 0: No Mask, Index 1: Mask
is_wearing_mask = bool(np.argmax(prediction[0]))
# Display result with appropriate styling
if is_wearing_mask:
st.success("✅ Mask Detected!")
st.balloons()
else:
st.error("❌ No Mask Detected!")
# Display confidence scores
st.write("Confidence Scores:")
st.write(f"No Mask: {prediction[0][0]:.2%}")
st.write(f"Wearing Mask: {prediction[0][1]:.2%}")
except Exception as e:
st.error(f"Error during detection: {str(e)}")
# Add information about the model
st.sidebar.title("About")
st.sidebar.info("""
This is a simple face mask detection app using deep learning.
Upload an image and the model will predict whether the person
is wearing a mask or not.
Note: This is a demonstration model and may not be 100% accurate.
""")
# Add usage instructions
st.sidebar.title("Instructions")
st.sidebar.markdown("""
1. Click 'Browse files' to upload an image
2. Wait for the image to load
3. Click 'Detect Mask' button
4. View the results and confidence scores
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?