Face Mask Detector with Deep Learning

by. Jongwon Lee | 243 Views (156 Uniq Views) | over 3 years ago
#OpenCV #Python #DeepLearning
COVID-19: Face Mask Detector with OpenCV, Keras/TensorFlow, and Deep Learning
Writing in Progress
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.models import load_model
import numpy as np
import cv2

facenet = cv2.dnn.readNet('models/deploy.prototxt', 'models/res10_300x300_ssd_iter_140000.caffemodel')
model = load_model('models/mask_detector.model')

img = cv2.imread('week3/assets/mask.jpg')
img = cv2.resize(img, (960, 540))

h, w, c = img.shape

#preprocess image
blob = cv2.dnn.blobFromImage(img, size=(300, 300), mean=(104., 177., 123.))

#face detection by google
facenet.setInput(blob)
dets = facenet.forward()

#iterate each detected face
for i in range(dets.shape[2]):
    confidence = dets[0, 0, i, 2]

    if confidence < 0.5:
        continue

    #find vertex
    x1 = int(dets[0, 0, i, 3] * w)
    y1 = int(dets[0, 0, i, 4] * h)
    x2 = int(dets[0, 0, i, 5] * w)
    y2 = int(dets[0, 0, i, 6] * h)

    face = img[y1:y2, x1:x2]

    face_input = cv2.resize(face, dsize=(224, 224))
    face_input = cv2.cvtColor(face_input, cv2.COLOR_BGR2RGB)
    face_input = preprocess_input(face_input)
    face_input = np.expand_dims(face_input, axis=0)

    mask, nomask = model.predict(face_input).squeeze()
    
    #write text
    if mask > nomask:
            color = (0, 255, 0)
            confidence = 'Mask ' + str(round(mask*100, 2)) + '%'
    else:
        color = (0, 0, 255)
        confidence = 'No Mask ' + str(round(nomask*100, 2)) + '%'
    
    #draw rectangle
    cv2.rectangle(img, pt1=(x1, y1), pt2=(x2, y2), thickness=2, color=color)
    cv2.putText(img, text=confidence, org=(x1, y1 - 10), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.8, color=color, thickness=2)

cv2.imshow('result', img)
cv2.waitKey(0)


1. Detect Face