Spaces:
Sleeping
Sleeping
File size: 4,489 Bytes
4d4558a a5b05e7 98f27b5 4d4558a a5b05e7 f64359a 98f27b5 f64359a 98f27b5 312dfa3 98f27b5 f64359a 98f27b5 312dfa3 f64359a 312dfa3 f64359a 98f27b5 312dfa3 98f27b5 312dfa3 98f27b5 f64359a 98f27b5 312dfa3 98f27b5 312dfa3 98f27b5 312dfa3 2f22d9a 312dfa3 2f22d9a 70c408e 312dfa3 98f27b5 312dfa3 98f27b5 5409c3a 98f27b5 818c8e3 312dfa3 98f27b5 6465969 2f22d9a 6465969 2f22d9a 98f27b5 312dfa3 818c8e3 98f27b5 a5b05e7 5409c3a 312dfa3 98f27b5 5409c3a a42e93f 98f27b5 |
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
import streamlit as st
import numpy as np
import cv2
from keras.models import load_model
from keras.preprocessing.image import img_to_array
from PIL import Image
# Page config
st.set_page_config(
page_title="π· Smart Face Mask Detection",
layout="wide",
page_icon="π·"
)
# Load model with caching
@st.cache_resource
def load_model_cached():
return load_model("project_face_mask_detection.keras")
model = load_model_cached()
# Haar Cascade for face detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
# Sidebar
with st.sidebar:
st.title("π§ About This App")
st.markdown("""
This app uses deep learning to detect whether a person is wearing a face mask.
- Upload or capture an image.
- Get instant feedback.
- Built with Streamlit & Keras.
""")
st.info("Tip: Use well-lit images with clear faces for best results.")
st.markdown("---")
st.caption("π Developed by Surendra β’ 2025")
# Optional resize function (only for uploads)
def resize_image(image, max_size=(400, 400)):
image = image.copy()
image.thumbnail(max_size) # maintains aspect ratio
return image
# Detection function
def detect_and_predict(image_input):
image_np = np.array(image_input.convert("RGB"))
gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
if len(faces) == 0:
return image_input, None, "β οΈ No face detected"
x, y, w, h = faces[0]
face_roi = image_np[y:y+h, x:x+w]
face_pil = Image.fromarray(face_roi).resize((200, 200))
img_array = img_to_array(face_pil) / 255.0
img_array = np.expand_dims(img_array, axis=0)
prediction = model.predict(img_array)[0][0]
confidence = (1 - prediction) if prediction < 0.5 else prediction
label = "β
Mask Detected" if prediction < 0.5 else "π« No Mask Detected"
# Draw results on original image
color = (0, 255, 0) if prediction < 0.5 else (255, 0, 0)
cv2.rectangle(image_np, (x, y), (x + w, y + h), color, 2)
cv2.putText(image_np, f"{label} ({confidence*100:.2f}%)", (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
return Image.fromarray(image_np), confidence, label
# App header
st.markdown("<h1 style='text-align: center;'>π· AI Face Mask Detection System</h1>", unsafe_allow_html=True)
st.markdown("<p style='text-align: center;'>Upload or capture an image to analyze mask presence.</p>", unsafe_allow_html=True)
# Input choice
input_choice = st.selectbox("Choose Input Method", ["π€ Upload Image", "π· Use Webcam"])
# === Upload Image ===
if input_choice == "π€ Upload Image":
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
if uploaded_file:
image_input = Image.open(uploaded_file)
resized_display = resize_image(image_input)
col1, col2 = st.columns(2)
with col1:
st.image(resized_display, caption="Uploaded Image")
with st.spinner("Analyzing with AI model..."):
result_img, confidence, label = detect_and_predict(image_input)
with col2:
st.image(result_img, caption="Detection Output")
if confidence is not None:
st.metric("Confidence Score", f"{confidence*100:.2f}%")
# st.success(label) if "Mask" in label else st.error(label)
if "Mask" in label:
st.success(label)
else:
st.error(label)
else:
st.warning(label)
# === Webcam Input ===
elif input_choice == "π· Use Webcam":
col1, col2 = st.columns([1, 3])
with col1:
camera_image = st.camera_input("Take a picture using webcam")
if camera_image:
image_input = Image.open(camera_image)
with st.spinner("Analyzing..."):
result_img, confidence, label = detect_and_predict(image_input)
with col2:
st.write("Resulted Image")
st.image(result_img, caption="Detection Output")
if confidence is not None:
st.metric("Confidence Score", f"{confidence*100:.2f}%")
# st.success(label) if "Mask" in label else st.error(label)
if "Mask" in label:
st.success(label)
else:
st.error(label)
else:
st.warning(label)
|