Previously, I used OpenCV to visualize the viewing situation of art works. https://qiita.com/cami_oshimo/items/ce33491c07f9625ed121
I thought that if I applied this and changed to mouth detection instead of eyes, I could use the mask to judge wearing.
First, simply determine if your mouth is exposed and then determine if you are wearing a mask.
import sys
sys.path.append('/home/pi/.local/lib/python2.7/site-packages')
import numpy as np
import cv2
import time
import datetime
import ambient
args = sys.argv
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
mouth_cascade = cv2.CascadeClassifier('haarcascade_mcs_mouth.xml')
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
height = frame.shape[0]
width = frame.shape[1]
import sys
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.putText(frame, 'Please face in frame.', (350, 20), cv2.FONT_HERSHEY_PLAIN, 1.5, (0,255,0), thickness=2)
for (x,y,w,h) in faces:
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
mouth = mouse_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in mouth:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,255),2)
cv2.putText(frame, 'Not masked!!', (10, 460), cv2.FONT_HERSHEY_DUPLEX, 1.0, (0,0,255), thickness=2)
resized_img = cv2.resize(frame,(width*2, height*2))
cv2.imshow('maskcheck', resized_img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
maskcheck.sh
#! /bin/bash
cd /home/pi/opencv_programs/
sudo python3 detect_face_camera.py
Recommended Posts