Skip to content

Instantly share code, notes, and snippets.

@karan19100
Created December 18, 2020 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karan19100/94ec8e1dab7e8ba4deab7de5670a8625 to your computer and use it in GitHub Desktop.
Save karan19100/94ec8e1dab7e8ba4deab7de5670a8625 to your computer and use it in GitHub Desktop.
import numpy as np
import cv2
import random
# multiple cascades: https://github.com/karan19100/Face-Mask-Detection-using-Open-CV-/tree/main/data/xml
face_cascade = cv2.CascadeClassifier('data\\xml\\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('data\\xml\\haarcascade_eye.xml')
mouth_cascade = cv2.CascadeClassifier('data\\xml\\haarcascade_mcs_mouth.xml')
upper_body = cv2.CascadeClassifier('data\\xml\\haarcascade_upperbody.xml')
# Adjust threshold value in range 80 to 105 based on your light.
bw_threshold = 80
# User message
font = cv2.FONT_HERSHEY_SIMPLEX
org = (30, 30)
weared_mask_font_color = (255, 255, 255)
not_weared_mask_font_color = (0, 0, 255)
thickness = 2
font_scale = 1
weared_mask = "Thank You for wearing MASK"
not_weared_mask = "Please wear MASK to defeat Corona"
cap = cv2.VideoCapture(0)
while 1:
# Get individual frame
ret, img = cap.read()
img = cv2.flip(img,1)
# Convert Image into gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Convert image in black and white
(thresh, black_and_white) = cv2.threshold(gray, bw_threshold, 255, cv2.THRESH_BINARY)
cv2.imshow('black_and_white', black_and_white)
# detect face
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Face prediction for black and white
faces_bw = face_cascade.detectMultiScale(black_and_white, 1.1, 4)
if(len(faces) == 0 and len(faces_bw) == 0):
cv2.putText(img, "No face found...", org, font, font_scale, weared_mask_font_color, thickness, cv2.LINE_AA)
elif(len(faces) == 0 and len(faces_bw) == 1):
# It has been observed that for white mask covering mouth, with gray image face prediction is not happening
cv2.putText(img, weared_mask, org, font, font_scale, weared_mask_font_color, thickness, cv2.LINE_AA)
else:
# Draw rectangle on gace
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 255), 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = img[y:y + h, x:x + w]
# Detect lips counters
mouth_rects = mouth_cascade.detectMultiScale(gray, 1.5, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment