import streamlit as st import cv2 import numpy as np from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing.image import img_to_array from tensorflow.keras.applications.mobilenet_v2 import preprocess_input # Function to detect and predict mask def detect_and_predict_mask(frame, faceNet, maskNet, confidence_threshold): (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), (104.0, 177.0, 123.0)) faceNet.setInput(blob) detections = faceNet.forward() faces = [] locs = [] preds = [] for i in range(detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > confidence_threshold: 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 = frame[startY:endY, startX:endX] if face.shape[0] > 0 and face.shape[1] > 0: face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) face = cv2.resize(face, (224, 224)) face = img_to_array(face) face = preprocess_input(face) faces.append(face) locs.append((startX, startY, endX, endY)) if len(faces) > 0: faces = np.array(faces, dtype="float32") preds = maskNet.predict(faces, batch_size=32) return (locs, preds) # Load models @st.cache_resource def load_models(): prototxtPath = "face_detector/deploy.prototxt" weightsPath = "face_detector/res10_300x300_ssd_iter_140000.caffemodel" faceNet = cv2.dnn.readNet(prototxtPath, weightsPath) maskNet = load_model("mask_detector.model") return faceNet, maskNet faceNet, maskNet = load_models() # Streamlit UI st.title("Real-Time Face Mask Detection with TensorFlow") st.text("Turn on your webcam to detect masks in real-time.") run = st.button("Start Camera") # Create a Streamlit "Stop" button outside the loop to avoid duplicate key issues stop_button = st.button("Stop") if run: confidence_threshold = st.slider("Confidence Threshold", 0.1, 1.0, 0.5, 0.1) stframe = st.empty() cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() if not ret: st.error("Failed to access camera.") break frame = cv2.resize(frame, (800, 600)) locs, preds = detect_and_predict_mask(frame, faceNet, maskNet, confidence_threshold) for (box, pred) in zip(locs, preds): (startX, startY, endX, endY) = box (mask, withoutMask) = pred label = "Mask" if mask > withoutMask else "No Mask" color = (0, 255, 0) if label == "Mask" else (0, 0, 255) text = f"{label}: {'Allowed' if label == 'Mask' else 'Not Allowed'}" cv2.putText(frame, text, (startX, startY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2) cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2) stframe.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), channels="RGB") # Check if the "Stop" button was clicked if stop_button: break cap.release() cv2.destroyAllWindows()