Amaan Javed
Published

Python OpenCV NodeMCU Mask Detection

The system prevents you to forget the mask at home and also remind you to sanitize your hands when come come

IntermediateFull instructions provided1 hour3,862
Python OpenCV NodeMCU Mask Detection

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
IR Proximity Sensor
Digilent IR Proximity Sensor
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Jumper wires (generic)
Jumper wires (generic)
×1
web camera
×1

Software apps and online services

Arduino IDE
Arduino IDE
OpenCV
OpenCV

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Code

NodeMCU

Arduino
#include <ESP8266WiFi.h>
#include <Servo.h>

const char* ssid = "ssid";
const char* password = "pass";

#define echoPin 5 
#define trigPin 4
long duration; 
int distance;
int val;
int irs = 0;
//int pump = 14;

Servo myservo;
WiFiServer server(80);

void setup() {
  WiFi.softAPdisconnect (true);
  Serial.begin(115200);
  delay(10);
  myservo.attach(2)
  myservo.write(0);
  pinMode(irs, INPUT);
  //pinMode(pump,OUTPUT);
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  server.begin();
  
 Serial.print(WiFi.localIP());
  
}
 
void loop() {
  int irsval = digitalRead(irs);
  digitalWrite(trigPin, 0);
  delayMicroseconds(2);
  digitalWrite(trigPin, 1);
  delayMicroseconds(10);
  digitalWrite(trigPin, 0);
  duration = pulseIn(echoPin, 1);
  distance = duration * 0.034 / 2; 
  if (distance < 45){
    Serial.println('H');
    delay(2000);
  }
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  while(!client.available()){
    delay(1);
  }
 
  String request = client.readStringUntil('\r');
  client.flush();
  
  if (request.indexOf("/M") != -1){
    myservo.write(90);
    delay(1000);
    myservo.write(0);
  }
  else if (request.indexOf("/P") != -1)  {
    Serial.println('D');
    delay(30000);
    if(irsval==0){
      Serial.println('S');
    }
  }
  
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); 
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<center>");
  client.println("<h1 style=background-color:DodgerBlue;>ATI SYSTEMS</h1>");
  client.println("</center>"); 
  client.println("</html>");
 
  delay(1);
}

Python OpenCV

Python
import cv2
import serial
import webbrowser
import pyttsx3

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)

def say(audio):
    engine.say(audio) 
    engine.runAndWait()
    
m5 = serial.Serial('COM8', 115200, timeout=.1)

face_cascade = cv2.CascadeClassifier('/Users/HP/Desktop/haarcascade_frontalface_default.xml')
mouth_cascade = cv2.CascadeClassifier('/Users/HP/Desktop/Mouth.xml')

cap = cv2.VideoCapture(1)

while True:
    da = m5.readline()[:-2]
    if da==b'H':
        ret, img = cap.read()
        img = cv2.flip(img,1)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        (thresh, black_and_white) = cv2.threshold(gray, 80, 255, cv2.THRESH_BINARY)
        faces = face_cascade.detectMultiScale(gray, 1.1, 4)
        faces_bw = face_cascade.detectMultiScale(black_and_white, 1.1, 4)
        for (x, y, w, h) in faces:
            cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
            roi_gray = gray[y:y + h, x:x + w]
            roi_color = img[y:y + h, x:x + w]
            mouth_rects = mouth_cascade.detectMultiScale(gray, 1.5, 5)
            if(len(mouth_rects) == 0):
                print("MASK DETECTED")
            else:
                for (mx, my, mw, mh) in mouth_rects:
                    if(y < my < y + h):
                        print("NO MASK DETECTED")
                        say("sir please wear mask")
                        webbrowser.open('http://192.168.43.212/M')
                        break    
    elif da==b'D':
        say("sir please sanitize your hands")
    elif da==b'S':
        webbrowser.open('http://192.168.43.212/P')
    else:
        print("YOU ARE GREAT AJ")
    #cv2.imshow('frame',img)    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Credits

Amaan Javed

Amaan Javed

19 projects • 4 followers
Student

Comments