AI-Based Facial Recognition Using Raspberry Pi 5

Introduction

With the advancements in artificial intelligence (AI) and edge computing, Raspberry Pi 5 has become a powerful tool for various AI applications, including facial recognition. Whether you want to enhance home security, automate attendance tracking, or create a smart door lock system, Raspberry Pi 5 provides an affordable and efficient platform to implement facial recognition.

In this blog post, we’ll walk through the process of setting up a real-time facial recognition system using Raspberry Pi 5, OpenCV, and Python.


Why Use Raspberry Pi 5 for Facial Recognition?

The Raspberry Pi 5 is a perfect choice for facial recognition due to:

  • Improved Processing Power – Faster CPU and GPU for real-time processing.
  • AI & Machine Learning Capabilities – Supports TensorFlow Lite and OpenCV.
  • Compact & Energy-Efficient – Ideal for IoT and embedded AI applications.
  • Cost-Effective – More affordable than dedicated AI hardware.

Project: Real-Time Facial Recognition System

Components Required:

  • Raspberry Pi 5
  • Raspberry Pi Camera Module or USB Webcam
  • MicroSD Card (16GB or higher)
  • Power Adapter
  • Python and OpenCV Installed

Step 1: Setting Up Raspberry Pi 5

First, install Raspberry Pi OS and update the system:

sudo apt update && sudo apt upgrade -y

Then, install essential libraries:

sudo apt install python3-opencv libopencv-dev

Step 2: Installing Facial Recognition Libraries

We’ll use OpenCV and Dlib for face detection and recognition:

pip install opencv-python dlib face-recognition numpy

Step 3: Capturing & Training the System

We need to collect images of faces to train the model. Use the following script to capture face images:

import cv2
cam = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
i = 0
while i < 10:
    ret, frame = cam.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        face = frame[y:y+h, x:x+w]
        cv2.imwrite(f'face_{i}.jpg', face)
        i += 1
    cv2.imshow("Capturing", frame)
    if cv2.waitKey(100) & 0xFF == ord('q'):
        break
cam.release()
cv2.destroyAllWindows()

This script captures 10 images of a face, which will be used for recognition.

Step 4: Running Real-Time Facial Recognition

Now, we load the trained images and detect faces in real time:

import cv2
import face_recognition
import numpy as np

known_faces = []
names = ["User"]
for i in range(10):
    img = face_recognition.load_image_file(f'face_{i}.jpg')
    known_faces.append(face_recognition.face_encodings(img)[0])

video_capture = cv2.VideoCapture(0)
while True:
    ret, frame = video_capture.read()
    rgb_frame = frame[:, :, ::-1]
    face_locations = face_recognition.face_locations(rgb_frame)
    face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
    for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
        matches = face_recognition.compare_faces(known_faces, face_encoding)
        name = "Unknown"
        if True in matches:
            name = names[0]
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
        cv2.putText(frame, name, (left, top-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
    cv2.imshow("Face Recognition", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()

This script recognizes faces in real-time and labels them accordingly.


Expanding the Project

To enhance this system, consider:

  • Integrating AI-based facial emotion detection.
  • Connecting Raspberry Pi to a door lock system for smart access control.
  • Using cloud storage for recognized faces and logs.
  • Adding speech recognition for a voice-activated system.

Conclusion

Raspberry Pi 5, combined with OpenCV and face-recognition libraries, makes real-time facial recognition affordable and accessible. Whether you’re a beginner or an advanced user, this project opens doors to numerous AI applications, from security systems to smart assistants.

Share the Post:

Related Posts

Join Our Newsletter