Launching AWS instance and running system commands through Facial Recognition.

Shashwat Gaur
4 min readJun 24, 2021

--

Facial recognition is not a novel concept, yet the progress it has made in the recent years is truly amazing. Facial recognition has found varied applications in multiple domains, and with each passing day, new approaches involving its application have been witnessed. In this particular blog, we would be automating the procedure of launching an AWS instance, and also will be running the system commands, through face recognition. To keep in mind the knowledge of open cv, and other modules of python including OS, pywhatkit and JSON is necessary.

We would start by importing the necessary modules —

  • cv2,
  • -numpy,
  • -pywhatkit,
  • -os,
  • -json
import cv2
import numpy as np
from os import listdir
from os.path import isfile,join
import json
import subprocess
import pywhatkit

To detect the face in the frame we would be using “haarcascade_frontalface_default.xml”, which is an already trained model. Furthermore, using a combination of open-cv and haarcascade, we will generate the training data. Hereby, we will generate 100 photos. These would be saved at a specific path in our PCs.

face_classifier= cv2.CascadeClassifier('haarcascade_frontalface_default.xml')def face_extractor(img):
gray= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.array(gray, dtype='uint8')
faces= face_classifier.detectMultiScale(gray,1.3,5)
if faces is ():
return None
for(x,y,w,h) in faces:
cropped=img[y:y+h,x:x+w]
return cropped
#Data creation
cap=cv2.VideoCapture(0)
count=0
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)

file_name_path='./faces/user/'+str(count)+'.jpg'
cv2.imwrite(file_name_path,face)
cv2.putText(face,str(count),(50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
cv2.imshow('Face crop',face)

else:
print("NOTHING")
pass
if cv2.waitKey(10)==13 or count==100:
break
cap.release()
cv2.destroyAllWindows()
print("Collected")

Also we will have to maintain uniformity across the photos of the user's face,so, we will crop the images taken only to the face, and save them. After gathering data we will prepare our model and feed the data to train it. Now we will create different models for different users. For this we will use LBPHFaceRecognizer.

#train for user 1
import cv2
import numpy as np
from os import listdir
from os.path import isfile,join
data_path='./faces/user/'onlyfiles=[f for f in listdir(data_path) if isfile(join(data_path,f))]
training, labels=[],[]
for i,files in enumerate(onlyfiles):
img_pth=data_path+onlyfiles[i]
images=cv2.imread(img_pth,cv2.IMREAD_GRAYSCALE)
training.append(np.asarray(images,dtype=np.uint8))
labels.append(i)

labels=np.asarray(labels, dtype=np.int32)
model=cv2.face_LBPHFaceRecognizer.create()
model.train(np.asarray(training),np.asarray(labels))
print("Training done")
#User2
import cv2
import numpy as np
from os import listdir
from os.path import isfile,join
data_path1='./facenew/user2/'
onlyfiles1=[f for f in listdir(data_path1) if isfile(join(data_path1,f))]
training1, labels1=[],[]
for i,files in enumerate(onlyfiles1):
img_pth1=data_path1+onlyfiles1[i]
images1=cv2.imread(img_pth1,cv2.IMREAD_GRAYSCALE)
training1.append(np.asarray(images1,dtype=np.uint8))
labels1.append(i)

labels1=np.asarray(labels1, dtype=np.int32)
model1=cv2.face_LBPHFaceRecognizer.create()
model1.train(np.asarray(training1),np.asarray(labels1))
print("Training done")

After creating the model and training it, we use the modules to send text on whatsapp if the user is USER1 and mail it to their mail ids. But if the face recognized is USER-2, it creates an AWS instance.

face_classifier=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
def face_detector(img, size=0.5):
gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces= face_classifier.detectMultiScale(gray,1.3,5,0)
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

cap=cv2.VideoCapture(0)
def confidence(results, image):
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)
return confidence
while True:
ret, frame= cap.read()
image,face=face_detector(frame)
try:
face=cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
results=model.predict(face)
results1=model1.predict(face)

face1= confidence(results, image)
face2 = confidence(results1, image)


if face1 > 80:
cv2.putText(image, "User1", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
cv2.imshow('Face Recognition', image )
import smtplib
gmail_user = "username"
gmail_pwd = "pass"
TO = 'xyz'
SUBJECT = "text"
TEXT = "text"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
BODY = '\r\n'.join(['To: %s' % TO,
'From: %s' % gmail_user,
'Subject: %s' % SUBJECT,
'', TEXT])
server.sendmail(gmail_user, [TO], BODY)
print ('email sent')

pk.sendwhatmsg("+91xxxxxxxxxxx",
"Hello",
hour, min)
print("Successfully Sent!")
if cv2.waitKey(10)==13:
break
cv2.destroyAllWindows()
elif face2 > 80:
cv2.putText(image, "User2", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
cv2.imshow('Face Recognition', image )
instance_id = sp.getoutput("aws ec2 run-instances --image-id <id> --instance-type t2.micro --count 1 --subnet-id subnet-id-value --security-group-ids <sg-id> --key-name <keyname> --tag-specifications ResourceType=instance,Tags=[{Key=Name,Value=cvTask_Instance}] --query Instances[*].[InstanceId] --output text")
print("Instance Id : "+str(instance_id))
volume_id = sp.getoutput("aws ec2 create-volume --volume-type gp2 --availability-zone <zone> --size 5 --tag-specifications=ResourceType=volume,Tags=[{Key=Name,Value=cvTask_volume}] --query VolumeId --output text")
print("Volume Id : "+str(volume_id) )
time.sleep(20)
sp.getoutput("aws ec2 attach-volume --volume-id {} --instance-id {} --device /dev/sdf".format(volume_id,instance_id))
print("Volume and Instance Attached")
if cv2.waitKey(10)==13:
break
cv2.destroyAllWindows()
else:
cv2.putText(image, "idk who", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
cv2.imshow('Face Recognition', image )
except:
cv2.putText(image,"Nope idk",(220,120),cv2.FONT_HERSHEY_COMPLEX,1,(0,0,255),2)
cv2.putText(image,"Lets see",(250,450),cv2.FONT_HERSHEY_COMPLEX,1,(0,0,255),2)
cv2.imshow('Face recognition', image)
pass
if cv2.waitKey(1)==13:
break
cap.release()
cv2.destroyAllWindows()

Ta-da, our model is created and works perfectly. On recognizing the respective users,i.e., seeing user 1 it sends a WhatsApp text and an email. On seeing user 2, it creates an AWS instance and attaches EBS to it.

--

--

Shashwat Gaur
Shashwat Gaur

Written by Shashwat Gaur

0 Followers

A tech enthusiast and an aspirer, aiming to olve real life problems, looking forward with a collaborative mindset. Planning to put the skills to best use.

No responses yet