diff --git a/Detection Models/Face_Mask_Detection/Face_Mask_Detection.ipynb b/Detection Models/Face_Mask_Detection/Face_Mask_Detection.ipynb index b937ecd44..a236a398c 100644 --- a/Detection Models/Face_Mask_Detection/Face_Mask_Detection.ipynb +++ b/Detection Models/Face_Mask_Detection/Face_Mask_Detection.ipynb @@ -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" }, @@ -1081,7 +1092,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.12.1" } }, "nbformat": 4, diff --git a/Detection Models/Face_Mask_Detection/mask_detection.keras b/Detection Models/Face_Mask_Detection/mask_detection.keras new file mode 100644 index 000000000..63dc22c9b Binary files /dev/null and b/Detection Models/Face_Mask_Detection/mask_detection.keras differ diff --git a/Detection Models/Face_Mask_Detection/predictive_system_gui.py b/Detection Models/Face_Mask_Detection/predictive_system_gui.py new file mode 100644 index 000000000..0243d7db2 --- /dev/null +++ b/Detection Models/Face_Mask_Detection/predictive_system_gui.py @@ -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() diff --git a/Detection Models/Face_Mask_Detection/streamlit_app.py b/Detection Models/Face_Mask_Detection/streamlit_app.py new file mode 100644 index 000000000..c30cc9a77 --- /dev/null +++ b/Detection Models/Face_Mask_Detection/streamlit_app.py @@ -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("