Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image, ImageEnhance
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
import os
|
| 6 |
+
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
|
| 7 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
| 8 |
+
from tensorflow.keras.models import load_model
|
| 9 |
+
import detect_mask_image
|
| 10 |
+
|
| 11 |
+
# Setting custom Page Title and Icon with changed layout and sidebar state
|
| 12 |
+
st.set_page_config(page_title='Face Mask Detector', page_icon='😷', layout='centered', initial_sidebar_state='expanded')
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def local_css(file_name):
|
| 16 |
+
""" Method for reading styles.css and applying necessary changes to HTML"""
|
| 17 |
+
with open(file_name) as f:
|
| 18 |
+
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def mask_image():
|
| 22 |
+
global RGB_img
|
| 23 |
+
# load our serialized face detector model from disk
|
| 24 |
+
print("[INFO] loading face detector model...")
|
| 25 |
+
prototxtPath = os.path.sep.join(["face_detector", "deploy.prototxt"])
|
| 26 |
+
weightsPath = os.path.sep.join(["face_detector",
|
| 27 |
+
"res10_300x300_ssd_iter_140000.caffemodel"])
|
| 28 |
+
net = cv2.dnn.readNet(prototxtPath, weightsPath)
|
| 29 |
+
|
| 30 |
+
# load the face mask detector model from disk
|
| 31 |
+
print("[INFO] loading face mask detector model...")
|
| 32 |
+
model = load_model("mask_detector.h5")
|
| 33 |
+
|
| 34 |
+
# load the input image from disk and grab the image spatial
|
| 35 |
+
# dimensions
|
| 36 |
+
image = cv2.imread("./images/out.jpg")
|
| 37 |
+
(h, w) = image.shape[:2]
|
| 38 |
+
|
| 39 |
+
# construct a blob from the image
|
| 40 |
+
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300),
|
| 41 |
+
(104.0, 177.0, 123.0))
|
| 42 |
+
|
| 43 |
+
# pass the blob through the network and obtain the face detections
|
| 44 |
+
print("[INFO] computing face detections...")
|
| 45 |
+
net.setInput(blob)
|
| 46 |
+
detections = net.forward()
|
| 47 |
+
|
| 48 |
+
# loop over the detections
|
| 49 |
+
for i in range(0, detections.shape[2]):
|
| 50 |
+
# extract the confidence (i.e., probability) associated with
|
| 51 |
+
# the detection
|
| 52 |
+
confidence = detections[0, 0, i, 2]
|
| 53 |
+
|
| 54 |
+
# filter out weak detections by ensuring the confidence is
|
| 55 |
+
# greater than the minimum confidence
|
| 56 |
+
if confidence > 0.5:
|
| 57 |
+
# compute the (x, y)-coordinates of the bounding box for
|
| 58 |
+
# the object
|
| 59 |
+
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
|
| 60 |
+
(startX, startY, endX, endY) = box.astype("int")
|
| 61 |
+
|
| 62 |
+
# ensure the bounding boxes fall within the dimensions of
|
| 63 |
+
# the frame
|
| 64 |
+
(startX, startY) = (max(0, startX), max(0, startY))
|
| 65 |
+
(endX, endY) = (min(w - 1, endX), min(h - 1, endY))
|
| 66 |
+
|
| 67 |
+
# extract the face ROI, convert it from BGR to RGB channel
|
| 68 |
+
# ordering, resize it to 224x224, and preprocess it
|
| 69 |
+
face = image[startY:endY, startX:endX]
|
| 70 |
+
face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
|
| 71 |
+
face = cv2.resize(face, (224, 224))
|
| 72 |
+
face = img_to_array(face)
|
| 73 |
+
face = preprocess_input(face)
|
| 74 |
+
face = np.expand_dims(face, axis=0)
|
| 75 |
+
|
| 76 |
+
# pass the face through the model to determine if the face
|
| 77 |
+
# has a mask or not
|
| 78 |
+
(mask, withoutMask) = model.predict(face)[0]
|
| 79 |
+
|
| 80 |
+
# determine the class label and color we'll use to draw
|
| 81 |
+
# the bounding box and text
|
| 82 |
+
label = "Mask" if mask > withoutMask else "No Mask"
|
| 83 |
+
color = (0, 255, 0) if label == "Mask" else (0, 0, 255)
|
| 84 |
+
|
| 85 |
+
# include the probability in the label
|
| 86 |
+
label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)
|
| 87 |
+
|
| 88 |
+
# display the label and bounding box rectangle on the output
|
| 89 |
+
# frame
|
| 90 |
+
cv2.putText(image, label, (startX, startY - 10),
|
| 91 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
|
| 92 |
+
cv2.rectangle(image, (startX, startY), (endX, endY), color, 2)
|
| 93 |
+
RGB_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 94 |
+
mask_image()
|
| 95 |
+
|
| 96 |
+
def mask_detection():
|
| 97 |
+
local_css("css/styles.css")
|
| 98 |
+
st.markdown('<h1 align="center">😷 Face Mask Detection</h1>', unsafe_allow_html=True)
|
| 99 |
+
activities = ["Image", "Webcam"]
|
| 100 |
+
#st.set_option('deprecation.showfileUploaderEncoding', False)
|
| 101 |
+
st.sidebar.markdown("# Mask Detection on?")
|
| 102 |
+
choice = st.sidebar.selectbox("Choose among the given options:", activities)
|
| 103 |
+
|
| 104 |
+
if choice == 'Image':
|
| 105 |
+
st.markdown('<h2 align="center">Detection on Image</h2>', unsafe_allow_html=True)
|
| 106 |
+
st.markdown("### Upload your image here ⬇")
|
| 107 |
+
image_file = st.file_uploader("", type=['jpg']) # upload image
|
| 108 |
+
if image_file is not None:
|
| 109 |
+
our_image = Image.open(image_file) # making compatible to PIL
|
| 110 |
+
im = our_image.save('./images/out.jpg')
|
| 111 |
+
saved_image = st.image(image_file, caption='', use_column_width=True)
|
| 112 |
+
st.markdown('<h3 align="center">Image uploaded successfully!</h3>', unsafe_allow_html=True)
|
| 113 |
+
if st.button('Process'):
|
| 114 |
+
st.image(RGB_img, use_column_width=True)
|
| 115 |
+
|
| 116 |
+
if choice == 'Webcam':
|
| 117 |
+
st.markdown('<h2 align="center">Detection on Webcam</h2>', unsafe_allow_html=True)
|
| 118 |
+
st.markdown('<h3 align="center">This feature will be available soon!</h3>', unsafe_allow_html=True)
|
| 119 |
+
mask_detection()
|