Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,100 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
from keras.models import load_model
|
| 4 |
-
from keras.preprocessing import
|
| 5 |
from PIL import Image
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
st.
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
img = Image.open(uploaded_file)
|
| 20 |
-
st.image(img, caption="Uploaded Image", width=200)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
img = img / 255.0
|
| 27 |
|
| 28 |
-
|
| 29 |
-
prediction = model.predict(img)[0][0]
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
from keras.models import load_model
|
| 5 |
+
from keras.preprocessing.image import img_to_array
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
|
| 8 |
+
# Set page config
|
| 9 |
+
st.set_page_config(page_title="Face Mask Detection", layout="centered")
|
| 10 |
|
| 11 |
+
# Load model once
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_model_cached():
|
| 14 |
+
return load_model("Face Detection.keras") # Make sure this is trained on cropped face images
|
| 15 |
|
| 16 |
+
model = load_model_cached()
|
|
|
|
| 17 |
|
| 18 |
+
# Load Haar Cascade for face detection
|
| 19 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Function to detect face and predict
|
| 22 |
+
def detect_and_predict(image_input):
|
| 23 |
+
image_np = np.array(image_input.convert("RGB"))
|
| 24 |
+
gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
|
|
|
|
| 25 |
|
| 26 |
+
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
|
|
|
|
| 27 |
|
| 28 |
+
if len(faces) == 0:
|
| 29 |
+
return image_input, None, "No face detected"
|
| 30 |
+
|
| 31 |
+
x, y, w, h = faces[0] # Just take the first detected face
|
| 32 |
+
face_roi = image_np[y:y+h, x:x+w]
|
| 33 |
+
face_pil = Image.fromarray(face_roi).resize((200, 200))
|
| 34 |
+
img_array = img_to_array(face_pil) / 255.0
|
| 35 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 36 |
+
|
| 37 |
+
prediction = model.predict(img_array)[0][0]
|
| 38 |
+
confidence = (1 - prediction) if prediction < 0.5 else prediction
|
| 39 |
+
label = "β
Mask Detected" if prediction < 0.5 else "π« No Mask Detected"
|
| 40 |
+
|
| 41 |
+
# Draw rectangle and label
|
| 42 |
+
color = (0, 255, 0) if prediction < 0.5 else (255, 0, 0)
|
| 43 |
+
cv2.rectangle(image_np, (x, y), (x + w, y + h), color, 2)
|
| 44 |
+
cv2.putText(image_np, f"{label} ({confidence*100:.2f}%)",(x, y - 10),
|
| 45 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.4, color, 2)
|
| 46 |
+
|
| 47 |
+
return Image.fromarray(image_np), confidence, label
|
| 48 |
+
|
| 49 |
+
# App UI
|
| 50 |
+
st.title("π· Smart Face Mask Detection App")
|
| 51 |
+
st.markdown("Upload a face image or use your webcam to check if a mask is being worn.")
|
| 52 |
+
|
| 53 |
+
# Tabs
|
| 54 |
+
tab1, tab2 = st.tabs(["π€ Upload Image", "π· Use Webcam"])
|
| 55 |
+
|
| 56 |
+
with tab1:
|
| 57 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 58 |
+
if uploaded_file:
|
| 59 |
+
try:
|
| 60 |
+
image_input = Image.open(uploaded_file)
|
| 61 |
+
st.image(image_input, caption="Uploaded Image", use_container_width=True)
|
| 62 |
+
|
| 63 |
+
with st.spinner("Analyzing..."):
|
| 64 |
+
result_img, confidence, label = detect_and_predict(image_input)
|
| 65 |
+
|
| 66 |
+
st.image(result_img, caption="Detection Result", use_container_width=True)
|
| 67 |
+
if confidence is not None:
|
| 68 |
+
st.metric("Confidence", f"{confidence*100:.2f}%")
|
| 69 |
+
if "Mask" in label:
|
| 70 |
+
st.success(label)
|
| 71 |
+
else:
|
| 72 |
+
st.error(label)
|
| 73 |
+
else:
|
| 74 |
+
st.warning(label)
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
st.error(f"β Error: {str(e)}")
|
| 78 |
+
|
| 79 |
+
with tab2:
|
| 80 |
+
camera_image = st.camera_input("Take a picture")
|
| 81 |
+
if camera_image:
|
| 82 |
+
try:
|
| 83 |
+
image_input = Image.open(camera_image)
|
| 84 |
+
st.image(image_input, caption="Webcam Snapshot", use_container_width=True)
|
| 85 |
+
|
| 86 |
+
with st.spinner("Analyzing..."):
|
| 87 |
+
result_img, confidence, label = detect_and_predict(image_input)
|
| 88 |
+
|
| 89 |
+
st.image(result_img, caption="Detection Result", use_container_width=True)
|
| 90 |
+
if confidence is not None:
|
| 91 |
+
st.metric("Confidence", f"{confidence*100:.2f}%")
|
| 92 |
+
if "Mask" in label:
|
| 93 |
+
st.success(label)
|
| 94 |
+
else:
|
| 95 |
+
st.error(label)
|
| 96 |
+
else:
|
| 97 |
+
st.warning(label)
|
| 98 |
+
|
| 99 |
+
except Exception as e:
|
| 100 |
+
st.error(f"β Error: {str(e)}")
|