Face Recognition Using OpenCV and Raspberry Pi: A Comprehensive Guide

Fahiz
3 min readSep 5, 2024

--

Face recognition has become a popular application in various fields, from security systems to personal gadgets. With advancements in computer vision libraries like OpenCV and affordable computing power from devices like the Raspberry Pi, creating a face recognition system is more accessible than ever. In this post, we’ll guide you through setting up a face recognition system using OpenCV on a Raspberry Pi.

What You’ll Need

Before we dive into the code, ensure you have the following:

  • Raspberry Pi (preferably with 4GB RAM or more)
  • Raspberry Pi Camera Module or a USB webcam
  • MicroSD Card (with Raspberry Pi OS installed)
  • Power Supply for the Raspberry Pi
  • Keyboard, Mouse, and Monitor (for initial setup)
  • Internet Connection (for installing libraries)

Step 1: Set Up Your Raspberry Pi

  1. Install Raspberry Pi OS: Download the Raspberry Pi Imager from the official website and flash the OS onto your MicroSD card.
  2. Boot Your Raspberry Pi: Insert the MicroSD card, connect the peripherals, and power on the Raspberry Pi.
  3. Update and Upgrade: Open a terminal and run:
sudo apt-get update
sudo apt-get upgrade

Step 2: Install OpenCV on Raspberry Pi

OpenCV is a powerful library for image processing. Installing it on the Raspberry Pi requires a few steps:

  1. Install Dependencies:
sudo apt-get install libhdf5-dev libhdf5-serial-dev libatlas-base-dev libjasper-dev libqtgui4 libqt4-test
sudo apt-get install libilmbase-dev libopenexr-dev libgstreamer1.0-dev

2. Install OpenCV:

pip install opencv-python
pip install opencv-python-headless

3. Test the Installation: Open Python and try importing OpenCV:

import cv2
print(cv2.__version__)

You’re good to go if no errors appear and the version number is displayed!

Step 3: Capture Images with the Raspberry Pi Camera

You’ll first need to capture images using the camera module to recognize faces.

  1. Set Up the Camera: Ensure the camera is enabled by running:
sudo raspi-config

Navigate to Interfacing Options > Camera and enable it.

2. Capture an Image: You can capture an image using a simple Python script:

import cv2

cam = cv2.VideoCapture(0)

ret, frame = cam.read()
if ret:
cv2.imwrite("image.jpg", frame)

cam.release()

Step 4: Train the Face Recognition Model

For this step, we’ll use OpenCV’s built-in face recognizer.

  1. Create a Dataset: Capture several images of the faces you want to recognize and store them in a directory. For example:
dataset/
├── person1/
│ ├── img1.jpg
│ ├── img2.jpg
├── person2/
├── img1.jpg
├── img2.jpg

2. Train the Model: Use the following script to train your face recognizer:

import cv2
import os
import numpy as np

recognizer = cv2.face.LBPHFaceRecognizer_create()

def get_images_and_labels(path):
image_paths = [os.path.join(path, f) for f in os.listdir(path)]
face_samples = []
ids = []
for image_path in image_paths:
gray_img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
face_id = int(os.path.split(image_path)[-1].split(".")[1])
face_samples.append(gray_img)
ids.append(face_id)
return face_samples, ids

faces, ids = get_images_and_labels('dataset')
recognizer.train(faces, np.array(ids))

recognizer.write('trainer/trainer.yml')

Step 5: Implement Real-Time Face Recognition

Now that you have a trained model, it’s time to recognize faces in real-time.

  1. Load the Trained Model:
recognizer.read('trainer/trainer.yml')

2. Detect Faces:

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

cam = cv2.VideoCapture(0)

while True:
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.2, 5)
for (x, y, w, h) in faces:
id, confidence = recognizer.predict(gray[y:y+h, x:x+w])
if confidence < 100:
id = f"Person {id}"
confidence = f" {round(100 - confidence)}%"
else:
id = "unknown"
confidence = f" {round(100 - confidence)}%"
cv2.putText(img, str(id), (x+5,y-5), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
cv2.putText(img, str(confidence), (x+5,y+h-5), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,0), 1)
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow('camera', img)
k = cv2.waitKey(10) & 0xff
if k == 27: # Press 'ESC' to quit
break

cam.release()
cv2.destroyAllWindows()

Step 6: Testing and Debugging

Run your script and test the face recognition system. Make adjustments as necessary, depending on the performance and accuracy of your model.

Conclusion

You’ve now set up a face recognition system using OpenCV on a Raspberry Pi! This system can be expanded with additional features like storing recognized faces in a database or triggering events upon recognition. The possibilities are endless, and this project can be a great foundation for more advanced applications.

Feel free to experiment and share your experiences in the comments below!

References:

--

--

Fahiz
Fahiz

No responses yet