File size: 3,695 Bytes
4f1e8fa a42cd85 318de6f a42cd85 318de6f a42cd85 aaa6e33 a42cd85 aaa6e33 318de6f aaa6e33 a42cd85 318de6f a42cd85 aaa6e33 318de6f a42cd85 aaa6e33 318de6f 4f1e8fa 318de6f aaa6e33 318de6f aaa6e33 318de6f aaa6e33 80a0030 318de6f aaa6e33 318de6f aaa6e33 318de6f aaa6e33 318de6f aaa6e33 318de6f aaa6e33 318de6f aaa6e33 318de6f aaa6e33 318de6f aaa6e33 318de6f aaa6e33 318de6f aaa6e33 |
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 |
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 settings
st.set_page_config(page_title="Smart Face Mask Scanner", layout="centered")
# Load model
@st.cache_resource
def load_model_cached():
return load_model("Face Detector.keras")
model = load_model_cached()
# Load Haar Cascade
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
# Detection and Prediction
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.5, color, 2)
return Image.fromarray(image_np), confidence, label
# App Header
st.markdown("<h2>π· Smart Face Mask Detection App</h2>", unsafe_allow_html=True)
st.markdown("""
This app allows you to **instantly check if a person is wearing a mask** by uploading an image or using your webcam.
""")
# Tabs with emphasized titles
tab1, tab2 = st.tabs([
"πΌοΈ **:blue[Upload Image]**",
"π· **:green[Use Webcam]**"
])
# Upload Image Tab
with tab1:
st.markdown("#### :blue[Upload a photo to detect mask status]")
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", width=300)
with st.spinner("Analyzing..."):
result_img, confidence, label = detect_and_predict(image_input)
st.image(result_img, caption="Detection Result", width=300)
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)}")
# Webcam Tab
with tab2:
st.markdown("#### :green[Take a picture using webcam to detect mask]")
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", width=300)
with st.spinner("Analyzing..."):
result_img, confidence, label = detect_and_predict(image_input)
st.image(result_img, caption="Detection Result", width=300)
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)}")
|