Skip to content

Commit 347f4ad

Browse files
authored
Merge pull request #1630 from Niraj1608/mask-detection
[Feature]: Streamlit Deployment and GUI for Face Mask Detection #1617
2 parents 20cc9d3 + c1e23a5 commit 347f4ad

File tree

4 files changed

+168
-2
lines changed

4 files changed

+168
-2
lines changed

Detection Models/Face_Mask_Detection/Face_Mask_Detection.ipynb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,12 +1062,23 @@
10621062
"id": "dbfeb3a0-d0a7-4446-8ae4-559206d46034",
10631063
"metadata": {},
10641064
"outputs": [],
1065+
"source": [
1066+
"#save the model\n",
1067+
"model.save('mask_detection_model.keras')\n"
1068+
]
1069+
},
1070+
{
1071+
"cell_type": "code",
1072+
"execution_count": null,
1073+
"id": "a62eb7fc",
1074+
"metadata": {},
1075+
"outputs": [],
10651076
"source": []
10661077
}
10671078
],
10681079
"metadata": {
10691080
"kernelspec": {
1070-
"display_name": "Python 3 (ipykernel)",
1081+
"display_name": "Python 3",
10711082
"language": "python",
10721083
"name": "python3"
10731084
},
@@ -1081,7 +1092,7 @@
10811092
"name": "python",
10821093
"nbconvert_exporter": "python",
10831094
"pygments_lexer": "ipython3",
1084-
"version": "3.11.5"
1095+
"version": "3.12.1"
10851096
}
10861097
},
10871098
"nbformat": 4,
Binary file not shown.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import cv2
2+
import numpy as np
3+
import keras
4+
import tk as tk
5+
from PIL import ImageTk, Image
6+
import tkinter as tk
7+
from tkinter import filedialog
8+
import os
9+
10+
# Load the face mask detector model
11+
model =keras.models.load_model("mask_detection.keras")
12+
13+
# Initialize the GUI
14+
root = tk.Tk()
15+
root.title("Mask Detection")
16+
root.geometry("500x400")
17+
18+
# Function to classify the image
19+
def classify_image(image):
20+
# Preprocess the image
21+
image = image.resize((128, 128))
22+
image = np.array(image)
23+
image = image / 255.0
24+
input_image_reshaped = np.reshape(image, [1,128, 128, 3])
25+
# Make predictions
26+
predictions = model.predict(input_image_reshaped)
27+
input_pred_label = np.argmax(predictions)
28+
29+
if input_pred_label == 1:
30+
result = "With Mask"
31+
else:
32+
result = "Without Mask"
33+
34+
# Update the result label text
35+
result_label.configure(text="Prediction: " + result)
36+
37+
# Function to handle file upload
38+
def upload_image():
39+
file_path = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select Image File",
40+
filetypes=(("JPEG files", "*.jpg"), ("PNG files", "*.png")))
41+
if file_path:
42+
# Display the uploaded image in the GUI
43+
image = Image.open(file_path)
44+
image.thumbnail((300, 300))
45+
photo = ImageTk.PhotoImage(image)
46+
photo_label.configure(image=photo)
47+
photo_label.image = photo
48+
49+
classify_image(image)
50+
51+
# Function to handle capturing photo from webcam
52+
def capture_photo():
53+
video_capture = cv2.VideoCapture(0)
54+
_, frame = video_capture.read()
55+
video_capture.release()
56+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
57+
image = Image.fromarray(frame)
58+
classify_image(image)
59+
60+
# Display the captured photo in the GUI
61+
image.thumbnail((300, 300))
62+
photo = ImageTk.PhotoImage(image)
63+
photo_label.configure(image=photo)
64+
photo_label.image = photo
65+
66+
result_label = tk.Label(root, text="", font=("Helvetica", 16))
67+
result_label.pack(pady=10)
68+
69+
# Create the GUI components
70+
upload_button = tk.Button(root, text="Upload Image", command=upload_image)
71+
upload_button.pack(pady=10)
72+
73+
capture_button = tk.Button(root, text="Capture Photo", command=capture_photo)
74+
capture_button.pack(pady=10)
75+
76+
result_label = tk.Label(root, text="")
77+
result_label.pack(pady=10)
78+
79+
photo_label = tk.Label(root)
80+
photo_label.pack()
81+
82+
# Run the GUI main loop
83+
root.mainloop()
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import cv2
2+
import numpy as np
3+
import keras
4+
import streamlit as st
5+
import PIL.Image
6+
7+
model=keras.models.load_model('mask_detection.keras')
8+
9+
10+
11+
12+
13+
def main():
14+
st.title("Mask Detection App")
15+
st.write("Please choose an option")
16+
17+
option = st.selectbox("Select an option", ("Upload Image", "Capture Photo"))
18+
19+
if option == "Upload Image":
20+
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
21+
22+
if uploaded_file is not None:
23+
# Read and preprocess the image
24+
image = PIL.Image.open(uploaded_file)
25+
image = image.resize((128,128))
26+
image = np.array(image) # Convert to NumPy array
27+
image = image / 255.0 # Normalize the image
28+
input_image_reshaped = np.reshape(image, [1, 128, 128, 3])
29+
30+
# Make predictions
31+
predictions = model.predict(input_image_reshaped)
32+
input_pred_label = np.argmax(predictions)
33+
34+
# Display the result
35+
if input_pred_label == 1:
36+
st.markdown("<h3 style='text-align: center; '>Prediction: With Mask</h3>", unsafe_allow_html=True)
37+
else:
38+
st.markdown("<h3 style='text-align: center; '>Prediction: Without Mask</h3>", unsafe_allow_html=True)
39+
40+
# Display the uploaded image
41+
st.write("")
42+
st.write("**Uploaded Image**")
43+
st.image(image, use_column_width=True)
44+
45+
elif option == "Capture Photo":
46+
video_capture = cv2.VideoCapture(0)
47+
_, frame = video_capture.read()
48+
video_capture.release()
49+
50+
# Convert captured frame to RGB
51+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
52+
53+
# Resize and preprocess the image
54+
image = cv2.resize(frame, (64, 64))
55+
image = image / 255.0
56+
input_image_reshaped = np.reshape(image, [1, 64, 64, 3])
57+
58+
# Make predictions
59+
predictions = model.predict(input_image_reshaped)
60+
input_pred_label = np.argmax(predictions)
61+
62+
# Display the result
63+
if input_pred_label == 1:
64+
st.markdown("<h3 style='text-align: center; '>Prediction: With Mask</h3>", unsafe_allow_html=True)
65+
else:
66+
st.markdown("<h3 style='text-align: center; '>Prediction: Without Mask</h3>", unsafe_allow_html=True)
67+
68+
# Display the captured photo
69+
st.image(frame, channels="RGB", use_column_width=True)
70+
71+
if __name__ == '__main__':
72+
main()

0 commit comments

Comments
 (0)