import streamlit as st from PIL import Image import numpy as np import cv2 import os import zipfile from tensorflow.keras.applications.mobilenet_v2 import preprocess_input from tensorflow.keras.preprocessing.image import img_to_array from tensorflow.keras.models import load_model import urllib.request # --- Download files if not already present --- def download_file(url, dest): if not os.path.exists(dest): urllib.request.urlretrieve(url, dest) # Replace with actual file URLs download_file("https://your-server.com/face_detector.zip", "face_detector.zip") download_file("https://your-server.com/css.zip", "css.zip") download_file("https://your-server.com/images.zip", "images.zip") download_file("https://your-server.com/mask_detector.h5", "mask_detector.h5") # --- Extract zip files only once --- def extract_zip_once(zip_path, extract_to): if not os.path.exists(extract_to): with zipfile.ZipFile(zip_path, 'r') as zip_ref: zip_ref.extractall(extract_to) # Extract required folders extract_zip_once("face_detector.zip", "face_detector") extract_zip_once("css.zip", "css") extract_zip_once("images.zip", "images") # --- Streamlit Config --- st.set_page_config(page_title='Face Mask Detector', page_icon='😷', layout='centered', initial_sidebar_state='expanded') # --- Local CSS --- def local_css(file_name): with open(file_name) as f: st.markdown(f'', unsafe_allow_html=True) # --- Mask Detection on Image --- def mask_image(): global RGB_img # Load face detector model prototxtPath = os.path.join("face_detector", "deploy.prototxt") weightsPath = os.path.join("face_detector", "res10_300x300_ssd_iter_140000.caffemodel") net = cv2.dnn.readNet(prototxtPath, weightsPath) # Load face mask detector model model = load_model("mask_detector.h5") # Read image image_path = os.path.join("images", "out.jpg") image = cv2.imread(image_path) (h, w) = image.shape[:2] # Detect faces blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0)) net.setInput(blob) detections = net.forward() for i in range(0, detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > 0.5: box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") (startX, startY) = (max(0, startX), max(0, startY)) (endX, endY) = (min(w - 1, endX), min(h - 1, endY)) face = image[startY:endY, startX:endX] face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) face = cv2.resize(face, (224, 224)) face = img_to_array(face) face = preprocess_input(face) face = np.expand_dims(face, axis=0) (mask, withoutMask) = model.predict(face)[0] label = "Mask" if mask > withoutMask else "No Mask" color = (0, 255, 0) if label == "Mask" else (0, 0, 255) label = f"{label}: {max(mask, withoutMask) * 100:.2f}%" cv2.putText(image, label, (startX, startY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2) cv2.rectangle(image, (startX, startY), (endX, endY), color, 2) RGB_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # --- Streamlit App --- def mask_detection(): local_css(os.path.join("css", "styles.css")) st.markdown('

😷 Face Mask Detection

', unsafe_allow_html=True) st.sidebar.markdown("# Mask Detection on?") choice = st.sidebar.selectbox("Choose among the given options:", ["Image", "Webcam"]) if choice == 'Image': st.markdown('

Detection on Image

', unsafe_allow_html=True) st.markdown("### Upload your image here ⬇") image_file = st.file_uploader("", type=['jpg', 'jpeg', 'png']) if image_file is not None: img = Image.open(image_file) save_path = os.path.join("images", "out.jpg") img.save(save_path) st.image(image_file, caption='', use_column_width=True) st.markdown('

Image uploaded successfully!

', unsafe_allow_html=True) if st.button('Process'): mask_image() st.image(RGB_img, use_column_width=True) elif choice == 'Webcam': st.markdown('

Detection on Webcam

', unsafe_allow_html=True) st.markdown('

This feature will be available soon!

', unsafe_allow_html=True) # Run the app mask_detection()