Facelock + AWS +Email + Whatsapp

In this article I’m going to show you how to create face recognition program which can detect your owners face and if face is matched then it will create AWS EC2 instance , EBS volume and attach EBS it EC2 on the fly.

But if someone else tried to access this app/program it will deny furthur access and click image of that person . That clicked image will be shared to owners email with attachment of that person. Also it will send text message on whats app that “Someone tried to access your application”.

Let’s understand step by strp process of this program and understand how it can be usefull for future.

Task Description 📄

❄️ Create a program that perform below mentioned task upon recognizing a particular face.

📌 When it recognize your face then —

👉 Create EC2 instance in the AWS using CLI.
👉 Create 5GB EBS volume and attach it to the instance.

📌 When it recognize second face, it can be your friend or family members face.

👉 It send mail to your mail id by writing this is face of your_name.
👉 Second it send whatsapp message to your friend, it can be anything.

STEP 1 : Capture owner’s face images for model training data

Lets understand how this part is done.

This code will capture 100 pic of owner as training data for model.

import cv2
import numpy as np
import time
# Load HAAR face classifier
face_classifier = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’)
# Load functions
def face_extractor(img):
# Function detects faces and returns the cropped face
# If no face detected, it returns the input image

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)

if faces is ():
return None

# Crop all faces found
for (x,y,w,h) in faces:
cropped_face = img[y:y+h, x:x+w]
return cropped_face# Initialize Webcam
cap = cv2.VideoCapture(0)
count = 0
# Collect 100 samples of your face from webcam input
while True:
ret, frame = cap.read()
if face_extractor(frame) is not None:
count += 1
face = cv2.resize(face_extractor(frame), (200, 200))
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
# Save file in specified directory with unique name
file_name_path = ‘./faces/user/’ + str(count) + ‘.jpg’
cv2.imwrite(file_name_path, face)
# Put count on images and display live count
cv2.putText(face, str(count), (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
cv2.imshow(‘Face Cropper’, face)
time.sleep(0.1)

else:
print(“Face not found”)
pass
if cv2.waitKey(1) == 13 or count == 100: #13 is the Enter Key
break

cap.release()
cv2.destroyAllWindows()
print(“Collecting Samples Complete”)

Step 2 : Creating model using captured images

using images captured in above steps it will create model using which we can detect face of user.

import cv2
import numpy as np
from os import listdir
from os.path import isfile, join
# Get the training data we previously made
data_path = './faces/user/'
onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path, f))]
# Create arrays for training data and labels
Training_Data, Labels = [], []
# Open training images in our datapath
# Create a numpy array for training data
for i, files in enumerate(onlyfiles):
image_path = data_path + onlyfiles[i]
images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
Training_Data.append(np.asarray(images, dtype=np.uint8))
Labels.append(i)
# Create a numpy array for both training data and labels
Labels = np.asarray(Labels, dtype=np.int32)
# Initialize facial recognizer
# model = cv2.face.createLBPHFaceRecognizer()
# NOTE: For OpenCV 3.0 use cv2.face.createLBPHFaceRecognizer()
# pip install opencv-contrib-python
# model = cv2.createLBPHFaceRecognizer()
Face_detect_model = cv2.face_LBPHFaceRecognizer.create()
# Let's train our model
Face_detect_model.train(np.asarray(Training_Data), np.asarray(Labels))
print("Model trained sucessefully")

Our model is trained sucessfully.

Now we have to create some functions for each tasks (tasks are sending mail , whats app message , creating ec2 and ebs and attach them) so our single code won’t look big and by creating functions it is also easier to manage our code.

Step 3: Creating functions for subtasks

function 1 : To send mail with image

In this function i’ve written python code which will select image from path of given folder and send that image as attachment in mail . I’ve used html code to send mail which will give it more better look.

If we send mail without html content it will share normal text and image .It can be worked for this task but using html made it more better in terms of look.

All credentials are stored in systems environmental variable to avoid credential leak .

