-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_mask.py
More file actions
157 lines (133 loc) · 5.38 KB
/
Copy pathdetect_mask.py
File metadata and controls
157 lines (133 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Programmed By Ujjwal Humagain
# Date of Completion 11 February 2021
# Copyright (c) 2021 Ujjwal Humagain
from gpiozero import LED # initalizing GPIO pins
green = LED(14) #setting green LED in pin 14
orange = LED(15) #setting orange LED in pin 15
# import the necessary packages
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input # for preprocessing input
from tensorflow.keras.preprocessing.image import img_to_array #for converting image file to aray
from tensorflow.keras.models import load_model # loading the model created
from imutils.video import VideoStream # for starting the video streaming
import numpy as np # imported numpy
import argparse # for parsing the argument
import imutils # imported imutils library
import time # imported time
import cv2 # imported open cv
import os # imported os
def predict_mask_detected(videoFrame, faceDisplayed, maskUsed):
# grab the dimensions of the frame and then construct a blob
# from it
(h, w) = videoFrame.shape[:2]
blob = cv2.dnn.blobFromImage(videoFrame, 1.0, (300, 300),
(104.0, 177.0, 123.0)) # Binary Large Object (blob) is used in open cv and deep neural network
# pass the blob through the network and obtain the face detections
faceDisplayed.setInput(blob)
detections = faceDisplayed.forward()
# initialize our list of faces, their corresponding locations,
# and the list of predictions from our face mask network
faces = []
locs = []
preds = []
# loop over the detections
for i in range(0, detections.shape[2]):
# extract the confidence (i.e., probability) associated with
# the detection
confidence = detections[0, 0, i, 2]
# filter out weak detections by ensuring the confidence is
# greater than the minimum confidence
if confidence > args["confidence"]:
# compute the (x, y)-coordinates of the bounding box for
# the object
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
# ensure the bounding boxes fall within the dimensions of
# the frame
(startX, startY) = (max(0, startX), max(0, startY))
(endX, endY) = (min(w - 1, endX), min(h - 1, endY))
# extract the face ROI, convert it from BGR to RGB channel
# ordering, resize it to 224x224, and preprocess it
face = videoFrame[startY:endY, startX:endX]
face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
face = cv2.resize(face, (224, 224))
face = img_to_array(face)
face = preprocess_input(face)
# add the face and bounding boxes to their respective
# lists
faces.append(face)
locs.append((startX, startY, endX, endY))
# only make a predictions if at least one face was detected
if len(faces) > 0:
# for faster inference we'll make batch predictions on *all*
# faces at the same time rather than one-by-one predictions
# in the above `for` loop
faces = np.array(faces, dtype="float32")
preds = maskUsed.predict(faces, batch_size=32)
# return a 2-tuple of the face locations and their corresponding
# locations
return (locs, preds)
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--face", type=str,
default="face_detector",
help="path to face detector model directory")
ap.add_argument("-m", "--model", type=str,
default="mask_detector.model",
help="path to trained face mask detector model")
ap.add_argument("-c", "--confidence", type=float, default=0.5,
help="minimum probability to filter weak detections")
args = vars(ap.parse_args())
# load previously trained face detector model
print("Wait the face detection model is loading...")
prototxtPath = os.path.sep.join([args["face"], "deploy.prototxt"])
weightsPath = os.path.sep.join([args["face"],
"res10_300x300_ssd_iter_140000.caffemodel"])
faceDisplayed = cv2.dnn.readNet(prototxtPath, weightsPath)
maskUsed = load_model(args["model"])
# initialize the video stream and allow the camera sensor to warm up
print("Starting Video streaming in two seconds")
vs = VideoStream(usePiCamera=True).start()
time.sleep(2.0)
# loop over the frames from the video stream
while True:
# grab the frame from the threaded video stream and resize it
# to have a maximum width of 400 pixels
videoFrame = vs.read()
videoFrame = imutils.resize(videoFrame, width=500)
# detect faces in the frame and determine if they are wearing a
# face mask or not
(locs, preds) = predict_mask_detected(videoFrame, faceDisplayed, maskUsed)
# loop over the detected face locations and their corresponding
# locations
for (box, pred) in zip(locs, preds):
# unpack the bounding box and predictions
(startX, startY, endX, endY) = box
(mask, withoutMask) = pred
# determine the class label and color we'll use to draw
# the bounding box and text
if mask > withoutMask:
label = "Mask Detected."
color = (0, 255, 0)
orange.off()
green.on()
else:
label = "Mask Not Detected."
color = (0, 0, 255)
green.off()
orange.on()
# display the label and bounding box rectangle on the output
# frame
cv2.putText(videoFrame, label, (startX-50, startY - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2)
cv2.rectangle(videoFrame, (startX, startY), (endX, endY), color, 2)
# show the output frame
cv2.imshow("Face Mask Detector", videoFrame)
key = cv2.waitKey(1) & 0xFF
# if the `s` key was pressed, break from the loop
if key == ord("s"):
green.off()
orange.off()
break
# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()