Face Recognition Program Using Python

Aditya Pande
5 min readJun 22, 2021

--

Task Description

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

📌 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.

đź“Ś When it recognize second face, it can be your friend or family members face.
👉 Create EC2 instance in the AWS using CLI.
👉 Create 5GB EBS volume and attach it to the instance.

This was a team task.

In this task, we have used Haarcascade as our face detection model which we have downloaded from below link:-

https://github.com/opencv/opencv/tree/master/data/haarcascades

But, if we want our computer will detect and recognize our face and accordingly do different tasks, then we have to create and train one model which will do face recognition. For doing this, we have to install library called “opencv-python” using “pip” command and then import “cv2” module in our program. One of the popular algorithm we have used is LBPH (Local Binary Patterns Histogram) that we can use to create face recognition model. But, for training this model we have to provide multiple images captured as training data to this model as we know that one image consists of multiple pixels which are equivalent to individual feature and one entire image is equivalent to one observation/record. For creating training data, we captured multiple images that will have our face detected which will be stored in some data path.

After creating training dataset, we first created model using LBPH algorithm using method called face_LBPHFaceRecognizer.create() but in order to do this, we have to install library called opencv-contrib-python using “pip” command. Then use “train()” to train our model using training dataset.

pip install opencv-contrib-python

Now, we will run face recognition program by testing our model such that if confidence score is greater than say 85%, then it will recognize our face and print one message. We want as soon as it recognizes the face, it will do the following:-

  • > First method will send image as attachment to recipient mail id and for doing this we first have to install “email” library using “pip” command and then import EmailMessage() method from it. We have added other details like subject, to, from and body of the mail to be sent along with image as attachment. As we know that image is nothing but a binary file that we have opened using “open()” and then read it. We have used os module in order to retrieve values environmental variables using “environ()” function that we have created in our system. Now, for connecting to SMTP mail server using SSL connection that works on port no. 465, we have to first install and import module called “smtplib” module and then use “SMTP_SSL()” function and specify mail server to which you want to connect along with its port no as arguments to this function. After that, send message using “send_message()” function.
  • > Second method will send whatsapp message to number that we will provide as input using “getpass()” function that comes under getpass module. But, in order to send whatsapp message to our friend, we have to first install one library called “pywhatkit” using “pip” command and use function called “sendwhatmsg_instantly()” which will open whatsapp web and send message specified as argument to recipient instantly.
  • > Third method will send SMS to specified phone number and for this, we have used one of AWS service called “AWS SNS” which is Simple Notification Service, a highly available, durable, secure, fully managed pub/sub messaging service that reliably deliver messages to the recipient’s phone no. specified. For, this we have to first register our phone no in this service. But, if you want to do this process using CLI, then for this we have to download and install AWSCLI software, then create one IAM user and run “aws configure” command to login using this user credentials by providing Access key and secret access key of this user. You can refer below article for this->

Now, from our python program if we want to connect to AWS SNS service, then we have to use a library called “boto3” but first we need to install it using “pip” command and use “client()” function and specify service and region name in which you are using this service. For sending SMS, use “publish()” function and provide phone no and message as argument to this function.

  • > Fourth method will send message to my telegram channel and for doing this I have created one file in which HTTP API url of the telegram bot that we have created is located. This URL consists of Chat id of sender, token of bot we created and text message we want to send to telegram channel. So, we have imported this file along with requests module in order to request a response from the server or URL using “get()” method.
  • > Now, if confidence score is less than specified value or we can say if it detect some other face apart from that face using which we have trained model, then it will create and launch EC2 instance and EBS volume in AWS console and also attach this volume to our instance. We can use AWS CLI commands for doing this but in our case, we have used provisioning tool called “Terraform” that we can install from URL:-

First, we have to specify it’s path in environmental variables and then we have used os module to run “terraform init” command which is used to initialize a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control. For running terraform file having (.tf) extension, we can use “terraform apply” command.

If none of face is detected and recognized by model, then it will finally print message as “NO face found.” I have uploaded my code on below specified GitHub URL.

You can also refer below video for more clear view and see entire practical working of program.

Thanks for reading!!

--

--

No responses yet