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

Aditgandhi
6 min readFeb 15, 2022

What are we doing in this blog,

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 a 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 haarcascade 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 types of objects 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 let’s us import numpy and haarcascade xml file. 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 below code snippet we are detecting face with the help of Haar Casacade xml file and then converting those image into grey form and further cropping it such that only face is visible.

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

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

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

Output of the above code will look like:

After collecting the sample check terminal to see this message

This is 100 samples your face stored in the file path you have given

Now it’s time to Train the model

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

We have to understand that face recognition is different from face detection, They are not 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 which best describe the image.

So to train our model to recongnize 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.

http://www.scholarpedia.org/article/File:LBP-face.jpg

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 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 our data of our face images/samples.

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

After detecting the face it will send a Mail and aWhatsapp text to email id and number you provided respectively and after that it will launch the AWS EC2 instance and will also create 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()

This code will show the following output.

Face Recongnized by the model

In terminal

Python generated Gmail message after face recongnization

Python code opens Whatsapp types the message

“Welcome back!!This is from python code”

The message will be send after 20 seconds

Instance is in running state

AWS EC2 Instance has been launched

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

You can Find code here:

https://github.com/ADIT-GANDHI/Using-Face-Recognition-Launch-AWS-Instance-with-EBS-Send-Mail-and-Whatsapp-Message.

--

--