Program for Recognizer to perform some Task

In this blog, I am going to explain how we can create a program to perform some tasks by recognizing someone’s face. For this purpose, we will train models which will recognize two person’s face and then it will send email and WhatsApp message when it recognizes first person’s face and will create EC2 instance and EBS Volume when it will recognize second persons face.

For face recognition, we will use LBPH Algorithm. We will train two different models with two different person’s faces. So let us first understand about LBPH algorithm.

Local Binary Patterns Histograms (LBPH)

Local Binary Pattern (LBP) is a simple yet very efficient texture operator which labels the pixels of an image by thresholding the neighborhood of each pixel and considers the result as a binary number.

It was first described in 1994 (LBP) and has since been found to be a powerful feature for texture classification. It has further been determined that when LBP is combined with histograms of oriented gradients (HOG) descriptors, it improves the detection performance considerably on some datasets.

Now let’s create code to collect the dataset, train the model and finally recognize the face to perform some task.

Step 1: Collect datasets of two-person and saves them in a folder.

In this step, we will create code which will collect 100 cropped image of one person and then it will save them in one folder.

import cv2
import numpy as np
# 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/user1/' + 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)

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

Similarly, we can write code to collect the cropped image of the second person and saves them in a file.

Step 2: Train the model with the image.

In this step, we will train the model with the collected dataset which is stored in a folder. We will pass those images to the model to get trained. Here the complete folder is acting as a dataset so we need to treat the folder as a dataset.

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/user1/'
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()
model1 = cv2.face_LBPHFaceRecognizer.create()
# Let's train our model
model1.train(np.asarray(Training_Data), np.asarray(Labels))
print("Model trained sucessefully")

This model is for person 1 whose face will be used to send Email and WhatsApp messages. Similarly, we can train another model for the second person face which will we use to create EC2 instance and EBS Volume.

Step 3: Create a function to send Mail and WhatsApp message.

Here we will use smtplib module to send the email and pywhatkit module to send the WhatsApp message.

# Send Email
import smtplib as s
def mail():
server = s.SMTP('smtp.gmail.com', 587)
server.starttls()
sender_mail = 'sender@gmail.com'
sender_passwd = '**********'
server.login(sender_mail, sender_passwd )
receiver_mail = 'receiver@gmail.com'
body_msg = "Hello Aditya, this is your face...!!"
server.sendmail(sender_mail,
receiver_mail, body_msg)
print("Sending Mail Please wait......")
print("Email has been sent to receiver_mail id")
# Send WhatsApp message
import pywhatkit
from datetime import datetime
def whatsapp():
number = '+91**********'
msg = 'hy this is python'
now = datetime.now()
hr = int(now.strftime("%H"))
min = int( now.strftime("%M"))
pywhatkit.sendwhatmsg(number,"Hello Aditya, this is your face...!!", hr,min+1 ,wait_time=10)

Step 4: Recognize face to send mail and WhatsApp message.

In this step, we will write code which will recognize the face of person 1 to send mail and WhatsApp message. If the accuracy will be more than 90 percent then only it will perform those tasks.

import cv2
import numpy as np
import os
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)
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 = model1.predict(face)
# harry_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, "Hey Aditya", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
cv2.imshow('Face Recognition', image )
cap.release()
cv2.destroyAllWindows()
mail()
whatsapp()
break

else:

cv2.putText(image, "I dont know, how r u", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
cv2.imshow('Face Recognition', image )
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

Let’s see the output of the mail that was sent.

Step 5: Write a function to create EC2 instance and EBS Volume.

For this purpose, I am using terraform to create EC2 instance and EBS Volume and then attach them. Here is the terraform code which will create EC2 instance, will create EBS Volume, and then attach volume with the instance.

# Provider
provider "aws" {
region = "ap-south-1"
profile="aditya"
}
#Launch EC2 instance
resource "aws_instance" "os1" {
ami = "ami-010aff33ed5991201"
instance_type = "t2.micro"
tags = {
Name = "myfirstos1"
}
}
#Create EBS Volume
resource "aws_ebs_volume" "st1" {
availability_zone = aws_instance.os1.availability_zone
size = 2
tags = {
Name = "EBS Volume"
}
}
#Attach EBS Volume to instance
resource "aws_volume_attachment" "ebs_att" {
device_name = "/dev/sdh"
volume_id = aws_ebs_volume.st1.id
instance_id = aws_instance.os1.id
}

Now I will create one function in python which will run terraform code using the OS module.

import os
def AWS_launch():
print("Instance and EVS Volume creating...")
os.system('terraform init')
os.system('terraform apply --auto-approve')
print("Instance and EBS Volume are launched")

This function will run terraform commands to apply the code.

Step 6: Recognize face to launch EC2 instance and EBS Volume.

Similar to the previous task we will write code that will recognize send persons face and then it will create EC2 instance and EBS Volume.

import cv2
import numpy as np
import os
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)
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 = model2.predict(face)
# harry_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, "Hey Aditya", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
cv2.imshow('Face Recognition', image )
cap.release()
cv2.destroyAllWindows()
AWS_launch()
break

else:

cv2.putText(image, "I dont know, how r u", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
cv2.imshow('Face Recognition', image )
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

Let’s check if has created an EC2 instance and EBS Volume or not.

I had trained this model with my friend's image and while recognizing I had used his image and we can see that it had performed the task which we wanted.

You can refer to the complete code on my GitHub.

GitHub Link: https://github.com/adyraj/FaceRecognizer.git

Thank You for reading!! 😇😇

--

--

--

I'm passionate learner diving into the concepts of computing 💻

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using Word Embeddings to help bridge different sets of vocab

Santander Customer Transaction Prediction: A Simple Machine Learning Problem

Edge AI - Computer vision on the edge

Building a Facial Recognition Pipeline with Deep Learning in Tensorflow

Decoding the Best Papers from ICLR 2019 — Neural Networks are Here to Rule

Guide to Coding a Custom Convolutional Neural Network in TensorFlow Core

Sentiment Analysis On IMDB Movie Review

Neural Networks and Self Driving Car without any complex Mathematics :

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
Aditya Raj

Aditya Raj

I'm passionate learner diving into the concepts of computing 💻

More from Medium

How to Install Docker with NVIDIA support?

Smart Traffic Management System

Face Detection in Videos

OCR is Optical Character Recognition, which defines the process of converting scanned documents or…