Tech Updates

How to Make A Face Recognition Software App

Today, I am writing this article to discuss how to make a face recognition software in Python using the OpenCV library,...

Written by Niel Patel · 9 min read >
how to make face recognition software

One of the latest outputs of artificial intelligence and machine learning is facial or face recognition software or application.

Facial recognition systems are slowly but surely infiltrating our daily lives. They can pick you out of a crowd and identify you as a person, which can lead to a variety of outcomes. They can unlock your phone screen, pay for your purchasing, get you imprisoned, mark your attendance in the office, and can do more things beyond our thinkings. And I’m developing a facial recognition algorithm when my facial recognition software will even recognize the picture you think. And will help you draw your fairy queen. Make fun of part one Let’s go back to the main topic.

There are two common methods to use create facial recognition software or application

  1. This first method is uses of pre-trained models such as dlibDeepFaceFaceNet, and others. So today we will discuss this method. It will take less time with efforts.
  2. The second method to make a face detection software is create a neural network from the ground up. This approach is appropriate for multi-purpose face recognition systems with complicated features. It involves more time and effort, as well as millions of photos in the training dataset, as opposed to a pre-trained model, which only requires thousands of images in transfer learning.

Biggest Artificial Intelligence Companies in the World

Today, I am writing this article to discuss

How to make a face recognition software in Python using the OpenCV library, DLib

But how do these face recognition software work in practice, how accurate are they, and who has the authority to use them in the public portals? Let’s make one ourselves using free, open-source software and photos of pals to find out. Let me keep telling you, these types of facial recognition software install in private areas like corporate offices.

Lets make list of everything we need to make a face recognition software

  1. You need to download the Sublime Text When the download is complete, open the text editor Sublime Text and continue the installation instructions. This is where we’ll create the code that will eventually turn into the software.

2. Check to determine if Python 3 is already installed on your machine. This is the most recommended programming language to make the best image recognition software in python. This is the language in which we will create our code. Open Terminal (under the utility folder in Applications) or Command Prompt (click Windows + X and select Command Prompt) on a Mac or Windows computer to achieve this. Then press enters to type the following:

3. Return to Terminal or Command Prompt and type the commands below, pressing enter after each line to install the required packages. It can take a bit to get to the fourth line, dlib.

pip3 install cmake
pip3 install face_recognition
pip3 install numpy
pip3 install dlib
pip3 install opencv-python

I’ll continue this code setup in below Section 2

Before Knowing how to make a face recognition software lets go through the Face detection and recognition process

The facial recognition process starts with a camera application installed on any device. The application is written in Golang and runs as a local console app on both Raspbian and Ubuntu. The application must be set up using a JSON config file with the Local Camera ID and Camera Reader type when it is first launched.
After that, this program can employ computer vision and a deep neural network to discover a potential face inside its stream.

There are two basic methods for doing so that are both effective:

  1. The TensorFlow object detection model
  2. Caffe face tracking

Both of these solutions have proven to be effective and are now included in the OpenCV library.

Once a face has been taken by facial recognition software, the cropped image will be sent to the back end via an HTTP form-data request. The API then saves this facial image on the local file system as well as in the detection log, along with a personID.

Image 2 : Euclidean distance

how to create face recognition software in python

On the back end, an algorithm finds records with the value ‘classified=false’ and uses the Dlib function to create a 128-dimension vector describing the qualities of this face. The programme then uses Euclidean distance to compare this vector to all of the facial records in the database to see if this new face matches any previously recorded faces.

how to make a face detection algorithm

Pictured above is a representation of the Dlib function’s code, with the index points corresponding to parts of the face. using the above algorithm, we can create a face recognition software that detects face more than 80% correct. This is just a demo code to represent the structure.

Know the Face Recognition Stages to Make Face Recognition Software

