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. With the help of this technology, I created an alert system which recognizes the face when someone tries to access the system and if it founds any suspicion then it alerts the owner by sending an email and starting the alarm.
I used the Windows Operating System for the creation of this project and used PyCharm IDE for the coding purpose.
Why I chose Windows?
Initially, when I started building this project, I faced a lot of issues in Ubuntu on the Virtual Box, and Memory was one of the major issues. So I switched to Windows from Ubuntu.
You can also use Linux if you are comfortable with it.
So let's move on and set up all the software requirements
Software set-up
Software set-upStep 1: Python installation
Let's first install Python on your PC.
If you are having python-3.x installed in your PC, you can skip to next step. And make sure the version should be 3.x(3+).
Go to the below link and Download python 3.7 and Install it.
https://www.python.org/ftp/python/3.7.1/python-3.7.1-amd64.exe
Now if you are done with the installation, let's take a step ahead and install the PyCharm IDE which makes coding very simpler.
Step 2: IDE installation and Configuration
PyCharm is an integrated development environment used in computer programming, specifically for the Python language. It is developed by the JetBrains.
If you are a Student there is some bonus for you. You can sign in to JetBrains using your college E-mail and you will get a one-year free subscription.
Go to the below link to download the IDE with Community version.
https://www.jetbrains.com/pycharm/download/#section=windows
When you are done with downloading follow the Steps given below.
NOTE: Do not try to mess with any settings which you don't know about. 😂
- Double click on the .exe file to open it.
- Select next till you find the location page, configure the location where you want to install it, I prefer you to keep it default.
- Click next till you find the install option.
- Click on Install.
- It will take 15-20 minutes to install the IDE (varies with the processor)
After installation, you can configure the IDE if you want or skip to the creation of the project.
Before moving on to the coding part we have to download some libraries which support face detection and the Bolt IoT module.
Installing python packages in PyCharmAfter the creation of the project let's install the libraries, follow the below steps.
NOTE: Make sure you have a working Internet connection.
- Click on the File option that you will find inside the PyCharm IDE on the top left corner or you can directly open it by keyboard shortcut Ctrl + Alt + s.
- After opening the setting menu, you will find an option Project with your project name, expand it by clicking the arrow.
- On expanding it, click on Project Interpreter.
- On that window, you will see a plus sign (+), click on it, a new menu will appear of all the Available Packages that can be installed.
- "Important" search of all the below-listed packages and install them one by one SEQUENTIALLY.
- opencv-python
- face_recognition (this will take time, keep patience)
- boltiot
If you face some issues while installing the packages, I suggest you search the error on Google.
If you are getting an error while installing from PyCharm, install it from the terminal using pip.
Example:
pip install face_recognition
pip install <module_name>
After you are done installing the libraries, now it's time to move to Coding Section.
CodeThe code is divided into four segments.
- Detecting Face.
- Verification of the Face.
- Switching ON the alarm (Incase any Unknown face detected).
- Sending the Mail.
Detection of the Face
Let's start with face detection first. Where the job is done by OpenCV.
The program will give you some hands-on to understand how each frame is captured in live video and the face is detected.
We will use the face_recognition library in this project which is based on the art face recognition(deep learning library). It says that the model provides an accuracy of 99.38 %.
Here you can find the full explanation of how this technology works.
https://pypi.org/project/face_recognition/
We can put numbers of Images in the dataset.
First let take an image that we already have and creates it face encoding, to do this we will use face_encoding function provided in the face_recognition package.
image = face_recognition.load_image_file("image.jpg") '''This loads the image'''
image_face_encoding=face_recognition.face_encodings(image)[0] '''Creates a face encoding'''
Once we have the face encoding we will take the input image from the camera and verify it with the image(s) present in the dataset.
You can go through this example of face recognition provided by Ageitgey which is fast and accurate used for real-time video.
Once we are done with it, Let's jump to the next segment and configure the alarm.
Buzzer Alert
We will now keep the hardware setup ready. We will be using buzzer as an alert module in our home system so that if an intruder tries to unlock then it will buzz.
The positive terminal (Longer length) of the Buzzer is input, connected to the '0' pin of the Bolt IoT module, and the negative terminal(shorter length) is to be Ground.
Here is the Schematic Diagram of the connection to be made.
Here is how the Setup looks like after the connection.
After all the connections were made now it's time to connect our code to the device.
Before that create a conf.py file which will store all the details about the API Keys and Device ID.
To get the Device ID of the Bolt IoT Module, go to this link https://cloud.boltiot.com/home/
Once you have the Device ID add it to the conf.py file. Here is the code snippet.
device_id = "BOLTXXXXXXX"
To get the API key of the Bolt IoT Module, go to this link.
https://cloud.boltiot.com/api_credentials
Once you have the API Key add it to the conf.py file. Here is the code snippet.
api_key = "XXXXXXXXXXXXXXXXXXX"
Now we have all the credentials So let's connect the Device to the code.
First, link the conf.py file to the main code file. To do this we have to import the conf.py file.
import conf
Now it's time to connect to the Bolt IoT Module.
To connect this we will use the Bolt from boltiot package
First import the Bolt module.
from boltiot import Bolt
mybolt = Bolt(conf.api_key, conf.device_id)
Here is the code Snippet which will only run when the system detects some unknown face.
print("Checking device status.... ")
response = mybolt.isOnline()
data = json.loads(response)
print("Device is ", str(data["value"]))
if "online" == str(data["value"]):
print("Sending request to Alarm")
response = mybolt.digitalWrite('0', 'HIGH')
Bolt Python Libraries Documentation. Here you will find the documentation all the API's provided by the Bolt.
Once you have done connecting the Bolt IoT module, So it's the time to move the final section sending alert via Email.
Sending Alert Via Email
For this segment, we will be using mailgun, which provide mail service by API call.
Before that create an account on mailgun.
Verify your email after that you will able to use their services.
Once you are done with the verification process, let's get all the credentials.
First, we need the Sandbox URL. To get this open your mailgun dashboard.
Scroll down and you will find it.
Add the URL to the conf.py file.
SANDBOX_URL= "sandboxxxxxxxxxxxxxxxxxxxxxx.mailgun.org"
To get the API key go to the following link.
Add the API key to conf.py file.
MAILGUN_API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Now add two more things to the conf.py file
- Sender email
SENDER_EMAIL = "test@SANDBOX_URL"
Replace the SANDBOX_URL with the URL you copied.
What it will look like.
SENDER_EMAIL = "test@sandboxxxxxxxxxxxxxxxxxxxxxx.mailgun.org"
- Recipient email
it is the mail you want to receive the Alert message.
RECIPIENT_EMAIL = "xxxxxx@gmail.com
Once you are done with the credentials setup conf.py file looks something like this:
api_key = "XXXXXXXXXXXXXX"
device_id = "BOLTXXXXX"
MAILGUN_API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
SANDBOX_URL= "XXXXXXXXXXXXXXX.mailgun.org"
SENDER_EMAIL = "XXXXXXXXXXXXXXXXXXXXXXX.mailgun.org"
RECIPIENT_EMAIL = "XXXXX@gmail.com"
Now import the Email module from boltiot package
from boltiot import Email
mailer = Email(MAILGUN_API_KEY, SANDBOX_URL, SENDER_EMAIL, RECIPIENT_EMAIL) '''Create object to send Email'''
mailer.send_email("Subject", "Email Body Content")''' Call function to send Email'''
Here is the code snippet to send the email.
mailer = Email(conf.MAILGUN_API_KEY, conf.SANDBOX_URL, conf.SENDER_EMAIL, conf.RECIPIENT_EMAIL)
response = mailer.send_email("Intruder Alert", "Someone tried to access your system")
response_text = json.loads(response.text)
print("Response received from Mailgun is: " + str(response_text['message']))
Now we are all set and ready to test our alert-system.
To run the program right click on the code file and click on run.
Or you can also run by the terminal by the following command
For windows
$py code.py
For Linux
$python code.py
Make sure you have already set up the environment variable and project directory contains code.py
file
Here you can find the whole code of the project.
If you have any queries regarding the project you can contact me on my Email.
ALL THE BEST!
Comments