Camkode
Camkode

Building a Real-Time Face Recognition System Using Python, OpenCV, and face_recognition

Posted by Kosal

Building a Real-Time Face Recognition System Using Python, OpenCV, and face_recognition

In this article, we'll explore how to build a real-time face recognition system using Python, OpenCV, and the face_recognition library. This project leverages powerful machine learning algorithms to identify and label faces in a video feed. The complete code is provided below, along with a detailed explanation of how it works.

Install packages:

pip install opencv-python face-recognition numpy

The Code Explained

Here's the complete code for the face recognition system:

import face_recognition
import cv2
import numpy as np

# Initialize the webcam
video_capture = cv2.VideoCapture(0)

# Load and encode known faces
kosal_image = face_recognition.load_image_file("kosal.jpg")
kosal_face_encoding = face_recognition.face_encodings(kosal_image)[0]

lika_image = face_recognition.load_image_file("lika.jpg")
lika_face_encoding = face_recognition.face_encodings(lika_image)[0]

# Store the encodings and corresponding names in arrays
known_face_encodings = [
    kosal_face_encoding,
    lika_face_encoding,
]
known_face_names = [
    "Kosal",
    "Molika",
]

# Initialize variables for face recognition
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    # Capture a frame from the webcam
    ret, frame = video_capture.read()

    if not ret:
        print("Failed to grab frame")
        break

    # Process every other frame for performance
    if process_this_frame:
        # Resize the frame to 1/4 for faster processing
        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

        # Convert BGR image to RGB
        rgb_small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)

        # Detect faces and get encodings
        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:
            # Check if the face matches any known faces
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"

            # Find the closest match
            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

    # Draw boxes around faces and label them
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale the coordinates back to the original 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)

        # Label the face with a name
        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 video feed
    cv2.imshow('Video', frame)

    # Break the loop on 'q' key press
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the webcam and close windows
video_capture.release()
cv2.destroyAllWindows()

How It Works

  1. Video Capture Initialization: The script starts by capturing video from the default webcam using cv2.VideoCapture(0).
  2. Face Encoding: Images of known individuals (kosal.png and lika.jpg) are loaded and encoded using the face_recognition library. These encodings are stored in arrays, which will be used to compare faces detected in the video feed.
  3. Processing Frames: The video is processed frame by frame. To enhance performance, only every other frame is processed. The frames are resized to a quarter of their original size to speed up the face detection and recognition process.
  4. Face Detection and Recognition: The script detects all faces in the frame using the face_recognition.face_locations() method. It then computes face encodings for the detected faces and compares them with the known encodings.
  5. Matching Faces: The detected face encodings are compared with the known face encodings to find the best match. The name corresponding to the best match is then assigned to the face.
  6. Displaying Results: The script draws rectangles around detected faces and labels them with the corresponding name. The processed frames are displayed in a window using OpenCV.
  7. Exit Condition: The loop runs until the 'q' key is pressed, after which the video capture is released, and all OpenCV windows are closed.

Building a real-time face recognition system using Python, OpenCV, and face_recognition is both straightforward and powerful. With minimal code, you can create applications that can recognize and label faces in real time. This system can be extended to recognize more faces by adding additional face encodings and names to the arrays.

This project is a great starting point for anyone interested in computer vision and machine learning. It demonstrates how easily these technologies can be integrated into practical applications.

References:

  • OpenCV: https://pypi.org/project/opencv-python/
  • NumPy: https://https://numpy.org/
  • Face Regnition: https://pypi.org/project/face-recognition/