Files changed (1) hide show
  1. app.py +76 -59
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 settings
9
- st.set_page_config(page_title="Smart Face Mask Scanner", layout="centered")
10
-
11
- # Load model
 
 
 
 
12
  @st.cache_resource
13
  def load_model_cached():
14
- return load_model("Face Detector.keras")
15
 
16
  model = load_model_cached()
17
 
18
- # Load Haar Cascade
19
  face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
20
 
21
- # Detection and Prediction
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
- # App Header
48
- st.markdown("<h2>😷 Smart Face Mask Detection App</h2>", unsafe_allow_html=True)
49
  st.markdown("""
50
- This app allows you to **instantly check if a person is wearing a mask** by uploading an image or using your webcam.
51
- """)
52
-
53
- # Tabs with emphasized titles
54
- tab1, tab2 = st.tabs([
55
- "πŸ–ΌοΈ **:blue[Upload Image]**",
56
- "πŸ“· **:green[Use Webcam]**"
57
- ])
58
-
59
- # Upload Image Tab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  with tab1:
61
- st.markdown("#### :blue[Upload a photo to detect mask status]")
62
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
63
  if uploaded_file:
64
- try:
65
- image_input = Image.open(uploaded_file)
66
- st.image(image_input, caption="Uploaded Image", width=300)
67
-
68
- with st.spinner("Analyzing..."):
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
- except Exception as e:
82
- st.error(f"❌ Error: {str(e)}")
83
 
84
- # Webcam Tab
 
 
 
 
 
 
 
 
 
85
  with tab2:
86
- st.markdown("#### :green[Take a picture using webcam to detect mask]")
87
- camera_image = st.camera_input("Take a picture")
88
  if camera_image:
89
- try:
90
- image_input = Image.open(camera_image)
91
- st.image(image_input, caption="Webcam Snapshot", width=300)
92
-
93
- with st.spinner("Analyzing..."):
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
- except Exception as e:
107
- st.error(f"❌ Error: {str(e)}")
 
 
 
 
 
 
 
 
 
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)