To make a mobile app with face recognition technology, you must first figure out what steps you need to do. What are the several steps of the identifying procedure?

  • Face detection at the start. The system must detect the face in the image in the first stage. This isn’t about identifying a person yet; it’s just about the fact that you’re staring at a human face. And if the notion of making face detection software appeals to you, and you don’t require anything more advanced, the procedures.
  • Determination of Reference Points There’s a lot more sensitive work to be done now. The program looks for reference points (features) on the face that can be used to determine a person’s unique qualities. Previously, the eyes were thought to be the primary reference point, but today’s computation technology has advanced to encompass over 70 crucial aspects.
  • Building a Frontal Face The face should be changed to obtain its frontal picture to make the process of matching things easier.
  • Calculation of Feature Descriptors Now the descriptor, which is a set of traits that describe a person’s face independent of age, hairstyle, or other factors, is being determined. To be more particular, we’ll need a specific digital portrait, a face vector, in order to perform a more accurate comparison.
  • Comparison of the face. The procedure of matching the generated digital face vector with the items contained in the database is the fifth stage.
  • Face Recognition is the final identification of a person (or, more precisely, his or her face) when a match has been found.

Section 2:

How to create face recognition software in python

pip3 install cmake
pip3 install face_recognition
pip3 install numpy
pip3 install dlib
pip3 install opencv-python

If the final command asked you to install Xcode (for Macs), go to the App Store and install. This may take a while as it is quite large. Then re-type the last line (pip3 install OpenCV-python) and press enter.

Making and Running the Application we Installed

1. Copy and paste the below code in an open source piece of code into Sublime Text.

#code forked and tweaked from https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
#to extend, just add more people into the known_people folder

import face_recognition
import cv2
import numpy as np
import os
import glob

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)

#make array of sample pictures with encodings
known_face_encodings = []
known_face_names = []
dirname = os.path.dirname(__file__)
path = os.path.join(dirname, 'known_people/')

#make an array of all the saved jpg files' paths
list_of_files = [f for f in glob.glob(path+'*.jpg')]
#find number of known faces
number_files = len(list_of_files)

names = list_of_files.copy()

for i in range(number_files):
    globals()['image_{}'.format(i)] = face_recognition.load_image_file(list_of_files[i])
    globals()['image_encoding_{}'.format(i)] = face_recognition.face_encodings(globals()['image_{}'.format(i)])[0]
    known_face_encodings.append(globals()['image_encoding_{}'.format(i)])

    # Create array of known names
    names[i] = names[i].replace("known_people/", "")  
    known_face_names.append(names[i])

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    # Grab a single frame of video
    ret, frame = video_capture.read()

    # Resize frame of video to 1/4 size for faster face recognition processing
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_small_frame = small_frame[:, :, ::-1]

    # Only process every other frame of video to save time
    if process_this_frame:
        # Find all the faces and face encodings in the current frame of video
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

        face_names = []
        for face_encoding in face_encodings:
            # See if the face is a match for the known face(s)
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"

            # # If a match was found in known_face_encodings, just use the first one.
            # if True in matches:
            #     first_match_index = matches.index(True)
            #     name = known_face_names[first_match_index]

            # Or instead, use the known face with the smallest distance to the new face
            face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
            best_match_index = np.argmin(face_distances)
            if matches[best_match_index]:
                name = known_face_names[best_match_index]

            face_names.append(name)

    process_this_frame = not process_this_frame


    # Display the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

    # Display the resulting image
    cv2.imshow('Video', frame)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()

2. Save this file with the above-written algorithm as recognize face.py in your system, and then create a folder called known people (make sure this is also in the Desktop folder).

3. Now we may add photographs of people we want to identify to the known people folder by saving an image of their face. Make sure the individual in the photograph is facing forward and is the only one in the picture (crop if necessary). Each file is given the name of the person as we want it to appear on the screen. Only.jpg files will be used in this scenario.

4. To execute the application, return to the Terminal (Mac) or Command Prompt (Windows) and type the following and click enter.

cd Desktop
python3 recognise_face.py

<---- if an error message appears try:----!>

python recognise_face.py


