Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import numpy as np
|
|
| 4 |
from PIL import Image
|
| 5 |
import time
|
| 6 |
import cvlib as cv
|
|
|
|
| 7 |
|
| 8 |
# Set page config
|
| 9 |
st.set_page_config(page_title="Face Mask Detection", layout="wide")
|
|
@@ -18,7 +19,7 @@ if "camera_key" not in st.session_state:
|
|
| 18 |
st.markdown("<h1 style='text-align: center;'>😷 Face Mask Detection</h1>", unsafe_allow_html=True)
|
| 19 |
input_method = st.selectbox("Choose Input Method", ["Camera Capture", "Upload Image"])
|
| 20 |
|
| 21 |
-
# Dummy
|
| 22 |
def classify_face_dummy(face_img):
|
| 23 |
mean = np.mean(face_img)
|
| 24 |
if mean % 2 < 1:
|
|
@@ -27,12 +28,22 @@ def classify_face_dummy(face_img):
|
|
| 27 |
return "No Mask", 0.60
|
| 28 |
|
| 29 |
def detect_and_classify_faces(img_np):
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
results = []
|
|
|
|
|
|
|
|
|
|
| 32 |
for i, face in enumerate(faces):
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
if face_crop.size == 0:
|
| 38 |
continue
|
|
@@ -69,11 +80,12 @@ if input_method == "Camera Capture":
|
|
| 69 |
if st.session_state.image_captured:
|
| 70 |
with col2:
|
| 71 |
st.markdown("### 🧠 Detection Result")
|
| 72 |
-
image = Image.open(st.session_state.image_captured)
|
| 73 |
img_np = np.array(image)
|
| 74 |
|
| 75 |
results = detect_and_classify_faces(img_np)
|
| 76 |
|
|
|
|
| 77 |
for res in results:
|
| 78 |
x, y, w, h = res["box"]
|
| 79 |
label = res["label"]
|
|
@@ -84,7 +96,7 @@ if input_method == "Camera Capture":
|
|
| 84 |
cv2.putText(img_np, f"{label} ({conf*100:.2f}%)", (x, y - 10),
|
| 85 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
|
| 86 |
|
| 87 |
-
st.image(img_np, caption="Result Image", channels="
|
| 88 |
|
| 89 |
for i, face in enumerate(results, 1):
|
| 90 |
label_color = "green" if face["label"] == "Mask" else "red"
|
|
@@ -103,13 +115,13 @@ elif input_method == "Upload Image":
|
|
| 103 |
st.markdown("### 📁 Upload Image")
|
| 104 |
uploaded_img = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 105 |
if uploaded_img:
|
| 106 |
-
image = Image.open(uploaded_img)
|
| 107 |
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 108 |
|
| 109 |
if uploaded_img:
|
| 110 |
with col2:
|
| 111 |
st.markdown("### 🧠 Detection Result")
|
| 112 |
-
img_np = np.array(
|
| 113 |
results = detect_and_classify_faces(img_np)
|
| 114 |
|
| 115 |
for res in results:
|
|
@@ -122,7 +134,7 @@ elif input_method == "Upload Image":
|
|
| 122 |
cv2.putText(img_np, f"{label} ({conf*100:.2f}%)", (x, y - 10),
|
| 123 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
|
| 124 |
|
| 125 |
-
st.image(img_np, caption="Result Image", channels="
|
| 126 |
|
| 127 |
for i, face in enumerate(results, 1):
|
| 128 |
label_color = "green" if face["label"] == "Mask" else "red"
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import time
|
| 6 |
import cvlib as cv
|
| 7 |
+
from cvlib.object_detection import draw_bbox
|
| 8 |
|
| 9 |
# Set page config
|
| 10 |
st.set_page_config(page_title="Face Mask Detection", layout="wide")
|
|
|
|
| 19 |
st.markdown("<h1 style='text-align: center;'>😷 Face Mask Detection</h1>", unsafe_allow_html=True)
|
| 20 |
input_method = st.selectbox("Choose Input Method", ["Camera Capture", "Upload Image"])
|
| 21 |
|
| 22 |
+
# Dummy classifier (replace this with actual ML model if needed)
|
| 23 |
def classify_face_dummy(face_img):
|
| 24 |
mean = np.mean(face_img)
|
| 25 |
if mean % 2 < 1:
|
|
|
|
| 28 |
return "No Mask", 0.60
|
| 29 |
|
| 30 |
def detect_and_classify_faces(img_np):
|
| 31 |
+
# Ensure color format is BGR
|
| 32 |
+
if img_np.shape[2] == 3: # 3 channels
|
| 33 |
+
img_bgr = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
|
| 34 |
+
else:
|
| 35 |
+
img_bgr = img_np
|
| 36 |
+
|
| 37 |
+
faces, confidences = cv.detect_face(img_bgr)
|
| 38 |
results = []
|
| 39 |
+
|
| 40 |
+
height, width = img_np.shape[:2]
|
| 41 |
+
|
| 42 |
for i, face in enumerate(faces):
|
| 43 |
+
startX, startY = max(face[0], 0), max(face[1], 0)
|
| 44 |
+
endX, endY = min(face[2], width - 1), min(face[3], height - 1)
|
| 45 |
+
|
| 46 |
+
face_crop = img_bgr[startY:endY, startX:endX]
|
| 47 |
|
| 48 |
if face_crop.size == 0:
|
| 49 |
continue
|
|
|
|
| 80 |
if st.session_state.image_captured:
|
| 81 |
with col2:
|
| 82 |
st.markdown("### 🧠 Detection Result")
|
| 83 |
+
image = Image.open(st.session_state.image_captured).convert("RGB")
|
| 84 |
img_np = np.array(image)
|
| 85 |
|
| 86 |
results = detect_and_classify_faces(img_np)
|
| 87 |
|
| 88 |
+
# Annotate image
|
| 89 |
for res in results:
|
| 90 |
x, y, w, h = res["box"]
|
| 91 |
label = res["label"]
|
|
|
|
| 96 |
cv2.putText(img_np, f"{label} ({conf*100:.2f}%)", (x, y - 10),
|
| 97 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
|
| 98 |
|
| 99 |
+
st.image(img_np, caption="Result Image", channels="RGB", use_container_width=True)
|
| 100 |
|
| 101 |
for i, face in enumerate(results, 1):
|
| 102 |
label_color = "green" if face["label"] == "Mask" else "red"
|
|
|
|
| 115 |
st.markdown("### 📁 Upload Image")
|
| 116 |
uploaded_img = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 117 |
if uploaded_img:
|
| 118 |
+
image = Image.open(uploaded_img).convert("RGB")
|
| 119 |
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 120 |
|
| 121 |
if uploaded_img:
|
| 122 |
with col2:
|
| 123 |
st.markdown("### 🧠 Detection Result")
|
| 124 |
+
img_np = np.array(image)
|
| 125 |
results = detect_and_classify_faces(img_np)
|
| 126 |
|
| 127 |
for res in results:
|
|
|
|
| 134 |
cv2.putText(img_np, f"{label} ({conf*100:.2f}%)", (x, y - 10),
|
| 135 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
|
| 136 |
|
| 137 |
+
st.image(img_np, caption="Result Image", channels="RGB", use_container_width=True)
|
| 138 |
|
| 139 |
for i, face in enumerate(results, 1):
|
| 140 |
label_color = "green" if face["label"] == "Mask" else "red"
|