-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1630 from Niraj1608/mask-detection
[Feature]: Streamlit Deployment and GUI for Face Mask Detection #1617
- Loading branch information
Showing
4 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
83 changes: 83 additions & 0 deletions
83
Detection Models/Face_Mask_Detection/predictive_system_gui.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |