Thursday, October 10, 2024

How to Create A Simple Face Recognition with OpenCV and Raspberry Pi

Building a face recognition system has become increasingly accessible thanks to advancements in computer vision libraries like OpenCV and affordable computing power from devices like the Raspberry Pi. This guide walks you through setting up a face recognition system using OpenCV on a Raspberry Pi, empowering you to create a project that can be used for various applications from security systems to personal gadgets.

Before diving into the code, you'll need a few essential components: a Raspberry Pi (preferably with 4GB RAM or more), a Raspberry Pi Camera Module or a USB webcam, a MicroSD Card (with Raspberry Pi OS installed), a power supply for the Raspberry Pi, a keyboard, mouse, and monitor for initial setup, and an internet connection for installing libraries.

The first step is to set up your Raspberry Pi by installing Raspberry Pi OS. Download the Raspberry Pi Imager from the official website and flash the OS onto your MicroSD card. Insert the MicroSD card, connect the peripherals, and power on the Raspberry Pi. Once booted, open a terminal and run sudo apt-get update followed by sudo apt-get upgrade to ensure you have the latest system updates.

OpenCV, a powerful library for image processing, is crucial for our face recognition system. Installing it on the Raspberry Pi involves several steps. Start by installing the necessary 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
    
Next, install OpenCV itself using pip:
pip install opencv-python
pip install opencv-python-headless
    
Finally, test the installation by opening Python and importing OpenCV:
import cv2
print(cv2.__version__)
    
If no errors appear and the version number is displayed, your OpenCV installation is successful.

The next step is to capture images using the camera module to recognize faces. Ensure the camera is enabled by running sudo raspi-config, navigating to Interfacing Options > Camera, and enabling it. 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()
    
This script accesses the camera, captures a frame, and saves it as "image.jpg".

To train the face recognition model, we'll utilize OpenCV's built-in face recognizer. Start by creating a dataset containing images of the faces you want to recognize. Organize them into directories named after each person, for example:

dataset/
├── person1/
│   ├── img1.jpg
│   ├── img2.jpg
├── person2/
    ├── img1.jpg
    ├── img2.jpg
    
Once you have your dataset, train the model using the following script:
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')
    
This script reads images from the dataset, extracts face features, and trains the face recognizer. The trained model is saved as "trainer/trainer.yml".

Now that you have a trained model, it's time to implement real-time face recognition. Begin by loading the trained model:

      recognizer.read('trainer/trainer.yml')
    
Next, detect faces in real-time using the following script:
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()
    
This script captures video from the camera, detects faces using a pre-trained face detector, and uses the trained face recognizer to identify them. The recognized faces are displayed on the screen with their corresponding IDs and confidence scores.

Once you have your face recognition system set up, run your script and test its performance. Adjust settings and refine the model as needed to optimize accuracy and functionality.

This system serves as a foundation for more advanced applications. You can expand it to store recognized faces in a database, trigger events upon recognition, or integrate it with other systems. The possibilities are endless, and this project can be a great starting point for exploring the world of computer vision and facial recognition.

Feel free to experiment, explore different approaches, and share your experiences with the community. The journey of learning and building is an exciting one, and we encourage you to embrace it fully.

0 comments:

Post a Comment