Nadeem sharief B
Created May 28, 2020 © GPL3+

Face Mask Detector Computer Vision

Computer Vision project that detects implementation of face mask in public places or gatherings.

IntermediateShowcase (no instructions)4 hours26
Face Mask Detector Computer Vision

Things used in this project

Hardware components

Webcam, Logitech® HD Pro
Webcam, Logitech® HD Pro
×1

Software apps and online services

OpenCV
OpenCV
TensorFlow
TensorFlow
Jupyter Notebook
Jupyter Notebook

Story

Read more

Code

Detecting Masks.ipynb

Python
Code is written and executed in Jupyter Notebook. The Model is pretrained with 1000 images in each class. The final code is pasted below.
!pip install keras
!pip install opencv-python
from keras.models import load_model
import cv2
import numpy as np

model = load_model('model-017.model')

face_clsfr=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

source=cv2.VideoCapture(0)

labels_dict={0:'MASKED',1:'NO MASK'}
color_dict={0:(0,255,0),1:(0,0,255)}

while(source.isOpened()):  # check !
    # capture frame-by-frame
    ret, img = source.read()

    if ret: # check ! (some webcam's need a "warmup")
        # our operation on frame come here
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces=face_clsfr.detectMultiScale(gray,1.3,5)

        # Display the resulting frame
        cv2.imshow('img', gray)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break  

    for (x,y,w,h) in faces:
    
        face_img=gray[y:y+w,x:x+w]
        resized=cv2.resize(face_img,(100,100))
        normalized=resized/255.0
        reshaped=np.reshape(normalized,(1,100,100,1))
        result=model.predict(reshaped)

        label=np.argmax(result,axis=1)[0]
      
        cv2.rectangle(img,(x,y),(x+w,y+h),color_dict[label],2)
        cv2.rectangle(img,(x,y-40),(x+w,y),color_dict[label],-1)
        cv2.putText(img, labels_dict[label], (x, y-10),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,255,255),2)
        
        
    cv2.imshow('LIVE',img)
    key=cv2.waitKey(1)
    
    if(key==27):
        break
        
cv2.destroyAllWindows()
source.release()

Credits

Nadeem sharief B

Nadeem sharief B

1 project • 0 followers
Thanks to Sentdex and The perceptron.

Comments