Python | Multiple Face Recognition using dlib

Last Updated : 26 Jun, 2026

Dlib is a popular machine learning and computer vision library used for face detection and face recognition. It can identify multiple faces in an image by detecting facial features and comparing them with known face encodings.

The multiple face recognition process involves a series of steps to identify people present in an image or video.

  1. Face Detection : The system first scans the image and detects all human faces present in it. A bounding box is created around each detected face.
  2. Feature Extraction : For every detected face, important facial features such as the eyes, nose, mouth, and facial landmarks are identified.
  3. Face Encoding : The extracted features are converted into a numerical representation called a face encoding. This encoding acts as a unique signature for each face.
  4. Face Comparison : The generated face encodings are compared with the encodings of known individuals stored in the database.
  5. Face Recognition : If a matching encoding is found, the corresponding person's identity is retrieved. If no match is found, the face is labeled as Unknown.
  6. Display Results : The recognized person's name is displayed along with a bounding box around the face, allowing multiple faces to be identified simultaneously.

Implementation

1. Import Required Libraries

  • face_recognition: Used for face detection and recognition.
  • OpenCV (cv2): Used for image processing and drawing labels.
  • NumPy: Used for numerical operations.
  • cv2_imshow: Displays images in Google Colab.
Python
import face_recognition
import cv2
import numpy as np
from google.colab.patches import cv2_imshow

2. Load Training Images

These images act as the training data for face recognition.

Python
person1_image = face_recognition.load_image_file("Your Image path for person 1")
person2_image = face_recognition.load_image_file("Your Image path for person 2")

3. Generate Face Encodings

  • Face encodings are generated for each known face.
  • A face encoding is a numerical representation of facial features that helps compare and identify faces.
Python
person1_encoding = face_recognition.face_encodings(person1_image)[0]
person2_encoding = face_recognition.face_encodings(person2_image)[0]

4. Store Known Faces

  • The face encodings and corresponding names are stored in separate lists.
  • These lists form the face database used for recognition.
Python
known_face_encodings = [
    person1_encoding,
    person2_encoding
]

known_face_names = [
    "Kuldeep",
    "Ashish"
]


5. Load the Test Image

Since OpenCV reads images in BGR format and face_recognition uses RGB, the image is converted accordingly.

Python
image = cv2.imread("Your test image path")
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

6. Detect Faces in the Image

  • Detects all faces present in the image.
  • Generates encodings for each detected face.
Python
face_locations = face_recognition.face_locations(rgb_image)
face_encodings = face_recognition.face_encodings(
    rgb_image,
    face_locations
)

7. Compare Faces and Identify People

  • If a match is found, the corresponding name is assigned.
  • Otherwise, the face is labeled as Unknown.
Python
for face_encoding in face_encodings:

    matches = face_recognition.compare_faces(
        known_face_encodings,
        face_encoding
    )

    name = "Unknown"

    if True in matches:
        first_match_index = matches.index(True)
        name = known_face_names[first_match_index]

    print(name)

Output:

Kuldeep

Ashish

8. Draw Bounding Boxes and Labels

  • Draws a green rectangle around each detected face.
  • Displays the recognized person's name above the face.
Python
for (top, right, bottom, left), face_encoding in zip(
        face_locations,
        face_encodings):

    matches = face_recognition.compare_faces(
        known_face_encodings,
        face_encoding
    )

    name = "Unknown"

    if True in matches:
        first_match_index = matches.index(True)
        name = known_face_names[first_match_index]

    cv2.rectangle(
        image,
        (left, top),
        (right, bottom),
        (0, 255, 0),
        2
    )

    cv2.putText(
        image,
        name,
        (left, top - 10),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.8,
        (0, 255, 0),
        2
    )

9. Display the Result

Python
cv2_imshow(image)

Output:

output
Output

Download full code from here

Comment

Explore