DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • OpenCV Integration With Live 360 Video for Robotics
  • Facial Recognition and Identification in Computer Vision
  • From Polling to PubSub: Building an Asynchronous OPC UA Stack in Python
  • Introducing RAI Audit Kit: Evidence-Grade Responsible AI Audits in Python

Trending

  • Implementing Asynchronous Communication Between Microservices Using Kafka and Spring Boot
  • The New Insider Threat Isn't Human: Securing AI Agents Before They Secure Themselves
  • Two Clocks Are Running Out at Once, and Almost Nobody Is Watching Both
  • Context Rot: Why Your AI Agent Gets Worse the Longer It Works
  1. DZone
  2. Coding
  3. Languages
  4. Real-Time Face Recognition Using OpenCV, Dlib, and Python

Real-Time Face Recognition Using OpenCV, Dlib, and Python

A practical walkthrough for creating a Python-based face recognition app that identifies known individuals in real time using OpenCV, Dlib, and Python.

By 
venkataramaiah gude user avatar
venkataramaiah gude
·
Jul. 08, 26 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
50 Views

Join the DZone community and get the full member experience.

Join For Free

Face recognition has become one of the most widely used applications of artificial intelligence and computer vision. From smartphone authentication and smart surveillance systems to attendance management and access control solutions, facial recognition technology plays an important role in identifying individuals automatically. Advances in machine learning and image processing have made it possible to develop accurate face recognition systems using open-source tools and libraries.

This project demonstrates the implementation of a real-time face recognition application using Python, OpenCV, Dlib, and the Face Recognition library. The application captures video input, detects human faces, generates facial feature encodings, and compares them against a database of known individuals. Once a match is identified, the system displays the corresponding name on the video frame.

The project serves as an excellent example of practical computer vision implementation and provides a foundation for developing more advanced AI-based recognition systems.

Environment Setup

Before executing the application, it is important to prepare the development environment properly. Since the Face Recognition library depends heavily on Dlib, several prerequisites must be installed.

The first requirement is Python. Any recent Python version can be used, and users who already have Anaconda installed can use the existing Python environment.

The next step involves installing CMake. CMake is required to build and compile Dlib successfully. After downloading the latest stable Windows installer, the installation process should include adding CMake to the system PATH. Once installation is completed, the system should be restarted to ensure all environment variables are updated correctly.

Another important requirement is Microsoft Visual Studio Build Tools. During installation, the "Desktop Development with C++" workload must be selected. These build tools provide the necessary compiler and development libraries required by Dlib. After installation, a system restart is recommended.

Once the system prerequisites are installed, the Python libraries can be installed using the following commands:

Python
 
pip install cmake 

pip install opencv-python 

pip install dlib 

pip install face-recognition 


In some cases, Dlib installation may fail because of incomplete dependencies. Running the installation through an Anaconda Prompt with administrator privileges often resolves such issues. After installation, Dlib can be verified by importing it into Python and printing the installed version.

Python
 
#importing the required libraries
import cv2
import face_recognition
#capture the video from default camera 
webcam_video_stream = cv2.VideoCapture('images/testing/image1.mp4')
#load the sample images and get the 128 face embeddings from them
image1_image = face_recognition.load_image_file('images/samples/image1.jpg')
image1_face_encodings = face_recognition.face_encodings(image1_image)[0]
image2_image = face_recognition.load_image_file('images/samples/image2.jpg')
image2_face_encodings = face_recognition.face_encodings(image2_image)[0]

sen_image = face_recognition.load_image_file('images/samples/sen.jpg')
sen_face_encodings = face_recognition.face_encodings(sen_image)[0]

#save the encodings and the corresponding labels in seperate arrays in the same order
known_face_encodings = [image1_face_encodings, image2_face_encodings, sen_face_encodings]
known_face_names = ["Person1", "Person2", "Person3"]


#initialize the array variable to hold all face locations, encodings and names 
all_face_locations = []
all_face_encodings = []
all_face_names = []

#loop through every frame in the video
while True:
    #get the current frame from the video stream as an image
    ret,current_frame = webcam_video_stream.read()
    #resize the current frame to 1/4 size to proces faster
    current_frame_small = cv2.resize(current_frame,(0,0),fx=0.25,fy=0.25)
    #detect all faces in the image
    #arguments are image,no_of_times_to_upsample, model
    all_face_locations = face_recognition.face_locations(current_frame_small,number_of_times_to_upsample=1,model='hog')
    
    #detect face encodings for all the faces detected
    all_face_encodings = face_recognition.face_encodings(current_frame_small,all_face_locations)


    #looping through the face locations and the face embeddings
    for current_face_location,current_face_encoding in zip(all_face_locations,all_face_encodings):
        #splitting the tuple to get the four position values of current face
        top_pos,right_pos,bottom_pos,left_pos = current_face_location
        
        #change the position maginitude to fit the actual size video frame
        top_pos = top_pos*4
        right_pos = right_pos*4
        bottom_pos = bottom_pos*4
        left_pos = left_pos*4
        
        #find all the matches and get the list of matches
        all_matches = face_recognition.compare_faces(known_face_encodings, current_face_encoding)
       
        #string to hold the label
        name_of_person = 'Unknown face'
        
        #check if the all_matches have at least one item
        #if yes, get the index number of face that is located in the first index of all_matches
        #get the name corresponding to the index number and save it in name_of_person
        if True in all_matches:
            first_match_index = all_matches.index(True)
            name_of_person = known_face_names[first_match_index]
        
        #draw rectangle around the face    
        cv2.rectangle(current_frame,(left_pos,top_pos),(right_pos,bottom_pos),(255,0,0),2)
        
        #display the name as text in the image
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(current_frame, name_of_person, (left_pos,bottom_pos), font, 0.5, (255,255,255),1)
    
    #display the video
    cv2.imshow("Webcam Video",current_frame)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

