Posted by Kosal
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.
pip install opencv-python face-recognition numpy
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()
cv2.VideoCapture(0)
.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.face_recognition.face_locations()
method. It then computes face encodings for the detected faces and compares them with the known encodings.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: