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
print("Starting video stream") | |
vs = VideoStream(src=0).start() | |
time.sleep(2.0) | |
# looping 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 800 pixels | |
frame = vs.read() | |
frame = imutils.resize(frame, width=800) |
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
args = {} | |
args["face"] = "face_detector" | |
args["model"] = "mask_detector.model" | |
args["confidence"] = 0.4 | |
prototxtPath = os.path.sep.join([args["face"], "deploy.prototxt"]) | |
weightsPath = os.path.sep.join([args["face"], | |
"res10_300x300_ssd_iter_140000.caffemodel"]) | |
faceNet = cv2.dnn.readNet(prototxtPath, weightsPath) |
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
def detect_and_predict_mask(frame, faceNet, maskNet): | |
# grab the dimensions of the frame and then construct a blob | |
# from it | |
(h, w) = frame.shape[:2] | |
blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), | |
(104.0, 177.0, 123.0)) | |
# pass the blob through the network and obtain the face detections | |
faceNet.setInput(blob) | |
detections = faceNet.forward() |
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
N = EPOCHS | |
plt.style.use("ggplot") | |
plt.figure() | |
plt.plot(np.arange(0, N), H.history["loss"], label="train_loss") | |
plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss") | |
plt.plot(np.arange(0, N), H.history["accuracy"], label="train_acc") | |
plt.plot(np.arange(0, N), H.history["val_accuracy"], label="val_acc") | |
plt.title("Training Loss and Accuracy") | |
plt.xlabel("Epoch #") | |
plt.ylabel("Loss/Accuracy") |
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
# training the head of the network | |
H = model.fit( | |
aug.flow(trainX, trainY, batch_size=BS), | |
steps_per_epoch=len(trainX) // BS, | |
validation_data=(testX, testY), | |
validation_steps=len(testX) // BS, | |
epochs=EPOCHS) | |
# making predictions on the testing set |
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
# loading the MobileNetV2 network, ensuring the head FC layer sets are left off | |
baseModel = MobileNetV2(weights="imagenet", include_top=False, | |
input_tensor=Input(shape=(224, 224, 3))) | |
# constructing the head of the model that will be placed on top of the base model | |
headModel = baseModel.output | |
headModel = AveragePooling2D(pool_size=(7, 7))(headModel) | |
headModel = Flatten(name="flatten")(headModel) | |
headModel = Dense(128, activation="relu")(headModel) | |
headModel = Dropout(0.5)(headModel) |
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
# partition the data into training and testing splits using 75% of | |
# the data for training and the remaining 25% for testing | |
(trainX, testX, trainY, testY) = train_test_split(data, labels, | |
test_size=0.20, stratify=labels, random_state=42) | |
# construct the training image generator for data augmentation | |
aug = ImageDataGenerator( | |
rotation_range=20, | |
zoom_range=0.15, | |
width_shift_range=0.2, | |
height_shift_range=0.2, |
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
# initialize the initial learning rate, number of epochs to train for, | |
# and batch size | |
INIT_LR = 1e-4 | |
EPOCHS = 20 | |
BS = 32 | |
# grab the list of images in our dataset directory, then initialize | |
# the list of data (i.e., images) and class images | |
imagePaths = list(paths.list_images('../input/face-mask-detection-data')) |
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
from tensorflow.keras.preprocessing.image import ImageDataGenerator | |
from tensorflow.keras.applications import MobileNetV2 | |
from tensorflow.keras.layers import AveragePooling2D | |
from tensorflow.keras.layers import Dropout | |
from tensorflow.keras.layers import Flatten | |
from tensorflow.keras.layers import Dense | |
from tensorflow.keras.layers import Input | |
from tensorflow.keras.models import Model | |
from tensorflow.keras.optimizers import Adam | |
from tensorflow.keras.models import load_model |
NewerOlder