Update app.py
#1
by
saramneena
- opened
app.py
CHANGED
|
@@ -5,20 +5,24 @@ from keras.models import load_model
|
|
| 5 |
from keras.preprocessing.image import img_to_array
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
-
# Page
|
| 9 |
-
st.set_page_config(
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@st.cache_resource
|
| 13 |
def load_model_cached():
|
| 14 |
-
return load_model("
|
| 15 |
|
| 16 |
model = load_model_cached()
|
| 17 |
|
| 18 |
-
#
|
| 19 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
| 20 |
|
| 21 |
-
# Detection
|
| 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)
|
|
@@ -44,64 +48,77 @@ def detect_and_predict(image_input):
|
|
| 44 |
|
| 45 |
return Image.fromarray(image_np), confidence, label
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
st.markdown("<h2>π· Smart Face Mask Detection App</h2>", unsafe_allow_html=True)
|
| 49 |
st.markdown("""
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
with tab1:
|
| 61 |
-
st.
|
| 62 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 63 |
if uploaded_file:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
result_img, confidence, label = detect_and_predict(image_input)
|
| 70 |
-
|
| 71 |
-
st.image(result_img, caption="Detection Result", width=300)
|
| 72 |
-
if confidence is not None:
|
| 73 |
-
st.metric("Confidence", f"{confidence*100:.2f}%")
|
| 74 |
-
if "Mask" in label:
|
| 75 |
-
st.success(label)
|
| 76 |
-
else:
|
| 77 |
-
st.error(label)
|
| 78 |
-
else:
|
| 79 |
-
st.warning(label)
|
| 80 |
|
| 81 |
-
|
| 82 |
-
st.error(f"β Error: {str(e)}")
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
with tab2:
|
| 86 |
-
st.
|
| 87 |
-
camera_image = st.camera_input("Take a
|
| 88 |
if camera_image:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
result_img, confidence, label = detect_and_predict(image_input)
|
| 95 |
-
|
| 96 |
-
st.image(result_img, caption="Detection Result", width=300)
|
| 97 |
-
if confidence is not None:
|
| 98 |
-
st.metric("Confidence", f"{confidence*100:.2f}%")
|
| 99 |
-
if "Mask" in label:
|
| 100 |
-
st.success(label)
|
| 101 |
-
else:
|
| 102 |
-
st.error(label)
|
| 103 |
-
else:
|
| 104 |
-
st.warning(label)
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from keras.preprocessing.image import img_to_array
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
+
# π Page Config
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="Smart Face Mask Scanner π·",
|
| 11 |
+
layout="centered",
|
| 12 |
+
page_icon="π·"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# π§ Load model
|
| 16 |
@st.cache_resource
|
| 17 |
def load_model_cached():
|
| 18 |
+
return load_model("Face_Detector.keras", compile=False)
|
| 19 |
|
| 20 |
model = load_model_cached()
|
| 21 |
|
| 22 |
+
# π Haar Cascade for face detection
|
| 23 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
| 24 |
|
| 25 |
+
# π§ͺ Detection Function
|
| 26 |
def detect_and_predict(image_input):
|
| 27 |
image_np = np.array(image_input.convert("RGB"))
|
| 28 |
gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
|
|
|
|
| 48 |
|
| 49 |
return Image.fromarray(image_np), confidence, label
|
| 50 |
|
| 51 |
+
# π¨ Custom Styles
|
|
|
|
| 52 |
st.markdown("""
|
| 53 |
+
<style>
|
| 54 |
+
.main {
|
| 55 |
+
background-color: #f0f4f8;
|
| 56 |
+
padding: 1rem;
|
| 57 |
+
border-radius: 15px;
|
| 58 |
+
}
|
| 59 |
+
h2 {
|
| 60 |
+
text-align: center;
|
| 61 |
+
color: #2c3e50;
|
| 62 |
+
}
|
| 63 |
+
.stTabs [data-baseweb="tab"] {
|
| 64 |
+
background-color: #e3f2fd;
|
| 65 |
+
border-radius: 10px;
|
| 66 |
+
padding: 10px;
|
| 67 |
+
}
|
| 68 |
+
.stTabs [aria-selected="true"] {
|
| 69 |
+
background-color: #1976d2;
|
| 70 |
+
color: white;
|
| 71 |
+
}
|
| 72 |
+
</style>
|
| 73 |
+
""", unsafe_allow_html=True)
|
| 74 |
+
|
| 75 |
+
# π App Header
|
| 76 |
+
st.markdown("<h2>π‘οΈ Smart Face Mask Scanner</h2>", unsafe_allow_html=True)
|
| 77 |
+
st.markdown("<p style='text-align:center;'>Upload an image or use your webcam to check if a person is wearing a face mask.</p>", unsafe_allow_html=True)
|
| 78 |
+
|
| 79 |
+
# π Tabs
|
| 80 |
+
tab1, tab2 = st.tabs(["π€ Upload Image", "π· Use Webcam"])
|
| 81 |
+
|
| 82 |
+
# --- Upload Image Tab ---
|
| 83 |
with tab1:
|
| 84 |
+
st.subheader("π€ Upload a photo")
|
| 85 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 86 |
if uploaded_file:
|
| 87 |
+
image_input = Image.open(uploaded_file)
|
| 88 |
+
st.image(image_input, caption="π· Uploaded Image", use_column_width=True)
|
| 89 |
+
|
| 90 |
+
with st.spinner("π Analyzing..."):
|
| 91 |
+
result_img, confidence, label = detect_and_predict(image_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
st.image(result_img, caption="π Detection Result", use_column_width=True)
|
|
|
|
| 94 |
|
| 95 |
+
if confidence is not None:
|
| 96 |
+
st.metric("π§ Confidence", f"{confidence*100:.2f}%")
|
| 97 |
+
if "Mask" in label:
|
| 98 |
+
st.success(label)
|
| 99 |
+
else:
|
| 100 |
+
st.error(label)
|
| 101 |
+
else:
|
| 102 |
+
st.warning(label)
|
| 103 |
+
|
| 104 |
+
# --- Webcam Tab ---
|
| 105 |
with tab2:
|
| 106 |
+
st.subheader("π· Use your cam")
|
| 107 |
+
camera_image = st.camera_input("Take a snapshot")
|
| 108 |
if camera_image:
|
| 109 |
+
image_input = Image.open(camera_image)
|
| 110 |
+
st.image(image_input, caption="πΈ Captured Image", use_column_width=True)
|
| 111 |
+
|
| 112 |
+
with st.spinner("π Analyzing..."):
|
| 113 |
+
result_img, confidence, label = detect_and_predict(image_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
st.image(result_img, caption="π Detection Result", use_column_width=True)
|
| 116 |
+
|
| 117 |
+
if confidence is not None:
|
| 118 |
+
st.metric("π§ Confidence", f"{confidence*100:.2f}%")
|
| 119 |
+
if "Mask" in label:
|
| 120 |
+
st.success(label)
|
| 121 |
+
else:
|
| 122 |
+
st.error(label)
|
| 123 |
+
else:
|
| 124 |
+
st.warning(label)
|