My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMSĀ for Enterprise.
Upgrade āœØLearn more
Face Detection model to perform some task

Face Detection model to perform some task

Rachit Sharma's photo
Rachit Sharma
Ā·Jun 18, 2021Ā·

4 min read

Task

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

1) When it recognize your face then - šŸ‘‰ 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.

2) When it recognize second face, it can be your friend or family members face. šŸ‘‰ Create EC2 instance in the AWS using CLI. šŸ‘‰ Create 5 GB EBS volume and attach it to the instance.

Steps involved in accomplishment this task are as follows -

Step 1 - First we will create a training datasets of both the faces, that is, ours and the friend's face.

For this we collect 1000 samples of each face from webcam input using cv2.VideoCapture(0) and cap.read() .

Once the face is read, we resize the face and change it into grayscale image.

face = cv2.resize(face_extractor(frame), (200, 200))
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)

After that we store this image in specified directory with unique name.

file_name_path = '.' + str(count) + '.jpg'
cv2.imwrite(file_name_path, face)

After storing each face, value of count is incremented by 1, and when below condition is reached the program is stopped.

if cv2.waitKey(1) == 13 or count == 1000: 
        break

Note - The detection of face is done using haarcascade classifier.

Step 2 - Next, we create two prediction model for each of the two faces using

cv2.face_LBPHFaceRecognizer.create()

To train the models, we first need to store the pixel values of the images and labels in two different numpy arrays. After that training of the model is done using rachit_model.train(np.asarray(Training_Data), np.asarray(Labels)) .

Step 3 - Next we create necessary functions, which are to be used when a particular face is detected.

Various functions that are required are - send_mail(), send_whatsApp(), send_aws_request() and face_detector.

1. send_mail() - This function is used to send email if user's face is detected. For this first we need to open a URL (mail.google.com/mail/u/0/#inbox) in Microsoft Edge using command line. Then we need to specify the email address to which the mail should be sent. This is done using pyautogui.typewrite("rachitsharma19962000@gmail.com") . Next we specify the message to be sent.

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("sharmasudhanshu97@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')

2. send_whatsApp() - Next, in a similar way we open whatsApp in browser and Specify the contact of the friend and send the message.

def send_whatsApp():
    os.system( "start msedge https://web.whatsapp.com/")
    time.sleep(10)
    pyautogui.click(242,211)
    pyautogui.typewrite("Ritik CSE")
    time.sleep(2)
    pyautogui.press('enter')
    time.sleep(2)
    pyautogui.typewrite("Hello Amigo!")
    time.sleep(2)
    pyautogui.press('enter')

3. send_aws_request() - Launches the specified number of instances using an AMI for which you have permissions. You can launch the instance into a virtual private cloud (VPC), or if your account supports it, into EC2-Classic. Initially, your instance appears in the pending state, but changes to the running state after a few minutes.

To add a block device to your instance, specify the --block-device-mappings option when you use run-instances.

Add a tag to your instance, a tag is a label that you assign to an AWS resource. It enables you to add metadata to your resources that you can use for a variety of purposes.

Connect to your instance, when your instance is running, you can connect to it and use it just as you'd use a computer sitting in front of you.

List your instances, we can use the AWS CLI to list your instances and view information about them. You can list all your instances, or filter the results based on the instances that you're interested in.

The following command filters the list to only your t2.micro instances and outputs only the InstanceId values for each match.

aws ec2 describe-instances --filters "Name=instance-type,Values=t2.micro" --query "Reservations[].Instances[].InstanceId"

4. face_detector() - This function detects faces using face_classifier.detectMultiScale(gray, 1.3, 5) .

Step 4 - Capture the live photo of faces and call the face_detector function to know which face is detected. This is done based upon confidence score. If confidence score is greater than 90, we accept the face and call appropriate functions.