#release the stream and cam
#close all opencv windows open
webcam_video_stream.release()
cv2.destroyAllWindows()        


Project Overview

The objective of this project is to identify known individuals appearing in a video stream. The system uses previously stored facial images as references. Each reference image is converted into a mathematical representation known as a facial embedding.

When a video frame is processed, faces are detected and converted into similar embeddings. These embeddings are compared with stored embeddings, and if a match is found, the person's name is displayed on the screen.

The workflow consists of the following stages:

  1. Load reference images
  2. Generate face encodings
  3. Capture video frames
  4. Detect faces in each frame
  5. Generate facial embeddings
  6. Compare embeddings
  7. Display recognition results

Loading Sample Images

The application begins by loading images of known individuals. These images act as training references for the recognition process.

The Face Recognition library provides a convenient function called load_image_file() which reads image files and converts them into arrays suitable for processing.

For each image, the system generates a 128-dimensional face encoding. These encodings capture unique facial characteristics and serve as the identity signature of an individual.

The generated encodings are stored in an array together with corresponding labels. Maintaining the same order between encodings and names ensures accurate identification later during matching.

Video Stream Processing

After loading the known faces, the application opens a video source. The video can be captured from a webcam or from a video file.

Each frame is processed continuously within a loop. Since facial recognition is computationally intensive, the frame is resized to one-quarter of its original size. This optimization significantly improves processing speed while maintaining sufficient accuracy.

Reducing image size helps the system achieve smoother real-time performance, especially on systems without dedicated graphics hardware.

Face Detection

Once a frame is resized, the system searches for human faces within the image.

The Face Recognition library internally uses Dlib's face detector to locate faces. The code specifies the HOG (Histogram of Oriented Gradients) model, which is known for its balance between accuracy and performance.

The detector returns coordinates representing the location of each detected face. These coordinates include the top, right, bottom, and left boundaries of the face.

Because detection is performed on a reduced-size image, the coordinates are multiplied by four to map them back to the original frame dimensions.

Face Encoding Generation

After face locations are identified, the application generates face encodings for each detected face.

A face encoding is a numerical representation consisting of 128 values. These values describe the unique characteristics of a person's face and allow efficient comparison between different faces.

The encoding process is one of the most important stages of the recognition pipeline because it transforms visual information into mathematical data suitable for machine learning comparison.

Face Matching

The generated encoding from the current frame is compared against the stored encodings of known individuals.

The compare_faces() function performs this comparison and returns a list indicating whether each stored encoding matches the current face.

If a match exists, the application retrieves the index of the matching encoding and uses that index to obtain the corresponding person's name.

If no match is found, the face is labeled as "Unknown Face."

This approach provides a simple yet effective mechanism for identifying individuals in real time.

Displaying Recognition Results

Once a face is identified, the application visually highlights the result.

A rectangle is drawn around the detected face using OpenCV. The recognized person's name is displayed near the face boundary using text rendering functions.

These visual annotations provide immediate feedback and allow users to observe recognition results directly within the video stream.

The video continues processing until the user presses the "Q" key to terminate execution.

Applications

The concepts demonstrated in this project can be applied to numerous real-world scenarios.

Common applications include:

  • Employee attendance systems
  • Smart access control
  • Security monitoring
  • Visitor identification
  • Automated authentication
  • Educational research projects
  • AI-powered surveillance systems

Organizations can integrate similar systems into existing infrastructure to improve security and operational efficiency.

Future Enhancements

Although the current implementation performs effectively, several improvements can be introduced.

Future enhancements may include:

  • GPU acceleration for faster processing
  • Deep learning-based face detection models
  • Multi-camera support
  • Cloud-based facial databases
  • Emotion detection
  • Face mask recognition
  • Attendance report generation
  • Integration with mobile applications

These improvements would increase scalability and accuracy while enabling deployment in larger environments.

Conclusion

This project demonstrates the practical implementation of real-time face recognition using Python, OpenCV, Dlib, and the Face Recognition library. By combining face detection, feature extraction, and facial matching techniques, the system successfully identifies known individuals appearing in a video stream. The installation process, while requiring several dependencies such as CMake and Visual Studio Build Tools, provides a stable environment for Dlib and facial recognition functionality. 

The project highlights how modern computer vision libraries can be used to build intelligent recognition systems with relatively simple code. It serves as an excellent learning platform for students, researchers, and software developers interested in artificial intelligence, machine learning, and image processing technologies.

Dlib OpenCV Python (language)

Opinions expressed by DZone contributors are their own.

Related

  • OpenCV Integration With Live 360 Video for Robotics
  • Facial Recognition and Identification in Computer Vision
  • From Polling to PubSub: Building an Asynchronous OPC UA Stack in Python
  • Introducing RAI Audit Kit: Evidence-Grade Responsible AI Audits in Python

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook