Automation of email and WhatsApp messages with face detection

Abhishek Kumar
3 min readJun 24, 2021

In this article we will see how to do different action based on the face detection like : sending mails, whatapp messages or creating an EC2 instance in AWS.

1. 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 library

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.

Thank You for reading the article !!! :)

--

--