• Abhishek Singh

Unleash the power of Face i.e Facial recognition

In this blog post, we are going to make a facial recognition project with the help of python, open cv, SQLite and flask as backend.


Hello everyone, I welcome all of you on this amazing tutorial where you would learn about the power of facial recognition. In this world facial recognition plays an important role in computer vision fields. It has to have a variety of applications in the field of the security line, attendance based on facial recognition. By using facial recognition we are taking care of our privacy and safety. By using this technique we can also monitor the unusual activity happening in any part of the city and we can report to the police. In this blog post, we are going to make a web-based system in which we can login through or face.

Let us break down the things one by one

Our project structure consists of mainly three parts

1- Facial recognition – In this part, we are going to compare the faces that is available in our database if it is matched then we can proceed to the main page.

2- Creating the database- In this part, we are going to create a database, delete an existing user, add new user images and email details to our database.

3- Web-app design—In this part, we are going to design our web interface(by using Html, CSS, flask) and create a database.


Basic programming knowledge

Python- It is the most simple language that is ever built. We are using python a lot on this project. You must have a basic understanding of python so you can understand the code very easily. Note- High-level programming is not required basic understanding is enough.

Installed tools

1- For this program, we will need python installed on your computer

2- We have to also install OpenCV, numpy, flask, TensorFlow, Keras and sqlite3 to run our program

pip install numpy open-cv flask tensorFlow keras sqlite3

Numpy- This library is used for numerical computation and it provides tools for working with these arrays.

OpenCV- It is used to load an image, how to display it and how to save it back.

Flask- It is a lightweight web application framework. It has become one of the most popular web application frameworks. In our project, we need a high-level understanding of how the flask code is written.

Tensorflow- The most famous deep learning library released by Google. It is a computational framework for building machine learning models.

Keras- It is used to construct our neural network architecture.

Sqlite3- It is used to create a database or connect to an existing database. With the help of sqlite3, we can add records, delete records or update a record in a database.

Part 1- Facial recognition

In this part, we are going to write code for detecting faces from the webcam and save it in database

Now open the file and paste the code. In this file, we are defining two functions detect_face and detect_face_realtime.

In this function, we are detecting faces from a video stream if there are any face found we capture the image and save it and draw a rectangular box around it. Now we display the frame with a bounded rectangle.

For detecting face we are using haarcascade_frontalface_default.xml file( this is a haar cascade classifier that is used to detect a particular object from the source or video).

In the next function which is detected the face from real-time, we are doing the same thing as above but we will add one thing extra that is displaying the name of the person also. If the person's face is not presented in our database we will display an unknown face found.

Part 2- Initialise the database

In this part, we are going to write code for initializing the database and adding a user in the database.

Step 1- Create a custom loss function for our model.

Step 2- First we have to load our face recognition model that we save in our h5 file.

Step 3- Now we load or database if any available else we have to create our new database.

Step 4- Now we define a function so that we can remove any existing user from the database.

Step 5- Next we define another function in which we add a new user face or image to the database if the face is already present in the database we popped a message showing the user is already registered to try a different name.

Step 6- Next we define a function that adds a user to the webcam. If the face has been detected then we save the image is saved in our image folder. After that, we go inside the if statement and check if the email is already present or not. If it is already present then we display the message The user is already registered try different name. Else we add the image path to our database.

If the face is not detected then we display the message “Try again”.

Part 3- Designing our web application using flask

Now we design our web application using flask.

Step 1- The first step is to import all the libraries that are required

Step 2 - Now initialize our flask application and set model equal to none and user database to none and define the folder where we have to save the image

Step 3- To detect our face boundary load our harr cascade XML file

face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')

Step 4- Now we define our custom loss function having parameters true value, prediction value and alpha and now we apply our triplet loss formula and return the value of the loss.

Step 5- Now we define a function named face_present which will check whether a face is there in image or not. It will return true if a face is found and saves a crop bounded picture in our static folder otherwise it will return false.

Step 6- The next step is to load our face net model from the model's folder. That h5 file contains the saved weights of the images along with we also call our custom loss function. And then print the summary.

Step 7- Now load our saved user database that we make in the previous part. In this, we initialize the user_db as a global variable. If a database is already present then we open that database and load it otherwise create a new database.

Step 8- Now we define face_recognition function in which we gave an input face and check if it is a registered user or not. This is done by looping over all the database and checking for that encoding if it is present then print “Hi Abhishek” else print User is not in our database

Step 9- After doing face recognition now we add an unknown person or new user to the database. This can be done by creating image encoding on the face that is present in the image and then return the data and encoding.

Step 10 – Now we use render_template function to call our dashboard page, same for index page and login page also.

Step 11- The next step is to verify a user by asking their email id and password if it is matching to our database then we show the message that the user is logged in else we pop a message displaying the wrong password and ask again to enter a password.

Step 12- In this, we turn the session to false so that the user can logout and return them to the main page.

Step 13- Now we use render_template to call the sign-up page

Step 14- On this page, we add the user through the sign-up form. In this user enter her details like email, password, and name. If the user is new then we add a new user face and save in a saved image folder and if an image there is no face then we set user_status to false. If the face is present then we add the details to our database and commit it successfully an return the sign up page and ask the user to login.

Step 15- Now the last step is to make a predict function that will read the image in PIL format and save the image on the server-side. Now we apply facial recognition to check if the user is valid or not. If it true then we indicate the request as success else we popped a message displaying an unknown person. Now we return the data dictionary as a JSON response. Then we call the main function to load the model and start the server.

Note that we are running it on host= and port 5000.


This application can be used in many areas like facial attendance in college or school, also used for security purposes to identify the unknown person. I hope you will like this blog post if you have any question feel free to ask I will try my best to resolve your errors.