Pressing Ctrl + c with the Terminal window selected will exit the programme.

To summarise, the face recognition process can be broken down into four steps.

  1. Detect any faces which appear in the frame.
  2. To make a mask, find precise spots on the face, such as the tip of the nose and the corners of each eye as explain in Image 2 : Euclidean distance. Then, using only changes that preserve the original shape, such as rotation and zooming, reposition that mask so that it is pointing straight forward.
  3. The coding process. This stage entails finding essential features of a face (through computer vision) that will be similar in any photo of the same person but different in any photo of someone else.
how to make face recognition software in python

Face recognition programs on the world stage

Bloomberg estimates that the global facial recognition market will be worth $7.76 billion by 2022. (from 4.05 billion dollars in 2017).

In this regard, it would be fascinating to learn where in the globe people make the most money from facial recognition software (apps, websites, and other programs). The graph below provides an answer to the question.

face recognition software demand year by year

Ways of identifying a person’s face in face recognition software

  • 2D recognition is a popular approach to face recognition right now. It is based on the comparison and use of two-dimensional pictures.
  • Recognition in three dimensions. The method is becoming more popular, despite the fact that it is still significantly inferior than the prior one. It takes advantage of the ability to rebuild three-dimensional images. Apple’s Face ID is one of the most well-known 3D scanners.
  • In a controlled environment, face recognition is performed. The recognition model is straightforward; it presupposes a motionless and controllable background. In this situation, a face recognition app will have no trouble separating individual elements (such as the eyes, nose, and mouth) and recreating the entire item (a human face).
  • Face recognition based on colour. The software examines the image for areas of normal skin colour and attempts to capture, analyse, and identify facial segments.
  • Face recognition based on skin texture. We’re dealing with high-resolution photos that enable for a detailed assessment of the skin’s texture, including lines, wrinkles, and pores.
  • Motion-based face search It has to do with video images that have a motion effect. The software should check for specific reference points in this situation, such as blinking eyes, brows, nose, forehead, or lips. The algorithm attempts to model and identify the face as soon as any of these elements are identified, and compares it to those in the database.
  • Face recognition using thermal imaging. In recent years, thermal imaging cameras have become increasingly popular for identifying human faces. Furthermore, the area is constantly evolving from year to year.

How to make a mobile app for face recognition?

The most important question is undoubtedly how to make face recognition software in python. What resources would you require to bring your concept to live?

There are many options, we’ll describe the 3 most popular of them.

Native APIs

The most straightforward way to construct a face recognition app for Android or iOS is to use Apple and Google’s Native APIs. It is inexpensive, simple to execute, and does not necessitate any additional fees or effort. Of course, there won’t be a lot of features, but who knows, maybe just enough to get the job done.

Third-party solutions

Make a face recognition software is an easy-using third-party solution is the second alternative. And, let us say, the breadth of these tools’ options will astound you: many firms have APIs that may be used to build a face detection app. What do you think of these?

  • A Visual Recognition API is one of Amazon’s many development offerings (which also include a shopping platform and cloud solutions). Furthermore, both paid and free choices are available.
  • The Luxand.cloud API enables developers to create apps that can assess human sex, age, emotional condition, and more. BASIC, PRO, and ULTRA are the three price packages offered by the company.
  • FaceMark is a straightforward and dependable API solution that gives a plethora of reference points for recreating the entire face and profile, as well as ease of use and a variety of price options.

How Deep Learning Makes Face Recognition Software Error Free

One of the most innovative approaches to improving facial recognition technology is deep learning. Face embeddings are extracted from photos with faces. Distinct faces will have different facial embeddings. And the most efficient method to accomplish this is to train a deep neural network.

In the long run, though, if the facial recognition system has unique features, it may be the best option. The following are the most important considerations.

  • The correct selection of CNN architecture and loss function
  • Inference time optimization 
  • The power of a camera and other hardware

Leave a Reply

Your email address will not be published. Required fields are marked *