Facial Recognition and Execute Command

Today we are going to complete some tasks using face recognition such as, when

  1. It recognizes my face:

    • Create EC2 instance using AWS CLI
    • Create 5GB EBS volume and attach it
  1. When it recognize my friend's face:

    • Email on my mail that it is my face
    • Send a WhatsApp message to Friend

Now as for the steps that are required for accomplishing this task:

Collecting dataset

So to train a model to recognize any face we need to collect sample images of our faces. So made use of Haarcascade Face Recognizer to detect the face and crop the extra information/background from image and collect only the face information. The code for the above step is as:

BJ6A9T8B4D.jfif

Training models for our faces:

As we have collected our datasets we will store them in a list of training data and add labels.to train our model we will be using Local Binary Pattern Histogram algorithm. It is an architecture of face recognition which is a powerful algorithm to recognize the face under varying illumination conditions and at low resolution. The code for above step is:

We will be using number of libraries for our code:

import cv2
import numpy as np
from os import listdir
from os.path import isfile, join

z2rtnOQ4F.jfif

Similar procedure to be done for the training the model for my friends face as:

ZYPaVmzcN.jfif

So we have trained our models. My face model is stored in rit_model and my friends face is in sid_model

Recognizing Faces and Performimg Task:

libraries required for this step are:

import cv2
import numpy as np
import os
import pyautogui
import time

Various functions that will be called are as follows:

  • For sending a WhatsApp Message:
def send_whatsApp():
 os.system( "start msedge https://web.whatsapp.com/")
 time.sleep(10)
 pyautogui.click(242,211)
 pyautogui.typewrite("Friend_Name")
 time.sleep(2)
 pyautogui.press('enter')
 time.sleep(2)
 pyautogui.typewrite("Hello Amigo!")
 time.sleep(2)
 pyautogui.press('enter')
  • For sending an email:
def send_mail():
 os.system("start msedge https://mail.google.com/mail/u/0/#inbox")
 time.sleep(10)
 pyautogui.click(75,210)
 time.sleep(5)
 pyautogui.typewrite("example@gmail.com")
 time.sleep(2)
 pyautogui.press('tab')
 pyautogui.press('tab')
 pyautogui.typewrite("First Test")
 pyautogui.press('tab')
 pyautogui.typewrite("First Test BODy")
 time.sleep(2)
 pyautogui.hotkey('ctrl', 'enter')
  • For launch an AWS EC2 Instance and Attach 5 GB EBS volume to it using AWS CLI: We need to create a mapping.json file which will have information regarding EBS volume. The file will have following content:
[
    {
        "DeviceName": "/dev/xvdf",
        "Ebs": {
            "VolumeSize": 5
        }
    }
]

The function for creating instance:-

def send_aws_req():
 os.system('aws ec2 run-instances  --image-id ami-0aeeebd8d2ab47354 --instance-type t2.micro --count 1 --subnet-id subnet-cc788593 --key-name awsvmkey --security-group-ids sg-d477e2f5  --block-device-mappings "file://<path_of_mappings.json>"')

Now we will import Haarcascade Front Face Recognizer in a variable and detect the face. We will use open-cv library to retieve web cam and recognize user and if it recognizes my face will open Gmail and send a simple mail to my mail and and then send a WhatsApp message to my friend. And if it recognizes my friends face, will launch an EC2 instance.

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)
x=0
y=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 = sid_model.predict(face)
        results2 =rit_model.predict(face)
        # harry_model.predict(face)

        if results[1] < 500:
            confidence = int( 100 * (1 - (results[1])/400) )



        if confidence > 90:
            display_string = str(confidence) + '% Confident it is Sid'

            cv2.putText(image, display_string, (100, 120), cv2.FONT_HERSHEY_COMPLEX, 1, (255,120,150), 2)
            cv2.putText(image, "Hey Sudhanshu", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
            cv2.imshow('Face Recognition', image )
            if x == 1:
                continue
            send_mail()
            send_whatsApp()
            x=1


        else:
            cv2.putText(image, "I dont know, who r u", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
            cv2.imshow('Face Recognition', image )


        if results2[1] < 500:
            confidence2 = int( 100 * (1 - (results2[1])/400) )



        if confidence2 > 85:
            display_string2 = str(confidence2) + '% Confident it is Rit'

            cv2.putText(image, display_string2, (100, 120), cv2.FONT_HERSHEY_COMPLEX, 1, (255,120,150), 2)
            cv2.putText(image, "Hey Rithik", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)

            cv2.imshow('Face Recognition', image )
            if y == 1:
                continue
            send_aws_req()
            y=1            

        if x ==1 and y == 1: 
            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


cap.release()
cv2.destroyAllWindows()

Output:

_EYiEPZJu.jfif

As soon as this recognize my face it will launch an EC2 Instance.

6mpLmohR8c.jfif

And then when it recognize my friends face it will open new tab and send an email and WhatsApp Message

image.png

Now we have completed our task to recognize face and perform some sub task of sending WhatsApp Message , an Email and Launching an EC2 instance using AWS CLI.

Thank You