| 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 |
| import base64 |
|
|
| |
| st.set_page_config(page_title="Face Mask Detection", layout="centered") |
|
|
| |
| def set_background(image_path): |
| with open(image_path, "rb") as img_file: |
| encoded = base64.b64encode(img_file.read()).decode() |
| st.markdown( |
| f""" |
| <style> |
| .stApp {{ |
| background-image: url("data:image/jpg;base64,{encoded}"); |
| background-size: cover; |
| background-position: center; |
| background-repeat: no-repeat; |
| }} |
| </style> |
| """, |
| unsafe_allow_html=True |
| ) |
|
|
| |
| set_background("IMAGE_1.jpg") |
|
|
| |
| @st.cache_resource |
| def load_model_cached(): |
| return load_model("FaceDetection.keras") |
|
|
| model = load_model_cached() |
|
|
| |
| face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") |
|
|
| |
| 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" |
|
|
| |
| 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.4, color, 2) |
|
|
| return Image.fromarray(image_np), confidence, label |
|
|
| |
| st.title("Face Mask Detection") |
| st.markdown("Upload a face image or use your webcam to check if a mask is being worn.") |
|
|
| |
| tab1, tab2 = st.tabs([" Upload Image", "Use Webcam"]) |
|
|
| with tab1: |
| uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) |
| if uploaded_file: |
| try: |
| image_input = Image.open(uploaded_file) |
| st.image(image_input, caption="Uploaded Image", use_container_width=True) |
|
|
| with st.spinner("Analyzing..."): |
| result_img, confidence, label = detect_and_predict(image_input) |
|
|
| st.image(result_img, caption="Detection Result", use_container_width=True) |
| if confidence is not None: |
| st.metric("Confidence", f"{confidence*100:.2f}%") |
| if "Mask" in label: |
| st.success(label) |
| else: |
| st.error(label) |
| else: |
| st.warning(label) |
|
|
| except Exception as e: |
| st.error(f"β Error: {str(e)}") |
|
|
| with tab2: |
| camera_image = st.camera_input("Take a picture") |
| if camera_image: |
| try: |
| image_input = Image.open(camera_image) |
| st.image(image_input, caption="Webcam Snapshot", use_container_width=True) |
|
|
| with st.spinner("Analyzing..."): |
| result_img, confidence, label = detect_and_predict(image_input) |
|
|
| st.image(result_img, caption="Detection Result", use_container_width=True) |
| if confidence is not None: |
| st.metric("Confidence", f"{confidence*100:.2f}%") |
| if "Mask" in label: |
| st.success(label) |
| else: |
| st.error(label) |
| else: |
| st.warning(label) |
|
|
| except Exception as e: |
| st.error(f"β Error: {str(e)}") |
|
|