import smtplib
import os
import getpass
import imghdr
from os import environ
from email.message import EmailMessage
def send_email():

message = EmailMessage()


#All variable values are set inside system’s environmental variable


SenderMail = environ.get(‘SENDER_MAIL’) #mail id of sender
Recvmail = environ.get(‘RECEIVER_MAIL’) #password of sender
SenderPass = environ.get(‘SENDER_PASS’)#mail id of receiver

print(“Sending mail….”)

message[‘subject’] = “This is text code by pratik”
message[‘from’] = SenderMail
message[‘to’] = Recvmail
message.set_content(“Welcome to Face recognition app” )#it will only display if html content is not visible
html_message = open(“mail.html”).read()
message.add_alternative(html_message , subtype = “html”)

with open(“./image/Crop.jpg” , “rb”) as attach_file:
image_name = attach_file.name
image_type = imghdr.what(attach_file.name)
image_data = attach_file.read()

message.add_attachment(image_data , maintype = “image”,
subtype = image_type , filename = “FaceLock.jpg”
)

with smtplib.SMTP_SSL(“smtp.gmail.com”,465) as smtp:#to connect with gmail server
smtp.login(SenderMail,SenderPass)
smtp.send_message(message)

print(“Email Sent sucessfully”)
#send_email()

html code of our mail link — https://github.com/pratik2630/Mail_template

Function 2: To send whats app message using python

Hers’s a second subtask of our program is to send whats app message using python. It can notify owner on whats app when some other person tries to access our app.

import pywhatkit #pywhatkit library is used for whatsapp operation using python
from datetime import datetime #datetime module to get current time
def wpmsg():
now = datetime.now() # Get current time
hr = int(now.strftime(“%H”)) # Current Hour
min = int( now.strftime(“%M”)) # Current mint
number = environ.get(‘MOB_NUMBER’)
pywhatkit.sendwhatmsg(number,”Hi Pratik , Someone tried to access your app”, hr,min+1 ,wait_time=10)

Function 3: Function to call above two functions and for AWS CLI subtask

This is part is very important in excecution of our task. In this part program will provide permission to owner and deny access to unknown person.

This code will be called in main code which is yet to be come in article.

So just assume if part is for authenticated user(owner of program/app)

In “ if ” part first it will show image of unlocked lock with text “Access granted” means now you can use furthur part of code.

After that it will start launching aws ec2 instance with name “Task6”. When it is completed then it will show text that “EC2 instance launched sucessfully”.Next it will launch EBS volume , get values of instance id and volume id which can be used for attaching EBS and finally attach EBS with EC2 with this it will also print some text simultaniously according to the task completion. Here our if part is done.

Now what happens in “elif” part is whenever elif condition satifies it will show text “Access Denied” on picture of Lock. Crop users face from image and first send it it given mail id and send text on whats app that “ Someone tried to access your app”

This is where our all main conditions for task satisfies.

We just have call this code in next code where face detection is done.

def aws_cli_access():
if FaceDetect == 70:
unlock = cv2.imread(“./image/unlock.jpg”)

