Using Face Recognition Launch AWS Instance with EBS, Send Mail, and Whatsapp Message.

M G GOVARDHAN GOWDA
5 min readSep 7, 2021
Face recognition

When Python code recognizes your face then it should send mail to any mail-id and also send a WhatsApp message to your friend.

And then launch an AWS EC2 instance and also attach 8Gb EBS volume to EC2 on the fly.

So let’s get started.

We will need our face dataset so first, we generate the 100 images of our face data through the program.

we will import the library like cv2, NumPy, os, pywhatkit, datetime, subprocess, smtplib, etc. we have used a haar cascade classifier to detect the only face. we have captured the image and crop the face and put the particular folder. collected data successfully.

Libraries description

PyWhatKit is a Python library for Sending WhatsApp messages at a certain time.

The smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP listener daemon.

And after that, we will read the image dataset and train the model. and we will also use LBPH Face Recognizer to recognize the face to the dataset.

Haar Cascades can be used to detect any type of object as long as you have the appropriate XML file for it. You can even create your own XML files from scratch to detect whatever type of object you want

Now lets us import NumPy and haar cascade XML files. You can get this any frontal face detection from here: https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml

According to the below code snippet we are detecting the face with the help of the Haar Cascade XML file and then converting those images into grey form and further cropping it such that only the face is visible.

We have initialized the Camera. (0) this means our python code will use an interior webcam.

Once the Webcam is initiated it will collect 100 samples of your images

I have used count here to perform the counter operation and then it will save the file in the given path below.

Now it’s time to train the model

Human beings perform face recognition automatically every day and practically with no effort. In the Information Technology world, face recognition is basically the task of recognizing a person based on its Data stored in the image's smallest part pixel.

We have to understand that face recognition is different from face detection, They are not the same.

Face Detection: it has the objective of finding the faces (location and size) in an image and probably extract them to be used by the face recognition algorithm.
Face Recognition: with the facial images already extracted, cropped, resized, and usually converted to grayscale, the face recognition algorithm is responsible for finding characteristics that best describe the image.

So to train our model to recognize our face we will be using LBPH Face Recognizer.

Local Binary Pattern Histogram(LBPH) is a simple yet very efficient texture operator which labels the pixels of an image by thresholding the neighborhood of each pixel and considers the result as a binary number.

Crowd based face detection using LBP

It can also be represented as a 3x3 matrix containing the intensity of each pixel (0~255).

Here, data means Image/photo, and lable means the name of that particular image. The above code snippet will give the following output:

Now the main part, In this code it will start our camera, detects our face, and match it with our model which we created previously with the data of our face images/samples.

After comparing in real-time our model with the current ongoing video it will show the text on the image that it recognized you or not and show the percentage of accuracy(confidence score ) / percentage of similarity of the current user with the owner's face.

After detecting the face it will send a Mail and Whatsapp text to the email id and number you provided respectively and after that, it will launch the AWS EC2 instance and will also create an additional 8Gb of EBS Volume from CLI.

import cv2
import numpy as np
import os
import smtplib
import pywhatkit
face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')def face_detector(img, size=0.5):

# Convert image to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
if faces is ():
return img, []


for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,255),2)
roi = img[y:y+h, x:x+w]
roi = cv2.resize(roi, (200, 200))
return img, roi
# Open Webcam
cap = cv2.VideoCapture(0)
while True: ret, frame = cap.read()

image, face = face_detector(frame)

try:
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
# Pass face to prediction model
# "results" comprises of a tuple containing the label and the confidence value
results = sachin_model.predict(face)
# harry_model.predict(face)

if results[1] < 500:
confidence = int( 100 * (1 - (results[1])/400) )
display_string = str(confidence) + '% Confident it is User'

cv2.putText(image, display_string, (100, 120), cv2.FONT_HERSHEY_COMPLEX, 1, (255,120,150), 2)

if confidence > 65:
cv2.putText(image, "Hey Sachin", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
cv2.imshow('Face Recognition', image )
#os.system("chrome https://www.google.com/search?q=vimal+daga")


server = smtplib.SMTP_SSL("smtp.gmail.com",465)
server.login("sender gmail id", "sender gmail password")
server.sendmail("sender gmail id", "receiver email id", "hello how are you ? Welcome back to Gmail!" )
server.quit()

#import pywhatkit
pywhatkit.sendwhatmsg('+91...your friends number', 'Welcome back!We have recognized your face', 0,42)
print("Whatsapp message send Successfully!")
os.system("aws ec2 run-instances --image-id ami-0aeeebd8d2ab47354 --instance-type t2.micro --count 1 --subnet-id subnet-dd829690 --security-group-ids sg-474b0f41 --key-name sachin ")
print("\t\t\t==============Instance launch Successfully======================")
os.system("aws ec2 create-volume --availability-zone us-east-1a --volume-type gp2 --size 1")
print("\t\t\t\n==============V0lume created Successfully======================")
break
else:

cv2.putText(image, "I dont know, who are you", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
cv2.imshow('Face Recognition', image )
except:
cv2.putText(image, "No Face Found", (220, 120) , cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
cv2.putText(image, "looking for face", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
cv2.imshow('Face Recognition', image )
pass

if cv2.waitKey(1) == 13: #13 is the Enter Key
break

cap.release()
cv2.destroyAllWindows()

The instance is in running state

AWS EC2 Instance has been launched

Create 8GB EBS volume and attach it to the instance.

--

--

M G GOVARDHAN GOWDA

MLOPS internship trainee @LinuxWorld informatics Pvt. LTD. || Student @Dayananda Sagar University |