Facial Recognition Using Python and Execute Command

Facial Recognition Using Python and Execute Command

What are we going to do?

We Gonna recognize 2 faces, one mine and other of my friend and then perform some task which are as follows:

  1. When it recognize my face
    • Email on my mail that it is my face
    • Send a WhatsApp message to Friend
  2. When it recognize my friends face
    • Create EC2 instance using AWS CLI
    • Create 5GB EBS volume and attach it

Step1: Collect Face Sample of Me and My Friend

So before recognizing 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:-

Collect_Samples.PNG

We will collect samples of me and my friend by running the code separately and store the samples of me and friend in two separate folders inside one directory named face.

Step2: Train 2 models of Faces

We will be using number of libraries and importing the m such as-:

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

Then we will provide samples collected in previous step and store them in a list of training data and add labels. Then 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:-

Train_model.PNG

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

Train_model_R.PNG

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

Step3: Recognize Face and Perform Task

We will be importing number of libraries for this step that are as follows:-

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

So the task Includes various function being called :-

  1. 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')
    
  2. 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')
    
  3. 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 calling 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. The code for the same is below:-

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()

The Output is here: -

Screenshot (283).png As soon as this recognize my face it will open new tab and send an email and WhatsApp Message to my friend.

Email.PNG

And then when it recognize my friends face will launch an EC2 Instance.

Screenshot (284).png We can check the AWS console for the Start of EC2 Instance in AWS. aws.PNG

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

I would like to thank my mentor Mr.Vimal Daga for providing knowledge for performing this task.