Skip to content

Commit

Permalink
Merge pull request #1630 from Niraj1608/mask-detection
Browse files Browse the repository at this point in the history
[Feature]: Streamlit Deployment and GUI for Face Mask Detection #1617
  • Loading branch information
sanjay-kv authored Nov 7, 2024
2 parents 20cc9d3 + c1e23a5 commit 347f4ad
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Detection Models/Face_Mask_Detection/Face_Mask_Detection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1062,12 +1062,23 @@
"id": "dbfeb3a0-d0a7-4446-8ae4-559206d46034",
"metadata": {},
"outputs": [],
"source": [
"#save the model\n",
"model.save('mask_detection_model.keras')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a62eb7fc",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -1081,7 +1092,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.12.1"
}
},
"nbformat": 4,
Expand Down
Binary file not shown.
83 changes: 83 additions & 0 deletions Detection Models/Face_Mask_Detection/predictive_system_gui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import cv2
import numpy as np
import keras
import tk as tk
from PIL import ImageTk, Image
import tkinter as tk
from tkinter import filedialog
import os

# Load the face mask detector model
model =keras.models.load_model("mask_detection.keras")

# Initialize the GUI
root = tk.Tk()
root.title("Mask Detection")
root.geometry("500x400")

# Function to classify the image
def classify_image(image):
# Preprocess the image
image = image.resize((128, 128))
image = np.array(image)
image = image / 255.0
input_image_reshaped = np.reshape(image, [1,128, 128, 3])
# Make predictions
predictions = model.predict(input_image_reshaped)
input_pred_label = np.argmax(predictions)

if input_pred_label == 1:
result = "With Mask"
else:
result = "Without Mask"

# Update the result label text
result_label.configure(text="Prediction: " + result)

# Function to handle file upload
def upload_image():
file_path = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select Image File",
filetypes=(("JPEG files", "*.jpg"), ("PNG files", "*.png")))
if file_path:
# Display the uploaded image in the GUI
image = Image.open(file_path)
image.thumbnail((300, 300))
photo = ImageTk.PhotoImage(image)
photo_label.configure(image=photo)
photo_label.image = photo

classify_image(image)

# Function to handle capturing photo from webcam
def capture_photo():
video_capture = cv2.VideoCapture(0)
_, frame = video_capture.read()
video_capture.release()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image = Image.fromarray(frame)
classify_image(image)

# Display the captured photo in the GUI
image.thumbnail((300, 300))
photo = ImageTk.PhotoImage(image)
photo_label.configure(image=photo)
photo_label.image = photo

result_label = tk.Label(root, text="", font=("Helvetica", 16))
result_label.pack(pady=10)

# Create the GUI components
upload_button = tk.Button(root, text="Upload Image", command=upload_image)
upload_button.pack(pady=10)

capture_button = tk.Button(root, text="Capture Photo", command=capture_photo)
capture_button.pack(pady=10)

result_label = tk.Label(root, text="")
result_label.pack(pady=10)

photo_label = tk.Label(root)
photo_label.pack()

# Run the GUI main loop
root.mainloop()
72 changes: 72 additions & 0 deletions Detection Models/Face_Mask_Detection/streamlit_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import cv2
import numpy as np
import keras
import streamlit as st
import PIL.Image

model=keras.models.load_model('mask_detection.keras')





def main():
st.title("Mask Detection App")
st.write("Please choose an option")

option = st.selectbox("Select an option", ("Upload Image", "Capture Photo"))

if option == "Upload Image":
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
# Read and preprocess the image
image = PIL.Image.open(uploaded_file)
image = image.resize((128,128))
image = np.array(image) # Convert to NumPy array
image = image / 255.0 # Normalize the image
input_image_reshaped = np.reshape(image, [1, 128, 128, 3])

# Make predictions
predictions = model.predict(input_image_reshaped)
input_pred_label = np.argmax(predictions)

# Display the result
if input_pred_label == 1:
st.markdown("<h3 style='text-align: center; '>Prediction: With Mask</h3>", unsafe_allow_html=True)
else:
st.markdown("<h3 style='text-align: center; '>Prediction: Without Mask</h3>", unsafe_allow_html=True)

# Display the uploaded image
st.write("")
st.write("**Uploaded Image**")
st.image(image, use_column_width=True)

elif option == "Capture Photo":
video_capture = cv2.VideoCapture(0)
_, frame = video_capture.read()
video_capture.release()

# Convert captured frame to RGB
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

# Resize and preprocess the image
image = cv2.resize(frame, (64, 64))
image = image / 255.0
input_image_reshaped = np.reshape(image, [1, 64, 64, 3])

# Make predictions
predictions = model.predict(input_image_reshaped)
input_pred_label = np.argmax(predictions)

# Display the result
if input_pred_label == 1:
st.markdown("<h3 style='text-align: center; '>Prediction: With Mask</h3>", unsafe_allow_html=True)
else:
st.markdown("<h3 style='text-align: center; '>Prediction: Without Mask</h3>", unsafe_allow_html=True)

# Display the captured photo
st.image(frame, channels="RGB", use_column_width=True)

if __name__ == '__main__':
main()

0 comments on commit 347f4ad

Please sign in to comment.