Face Recognization Automation|and sending emails and WhatsApp messages via face detection.

Babuakash
Analytics Vidhya
Published in
4 min readJun 24, 2021

--

Using CV2, DeepLEarning, and Nural Network.

Face Recognization automation

Problem Statment

In this task I'm introducing the power of deep learning where recognizing the face and sending the automation message on WhatsApp and emails.

Note before going this article you can go through my Github Repository for source code and for better understanding go through my article.

According to Wikipedia — ‘A facial recognition system is a technology capable of identifying or verifying a person from a digital image or a video frame from a video source.’

But what does it actually mean in the real world?

What is the simple explanation behind this? How does this technology work? Which are the areas that make use of it?

This article will answer all these questions and more.

Put your reading glasses on to demystify the world of FRT (Facial Recognition Technology).

Pattern recognition, face analysis, deep learning, and machine learning form the major contributors to the development of facial recognition systems. Deep learning refers to the subset of Machine Learning that enables machines to learn and solve complex problems using algorithms inspired by the human brain, sans any human intervention. It is inspired by the structure and function of the brain called artificial neural networks.

These artificial neural networks simulate the network of neurons that make up the human brain so that the machine can learn things and take decisions in a human-like manner. Facial recognition technology makes use of the same by recognizing the face of an individual from its database, just like humans recall different people from our memory.

How does Facial Recognition Technology Work?

STEP 1: FACE DETECTION

Firstly, as the person looks directly into the camera, it detects and recognizes a face. The camera can also detect faces from a crowd of people.

Step 2: FACE ANALYSIS

Load HAAR face classifier for recognizing the face from here :- HaarClassifire.

Then Detect the face using the detectMultiScale classifier.

STEP 3: CONVERTING AN IMAGE TO DATA

Once the analysis of the face is done, it is turned into a facial signature, i.e. a mathematical formula. This signature comprises code and is addressed as a faceprint. Just like each of us has a unique thumbprint, we have our unique faceprint and save it into the local directory.

Step 3 working 100 times to collect our own dataset, which will be used to build our model and Save the file in a specified directory with a unique name.

file_name_path = ‘./faces/user/’ + str(count) + ‘.jpg’

STEP 4:Train our Model for face detection.

Using LBPHFaceRecognizer we can create a dummy model for training.

akash_model = cv2.face_LBPHFaceRecognizer.create()

If LBPHFaceRecognizer is not installed then you can install it by using this command:-

pip install opencv-contrib-python

Now we can Build our face recognization model.

akash_model.train(np.asarray(Training_Data), np.asarray(Labels))

At this point, we have built our model and now we can Run Our Facial Recognition model.

STEP 5: Detect Face using CascadeClassifier.

Using our created dataset we are going to recognize our own face by using the created model.

So now Pass faces the prediction model.

results = akash_model.predict(face)

results comprise of a tuple containing the label and the confidence value.

Now gaining the confidence of the detected face with our model.

confidence = int( 100 * (1 — (results[1])/400) )

If the gained confidence is greater than > 80%, we assume that the face is correctly detected by the same person we have created our dataset.

Now face is detected.

2.Sending automation mail through the detected face.

Using smtplib library it easier to send mail automatically.

so just install smtplib in your system.

pip3 install smtplib

just create smtplib object for gamail.com

smtpObj = smtplib.SMTP(‘smtp.gmail.com’)

Pass you creadential through obj. email( )

smtpObj.login(“your_@gmail.com”,”your_password")

subject=”your_subject”
body=”your_massage!!!”

and send the mail using sendmail( )

smtpObj.sendmail(“your_mail@gmail.com”, “your_friend_mail@gmail.com”, message)

Now mail sends successfully.

3. Sending automation Whatsapp massages through the detected face.

For Whatsapp massage just import pywhatkit Library

import pywhatkit as py

Take your friend's Mobile number and message and most important time.

number = “+91_mobile_number”
mess = “ Your_massage? “
time = hourse_time
mint = timeS_minuts

And send to the sendwhatmsg( )

py.sendwhatmsg(number,mess,time,mint)

WhatsApp message send successfully

4. Using AWS to save the recognized photo in EBS

Going ahead now I'm using AWS for saving the screenshot which is taken by an automated camera.

Here I'm using S3 simple AWS storage.

So when face is detected by our created model then this automated programme instantly launch the EC2 instance and S3 bucket storage.

Before going ahead first we have to import some libraries.

import subprocess as sp

By this set of code EC2 instance launched by instance_id.

instance_id = sp.getoutput("aws ec2 run-instances --image-id ami-0ad704c126371a549 --instance-type t2.micro --count 1 --key-name tryaws --subnet-id subnet-ba4ea5d1  --security-group-ids sg-0f02295aa9df10e89  --tag-specifications=ResourceType=instance,Tags=[{Key=Name,Value=CV_instances}]  --query Instances[*].[InstanceId] --output text")

After launching EC2 instance now we have to create S3 volume

vol_id = sp.getoutput("aws ec2 create-volume --availability-zone ap-south-1a --size 5 --tag-specifications ResourceType=volume,Tags=[{Key=Name,Value=CV_volume}] --query VolumeId --output text")

Here we have created S3 volume vol_id for storage so now attache this volume with the instance.

sp.getoutput("aws ec2 attach-volume --volume-id {} --instance-id {} --dev /dev/sdf".format(vol_id,instance_id))

Using sp.getoutput( ) method we sending the image data on created EC2 instance.

So we have seen 3 scenarios for the automation mechanism on face detection by using CV2.

Hope you find this article Helpful !!

For any query contact me by Linkedin Profile.

For Code, you can visit my Git-Hub Repo

Thanks for reading!!!

HappY Learing : )

--

--