cv2.putText(unlock, “Access”, (45, 50) , cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
cv2.putText(unlock, “Granted”, (45, 100) , cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
cv2.imshow(“unlock” , unlock)
cv2.waitKey(2000)
cv2.destroyAllWindows()
print(“=============================================”)
print(“Launching aws ec2 instance”)
os.system(“aws ec2 run-instances — image-id ami-0ad704c126371a549 — count 1 — instance-type t2.micro — availability-zone ap-south-1a — tag-specifications ResourceType=instance,Tags=[{Key=Name,Value=Task6}]”
)
print(“EC2 instance launched!!!!”)
print(“=============================================”)
print(“Launching EBS volume”)
os.system(“aws ec2 create-volume — volume-type gp2 — size 10 — availability-zone ap-south-1a — tag-specifications ResourceType=volume,Tags=[{Key=Name,Value=Task6EBS}]”)
print(“ EBS volume launched”)
print(“=============================================”)
print(“Saving instance id and volume id in variables”)
instance_id = subprocess.getoutput(“aws ec2 describe-instances — filters Name=tag:Name,Values=Task6 — query Reservations[*].Instances[*].[InstanceId] — output text”)
vol_id = subprocess.getoutput(“aws ec2 describe-volumes — filters Name=tag:Name,Values=Task6EBS — query Volumes[*].[VolumeId] — output text”)
print(“=============================================”)
print(“Attaching EBS volume”)
os.system(“aws ec2 attach-volume — instance-id {0} — volume-id {1} — device /dev/sdf”.format(instance_id,vol_id))
print(“EBS volume attached with EC2 sucessfully”)
print(“=============================================”)
print(“Infra deployed”)
print(“=============================================”)
elif Anon == 120:
lock = cv2.imread(“./image/lock.jpg”)
cv2.putText(lock, “Access”, (45, 50) , cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
cv2.putText(lock, “Denied”, (45, 100) , cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
cv2.imshow(“lock” , lock)
cv2.waitKey(200)
cv2.destroyAllWindows()

face_anon = face_classifier.detectMultiScale(image, 1.3, 5)#This code will detect face from image

for x,y,w,h in face_anon:
cimg1 = image[y:y+h, x:x+w, : ]#Here it crops the image using ordinates of image

#Below code was used to check output of cropped image
#cv2.imshow(“hii”, cimg1)
#cv2.waitKey(200)
#cv2.destroyAllWindows()
cv2.imwrite(“./image/Crop.jpg”,cimg1)#This will save our cropped picture inside /image folder
print(“=============================================”)
print(“We are sending mail to owner of this account”)
send_email()
print(“=============================================”)
print(“Sending whats app msg to owner”)
wpmsg()
print(“Message sent sucessfully to owner”)
print(“=============================================”)

Step 4 : Face detector

Here’s the main part of our program arrives . 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. After comparing 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.

If confidence score is more than 90 then it will show some text that he recognized you and count number of times face matched same with confidence score less than 90.

When it recognize image frames from video 70 times or fail to recognize image for 120 times then it will break the loop , stops video screen and call aws_cli_access function. If it recognized you 70 times then program will come to know that you are authenticated user and grant you access for next code which is launching ec2,ebs and connecting them.

On the other hand if program is failed to recognize user for 120 times then program concludes that it’s not authenticated user , it will crop face image of him/her and share it on owners mail and send text message to owner.

Coooool right!!!!!!!

It’s can be implemented as security feature with lots of more interesting use cases.

Here’s the code for it.

import cv2
import numpy as np
import os
import time
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)
FaceDetect = 0
Anon = 0
while True:ret, frame = cap.read()

image, face = face_detector(frame)
cv2.imwrite(“Mypic.jpg” , image )

try:
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
results = vimal_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 >= 90:
cv2.putText(image, “Hii Pratik”, (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
cv2.imshow(‘Face Recognition’, image )
FaceDetect += 1

#elif confidence < 90:
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 )
Anon += 1


#After matching with 70 frames it will break loop and go for next part where it will deploy infrastrure using terraform
if FaceDetect == 70:
print(“Your AWS infrastructure will be deployed”)
break


#After fail to match with 70 frames it will break loop and go for next part where it will take pic of that user , crop it , send mail to owner with that pic and send whats app msg
elif Anon == 120:
print(“Anon user”)
break

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
cv2.destroyAllWindows()# To call function where we wrote condition for sending mail+whats app message if face is not detected
#If it detects face , it will authenticate you and deploy terraform infrastructure
aws_cli_access()
cap.release()

This program concepts can be implemented in lots of real world use cases and can be useful for many of us. Hope you liked this article . here’s the github link for source code : https://github.com/pratik2630/Facelock_awscli_mail_whatsapp_msg

I’ll also add demo video here soon.

Or you can connect me on linkedin for updates: https://www.linkedin.com/in/pratikkorgaonkar

Thanks for reading!!!!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store