Surendradjh commited on
Commit
4d4558a
Β·
verified Β·
1 Parent(s): 50cbdd4

Upload 4 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ project_face_mask_detection.keras filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("project_face_mask_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("😷 Intelligent Mask Detection Platform")
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)}")
101
+
102
+
103
+ import streamlit as st
104
+ import numpy as np
105
+ import cv2
106
+ from keras.models import load_model
107
+ from keras.preprocessing.image import img_to_array
108
+ from PIL import Image
109
+
110
+ # Page config with improved UI layout
111
+ st.set_page_config(
112
+ page_title="😷 Smart Face Mask Detection",
113
+ layout="wide",
114
+ page_icon="😷"
115
+ )
116
+
117
+ # Load the model with caching
118
+ @st.cache_resource
119
+ def load_model_cached():
120
+ return load_model("project_face_mask_detection.keras")
121
+
122
+ model = load_model_cached()
123
+
124
+ # Haar Cascade for face detection
125
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
126
+
127
+ # Sidebar for app description and tips
128
+ with st.sidebar:
129
+ st.title("🧠 About This App")
130
+ st.markdown("""
131
+ This app uses deep learning to detect whether a person is wearing a face mask.
132
+
133
+ - Upload or capture an image.
134
+ - Get instant feedback.
135
+ - Built with Streamlit & Keras.
136
+ """)
137
+ st.info("Tip: Use well-lit images with clear faces for best results.")
138
+ st.markdown("---")
139
+ st.caption("πŸ“ Developed by YourName β€’ 2025")
140
+
141
+ # Core detection function
142
+ def detect_and_predict(image_input):
143
+ image_np = np.array(image_input.convert("RGB"))
144
+ gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
145
+ faces = face_cascade.detectMultiScale(gray, 1.1, 4)
146
+
147
+ if len(faces) == 0:
148
+ return image_input, None, "⚠️ No face detected"
149
+
150
+ x, y, w, h = faces[0]
151
+ face_roi = image_np[y:y+h, x:x+w]
152
+ face_pil = Image.fromarray(face_roi).resize((200, 200))
153
+ img_array = img_to_array(face_pil) / 255.0
154
+ img_array = np.expand_dims(img_array, axis=0)
155
+
156
+ prediction = model.predict(img_array)[0][0]
157
+ confidence = (1 - prediction) if prediction < 0.5 else prediction
158
+ label = "βœ… Mask Detected" if prediction < 0.5 else "🚫 No Mask Detected"
159
+
160
+ # Drawing results
161
+ color = (0, 255, 0) if prediction < 0.5 else (255, 0, 0)
162
+ cv2.rectangle(image_np, (x, y), (x + w, y + h), color, 2)
163
+ cv2.putText(image_np, f"{label} ({confidence*100:.2f}%)", (x, y - 10),
164
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
165
+
166
+ return Image.fromarray(image_np), confidence, label
167
+
168
+ # App Title
169
+ st.markdown("<h1 style='text-align: center;'>😷 AI Face Mask Detection System</h1>", unsafe_allow_html=True)
170
+ st.markdown("<p style='text-align: center;'>Upload or capture an image to analyze mask presence.</p>", unsafe_allow_html=True)
171
+
172
+ # Tabs for Upload and Webcam
173
+ tab1, tab2 = st.tabs(["πŸ“€ Upload Image", "πŸ“· Use Webcam"])
174
+
175
+ # Upload Image Tab
176
+ with tab1:
177
+ uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
178
+ if uploaded_file:
179
+ image_input = Image.open(uploaded_file)
180
+ st.image(image_input, caption="Uploaded Image", use_container_width=True)
181
+
182
+ with st.spinner("Analyzing with AI model..."):
183
+ result_img, confidence, label = detect_and_predict(image_input)
184
+
185
+ col1, col2 = st.columns(2)
186
+ with col1:
187
+ st.image(result_img, caption="Detection Output", use_container_width=True)
188
+ with col2:
189
+ if confidence is not None:
190
+ st.metric("Confidence Score", f"{confidence*100:.2f}%")
191
+ if "Mask" in label:
192
+ st.success(label)
193
+ else:
194
+ st.error(label)
195
+ else:
196
+ st.warning(label)
197
+
198
+ # Webcam Tab
199
+ with tab2:
200
+ camera_image = st.camera_input("Take a picture using webcam")
201
+ if camera_image:
202
+ image_input = Image.open(camera_image)
203
+ st.image(image_input, caption="Webcam Snapshot", use_container_width=True)
204
+
205
+ with st.spinner("Analyzing..."):
206
+ result_img, confidence, label = detect_and_predict(image_input)
207
+
208
+ col1, col2 = st.columns(2)
209
+ with col1:
210
+ st.image(result_img, caption="Detection Output", use_container_width=True)
211
+ with col2:
212
+ if confidence is not None:
213
+ st.metric("Confidence Score", f"{confidence*100:.2f}%")
214
+ if "Mask" in label:
215
+ st.success(label)
216
+ else:
217
+ st.error(label)
218
+ else:
219
+ st.warning(label)
haarcascade_frontalface_default.xml ADDED
The diff for this file is too large to render. See raw diff
 
project_face_mask_detection.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7d8ad036045b1f4ae1e0e7e7cd334bee94aad98743caf2d9d705b77681257e17
3
+ size 1510661
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ opencv-python-headless
2
+ streamlit
3
+ tensorflow
4
+ keras
5
+ Pillow
6
+ numpy