✨ Use RCAPP and get 5% off 👇
Skip to content
Free Delivery on Orders Above Rs 999/- Pan-India
Cash on Delivery Available for Orders above Rs.500/- and Upto Rs 3000/-
SAVE more when you BUY more. Upto 30% Off on BULK PURCHASE
GST Invoices for Your Business
Dedicated Technical Support Team
Safely Delivering Genuine Products PAN INDIA

Build a Raspberry Pi 5 and OpenCV Based Face Recognition

Build a Raspberry Pi 5 and OpenCV Based Face Recognition
-
Written By - Robocraze -
📅 Updated on 30 Jul 2025
Summarize with AI
✅ Prompt copied

Summary

After countless late nights tinkering with computer vision projects, I can confidently say that the intersection of affordable hardware and powerful AI libraries has never been more exciting.

When I first got my hands on the Raspberry Pi 5, I immediately knew I had to push its limits with a project that would truly showcase its enhanced capabilities.

The fascinating realm of computer vision has become remarkably accessible, transforming what once required expensive specialized hardware into achievable home projects that anyone can tackle on a weekend. 

In this comprehensive walkthrough, we'll dive deep into creating your own face recognition system using the Raspberry Pi 5 and OpenCV.

Whether you're a hobbyist looking to enhance your home security setup, a student exploring computer vision concepts, or a maker wanting to add intelligent features to your projects, this guide will take you through every step of building a functional face recognition system from scratch.  

Build a Raspberry Pi 5 and OpenCV Based Face Recognition - Cover image

What is OpenCV

After working with various computer vision libraries over the years, I keep coming back to OpenCV for one simple reason: it just works reliably across different platforms and use cases.

OpenCV (Open-Source Computer Vision Library) stands as one of the most powerful and versatile computer vision libraries available today, and I've personally witnessed its evolution from a research tool into the backbone of countless commercial applications.

What initially drew me to OpenCV was its incredible versatility - this remarkable toolkit provides developers with an extensive collection of algorithms and functions specifically designed for real-time computer vision applications.

Originally developed by Intel in 1999, OpenCV has evolved into a comprehensive platform that supports multiple programming languages and operating systems, and I've used it successfully on everything from high-end workstations to tiny embedded devices.

For face recognition applications, OpenCV provides several pre-trained models and algorithms, including Haar Cascade classifiers and more modern deep learning approaches.

These tools enable developers to detect faces in images and video streams with remarkable accuracy, making it an ideal foundation for building sophisticated recognition systems on affordable hardware platforms.

Components and Supplies

Official Raspberry Pi 5 Model 8GB RAM – High-end Raspberry Pi Board with 8GB RAM for intensive computing. -RobocrazeOfficial Raspberry Pi 5 Model 8GB RAM – High-end Raspberry Pi Board with 8GB RAM for intensive computing. -Robocraze

Official Raspberry Pi 5 Model 8GB RAM

Raspberry Pi 5 Model 8GB RAM This official Latest Raspberry Pi 5 8GB is equipped with a very high 2.4 GHz quad-core 64-bit Arm Cortex A76 CPU, which is over 2.5 times faster than the previous model Raspberry Pi 4, and is therefore the quickest...
Rs 22,451/-
Rs 22,451/-
Rs 25,473/-
Save Rs 3,022/-
Official Raspberry Pi Camera Module V2 - 8MP – Compact Camera Module with 8MP & 1080p. -RobocrazeOfficial Raspberry Pi Camera Module V2 - 8MP – Compact Camera Module with 8MP & 1080p. -Robocraze

    Official Raspberry Pi Camera Module V2 - 8 Megapixel,1080p

    Official Raspberry Pi Camera Module V2 - 8 Megapixel,1080p The Raspberry Pi Camera Module V2 is an excellent little camera developed for Raspberry Pi boards. It contains an 8-megapixel Sony IMX219 image sensor for capturing clear and vibrant photographs. You can also capture films...
    Rs 1,698/-
    Rs 1,698/-
    Rs 2,799/-
    Save Rs 1,101/-
    SanDisk 32GB Micro SD-SDHC Card – Essential Raspberry Pi SD card for OS & file storage. -RobocrazeSanDisk 32GB Micro SD-SDHC Card – Essential Raspberry Pi SD card for OS & file storage. -Robocraze

      SanDisk 32GB Micro SD-SDHC Memory Card

      SanDisk 32GB Micro SD-SDHC Memory Card The SanDisk 36GB Micro SD memory card is an A1 micro SD Card with a class 10 rating. It is well known for its high speed of Read/Write operation which helps to load applications and operating systems faster...
      Rs 1,499/-
      Rs 1,499/-
      Rs 1,899/-
      Save Rs 400/-
      5V 2A ERD Adapter with Micro USB Cable – ERD adapter for Raspberry Pi & microcontrollers.-Power Adapter -Robocraze5V 2A ERD Adapter with Micro USB Cable – ERD adapter for Raspberry Pi & microcontrollers.-Power Adapter -Robocraze

        5V 2A ERD Adapter with Micro USB cable

        5V 2A ERD Power Adapter This is an ERD fast charger adapter with 5V 2A capacity. ERD chargers comes with smart chip to protect your device from Short Circuit & Overload. They are compatible with Raspberry Pi boards for data transfer and connectivity.  5V/2A Ac-DC...
        Rs 199/-
        Rs 199/-
        Rs 299/-
        Save Rs 100/-
        3 in 1 Raspberry Pi Aluminium Heat Sink for Pi 33 in 1 Raspberry Pi Aluminium Heat Sink Pi 3 – Efficient heat sink for Raspberry Pi 3 -Electronic Components - Robocraze

        3 in 1 Raspberry Pi Aluminium Heat Sink for Pi 3

        3 in 1 Raspberry Pi Aluminium Heat Sink This is an aluminum heatsink kit used for cooling the chips on Raspberry Pi. Compared to another heat sink, it has been attached with thermal conductive adhesive tape, allowing the Raspberry Pi to cool more efficiently....
        Rs 19/-
        Rs 19/-
        Rs 34/-
        Save Rs 15/-
        HDMI to VGA Converter with Audio - Black – Premium Raspberry Pi HDMI cable for VGA connectivity. -RobocrazeHDMI to VGA Converter with Audio - Black – Premium Raspberry Pi HDMI cable for VGA connectivity. -Robocraze

          HDMI to VGA Converter with Audio - Black

          HDMI to VGA Converter with audio- Black HDMI to VGA Converter, with 3.5 mm Audio Out converts digital HDMI signal into analogue VGA video. This converter module includes an integrated audio output and is powered by an HDMI connector, so this HDMI Male to VGA Female...
          Rs 208/-
          Rs 208/-
          Rs 346/-
          Save Rs 138/-
          Raspberry Pi 400 Unit – Keyboard-integrated Raspberry Pi Board for a complete computing setup. -RobocrazeRaspberry Pi 400 Unit – Keyboard-integrated Raspberry Pi Board for a complete computing setup. -Robocraze

          Raspberry Pi 400 Unit

          Raspberry Pi 400 Unit Online The Raspberry Pi 400 is the all-new launch of the Raspberry Pi Foundation. The innovation behind the Raspberry Pi 400 will allow you to directly connect the keyboard to the desktop and start your projects and whatnot. It is...
          Rs 8,012/-
          Rs 8,012/-
          Rs 10,499/-
          Save Rs 2,487/-
          Raspberry Pi Official Mouse Black – Ergonomic Raspberry Pi Mouse with a sleek black design. -RobocrazeRaspberry Pi Official Mouse Black – Ergonomic Raspberry Pi Mouse with a sleek black design. -Robocraze

            Raspberry Pi Original Mouse (Black/Grey) (83)

            Raspberry Pi Official Mouse Online The Official Raspberry Pi mouse is a three-button optical mouse with a scroll wheel that connects to your Raspberry Pi board with a USB type-A connector. The mouse is compatible with all Raspberry Pi boards (including the Raspberry Pi Zero...
            Rs 630/-
            Rs 630/-
            Rs 1,699/-
            Save Rs 1,069/-

            Components and Tools Required

            Having built dozens of Raspberry Pi projects with camera over the years, I've learned the hard way which components are absolutely essential and which ones you can skimp on. Let me share the exact setup that has given me the most reliable results for Raspberry Pi 5 face recognition projects, along with some hard-earned wisdom about what works and what doesn't.

            Essential Hardware:

            After burning through several cheaper power supplies in previous projects, I cannot stress enough how crucial the official power adapter is for this raspberry pi based project. The Pi 5's increased power requirements mean that inadequate power will cause frustrating crashes during intensive opencv face recognition processing.

            Optional Components:

            • Ribbon Cable Adapter (if using older camera modules with Pi 5)
            • Case for protection and better cable management
            • External Storage (USB drive for storing training images and models)

            The active cooling solution isn't optional if you plan to run continuous face recognition - I learned this lesson during a 6-hour testing session that ended with thermal throttling bringing my frame rate to a crawl.

            Set Up Your Raspberry Pi 5

            Setting up the Raspberry Pi 5 for computer vision work taught me patience the first time around, but I've since streamlined the process to avoid the common pitfalls that can waste hours of your time. Getting your Raspberry Pi 5 ready for this opencv project begins with a fresh installation of the operating system, and I've found that taking the time to do this properly saves countless headaches later.

            Initial Setup Process:

            I always start by downloading the latest Raspberry Pi OS image using the official Raspberry Pi Imager tool - it's genuinely the most reliable method I've found. Flash your microSD card with Raspberry Pi OS and insert it into your Pi 5. Connect your monitor, keyboard, mouse, and power supply. During the first boot, you'll be guided through the initial configuration wizard where you'll set up your WiFi connection, enable SSH if needed, and update the system locale settings.

            System Updates and Preparation:

            The first thing I do after any fresh install is update the system - this step has saved me from mysterious compatibility issues more times than I can count. Open the terminal and update your package repositories: 

            
            sudo apt update sudo apt upgrade -y
            

            Enable Camera Interface:

            From my experience with multiple Raspberry Pi 5 project builds, forgetting to enable the camera interface is the most common oversight. Navigate to the Raspberry Pi Configuration tool through the main menu or use the command line:

            
            sudo raspi-config
            

            Select "Interface Options" and enable the camera interface. This step is crucial for your face detection with the Raspberry Pi 5 project to access the camera module properly. Always reboot after making these changes - I've seen too many projects fail because someone skipped this simple step.

            Install OpenCV and Python Libraries

            I remember spending an entire weekend compiling OpenCV from source on a Raspberry Pi 3. Fortunately, the installation process for how to install OpenCV on Raspberry Pi 5 has been streamlined significantly. The OpenCV library can now be installed using pre-compiled packages, and I've tested this method across multiple Pi 5 setups with consistent success.

            Installing Core Dependencies:

            Based on my testing with various OpenCV project configurations, this combination of packages provides the most stable foundation:

            bash

            
            sudo apt install python3-pip python3-numpy python3-opencv -y 
            

            This command installs the OpenCV library along with NumPy, which is essential for numerical operations in image processing. I've found that the pre-compiled OpenCV package for Raspberry Pi includes most common functionality needed for computer vision projects, eliminating the compilation headaches I used to face.

            Additional Required Libraries:

            After experimenting with different face recognition approaches, I've settled on this specific combination that delivers the best balance of accuracy and performance:

            bash

            
            pip3 install face-recognition imutils picamera2
            

            The Python face recognition library provides high-level functions that dramatically simplify implementation compared to building everything from scratch with raw OpenCV. Trust me —I've tried both approaches, and unless you have very specific requirements, this library will save you weeks of development time.

            Verification of Installation:

            I always test installations immediately to catch problems early. Open a Python3 shell and verify everything works:

            Python code

            
            import cv2 
            import face_recognition 
            from picamera2 import Picamera2 
            print("OpenCV version:", cv2.__version__)
            

            If all imports succeed without errors, you're ready to proceed. In my experience, installation issues are usually caught at this stage, making them much easier to troubleshoot.

            Connect the Camera to Raspberry Pi 5

            Having wrestled with camera connections across multiple Raspberry Pi generations, I can tell you that the Pi 5's updated connector initially caught me off guard. Proper camera connection is absolutely crucial for your Raspberry Pi 5 project success, and I've learned some tricks that can save you from the frustration I experienced during my first attempt.

            Physical Connection:

            The most important lesson I learned: always power down your Raspberry Pi 5 completely before connecting the camera. I once damaged a camera module by hot-plugging it, and that's a mistake you only make once. Locate the CSI (Camera Serial Interface) connector near the HDMI ports —it's smaller than previous Pi models, which initially confused me.

            The connector has a plastic clip that lifts up to allow cable insertion. Insert the camera ribbon cable with the blue side facing away from the Ethernet port. I've found that ensuring the connection is perfectly aligned before closing the clip prevents the intermittent connection issues that plagued my early attempts.

            Camera Module Compatibility:

            After testing various camera modules, I can confidently recommend that the Raspberry Pi 5 face recognition system works best with the camera module V2. The autofocus capabilities are essential for clear face capture at various distances, and I've seen dramatic improvements in recognition accuracy compared to fixed-focus modules.

            Testing Camera Functionality:

            I always test the camera immediately after connection using this simple script:

            python

            
            from picamera2 import Picamera2 
            import cv2 
             
            picam2 = Picamera2() 
            picam2.configure(picam2.create_preview_configuration(main={"format": 'XRGB8888', "size": (640, 480)})) 
            picam2.start() 
             
            # Capture a test image 
            array = picam2.capture_array() 
            cv2.imshow("Camera Test", array) 
            cv2.waitKey(0) 
            cv2.destroyAllWindows()
            

            This test has saved me hours of debugging later in the development process by confirming the camera works properly with OpenCV right from the start.

            Collect and Save Face Images for Training

            Through building multiple OpenCV face recognition systems, I've learned that the quality of your training data directly determines your system's success. This phase initially frustrated me because I underestimated its importance, but after seeing the dramatic difference that proper training data makes, I now consider it the most critical step in the entire process.

            Creating the Training Dataset:

            I always start with a well-organized directory structure—it prevents confusion later when you're managing multiple people's data:

            Creating the Training Dataset:

            I always start with a well-organized directory structure—it prevents confusion later when you're managing multiple people's data:

            bash

            
            mkdir -p ~/face_recognition/dataset 
            mkdir -p ~/face_recognition/models
            

            Image Capture Script:

            Now, we will create a Python script named

             “capture_training_images.py”,

            which will help us collect the images and process them in a systematic way.

            After experimenting with different capture methods, I developed this systematic approach that consistently produces high-quality training datasets:

            python

            
            from picamera2 import Picamera2 
            import cv2 
            import os 
            import time 
             
            def capture_training_images(person_name, num_images=30): 
                """Capture training images for a specific person""" 
                 
                # Create person directory 
                person_dir = f"dataset/{person_name}" 
                os.makedirs(person_dir, exist_ok=True) 
                 
                # Initialize camera 
                picam2 = Picamera2() 
                picam2.configure(picam2.create_preview_configuration(main={"format": 'XRGB8888', "size": (640, 480)})) 
                picam2.start() 
                 
                print(f"Capturing {num_images} images for {person_name}") 
                print("Press SPACE to capture image, ESC to exit") 
                 
                count = 0 
                while count < num_images: 
                    # Capture frame 
                    frame = picam2.capture_array() 
                     
                    # Display frame 
                    cv2.putText(frame, f"Images captured: {count}/{num_images}",  
                               (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) 
                    cv2.imshow("Capture Training Images", frame) 
                     
                    key = cv2.waitKey(1) & 0xFF 
                     
                    if key == ord(' '):  # Space key to capture 
                        filename = f"{person_dir}/{person_name}_{count+1}.jpg" 
                        cv2.imwrite(filename, frame) 
                        print(f"Saved: {filename}") 
                        count += 1 
                        time.sleep(0.5)  # Brief pause between captures 
                         
                    elif key == 27:  # ESC key to exit 
                        break 
                 
                picam2.stop() 
                cv2.destroyAllWindows() 
                print(f"Captured {count} images for {person_name}") 
            

            Based on my testing with dozens of recognition systems, capturing 25-30 high-quality images per person provides the optimal balance between training time and accuracy.

            Running the Image-Capture Script

            Next, we have to run this script so that it collects the images, which we will use it to train a model in the next step. Just follow the steps below:

            1. Open a terminal on your Raspberry Pi 5

            Navigate to the folder where you saved the script:

            bash

            
            cd ~/face_recognition
            

            2. Start the script.

            bash

            
            python3 capture_training_images.py
            
            • When prompted, type the person’s name exactly as you want it stored (e.g., Alice).
            • Press Enter.

            3. Position yourself (or the target person) in front of the camera.

            Stand or sit so the face is clearly visible in the preview window. Good lighting and head-and-shoulders framing improve results.

            4. Capture images. 

            • Press the Spacebar to save a frame.
            • The counter on-screen shows how many of the requested 30 images have been stored. 
            • Move slightly between shots—turn your head left/right, vary expressions, step closer or farther—to give the model useful variety.
            • If you need to abort, press Esc.

            5. Finish the session.

            After 30 images (or whatever number you set), the script closes automatically, saves all photos in ~/face_recognition/dataset/person_name/, and returns to the prompt.

            6. Repeat for each person.

            Run the script again with a new name to collect another set of training images.

            That’s it. We have captured the images, and they are now ready for training.

            Training the Model

            Now that we’ve collected the images, let’s now train a model. We will write a new script named “train_model.py”, that will train a model using the images that we collected earlier.

            After writing and debugging countless opencv face recognition implementations, I've refined this code to handle the edge cases and performance issues that typically cause problems for beginners. Let me walk you through the script that trains a model which hopefully creates a reliable face recognition system.

            Face Encoding Script

            python

            
            import face_recognition 
            import os 
            import pickle 
            import cv2 
            from imutils import paths 
             
            def train_face_recognition_model(): 
                """Train the face recognition model using collected images""" 
                 
                print("[INFO] Quantifying faces...") 
                 
                # Get paths to all training images   
                image_paths = list(paths.list_images("dataset")) 
                 
                # Initialize lists for encodings and names 
                known_encodings = [] 
                known_names = [] 
                 
                # Process each image 
                for (i, image_path) in enumerate(image_paths): 
                    print(f"[INFO] Processing image {i+1}/{len(image_paths)}") 
                     
                    # Extract person name from path 
                    name = image_path.split(os.path.sep)[-2] 
                     
                    # Load image and convert BGR to RGB 
                    image = cv2.imread(image_path) 
                    rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) 
                     
                    # Detect face locations 
                    boxes = face_recognition.face_locations(rgb, model="hog") 
                     
                    # Compute facial encodings 
                    encodings = face_recognition.face_encodings(rgb, boxes) 
                     
                    # Add each encoding + name to our lists 
                    for encoding in encodings: 
                        known_encodings.append(encoding) 
                        known_names.append(name) 
                 
                # Save encodings to disk 
                print("[INFO] Serializing encodings...") 
                data = {"encodings": known_encodings, "names": known_names} 
                with open("models/encodings.pickle", "wb") as f: 
                    pickle.dump(data, f) 
                 
                print("[INFO] Training complete!") 
             
            if __name__ == "__main__": 
                train_face_recognition_model()
            

            Run this script, using the command below, and you will have a model trained on the images that you captured earlier.

            bash

            
            cd ~/face_recognition 
            python3 train_model.py 
            

            Run and Test Your Face Recognition System

            Now it’s time to test how good the model is and whether it’s able to correctly recognize your face.

            For that, we will write another script, named

            “face_recognition_live.py”,

            which will do the following things:

             

            • Startup Phase: Automatically loads your saved face encodings from encodings.pickle and initializes the Raspberry Pi Camera using the modern Picamera2 interface
            • Continuous Frame Processing: Captures live video frames, converts them from BGR to RGB color format, and detects all face locations within each frame using OpenCV's detection algorithms
            • Real-Time Face Identification: Generates a 128-dimensional embedding for every detected face and compares it against your stored database using Euclidean distance calculations to determine the closest match
            • Visual Feedback System: Draws green bounding boxes around detected faces and displays either the recognized person's name or "Unknown" for unidentified faces directly on the live video feed
            • User Control Interface: Provides a clean exit mechanism - simply press 'q' to stop the camera stream and close all windows gracefully

            python

            
            import face_recognition 
            import pickle 
            import cv2 
            from picamera2 import Picamera2 
            import numpy as np 
             
            def load_known_faces(): 
                """Load the trained face encodings""" 
                print("[INFO] Loading encodings...") 
                with open("models/encodings.pickle", "rb") as f: 
                    data = pickle.load(f) 
                return data 
             
            def recognize_faces_live(): 
                """Perform live face recognition""" 
                 
                # Load known face encodings 
                data = load_known_faces() 
                 
                # Initialize camera 
                picam2 = Picamera2() 
                picam2.configure(picam2.create_preview_configuration(main={"format": 'XRGB8888', "size": (640, 480)})) 
                picam2.start() 
                 
                print("[INFO] Starting face recognition... Press 'q' to quit") 
                 
                while True: 
                    # Capture frame from camera 
                    frame = picam2.capture_array() 
                     
                    # Convert BGR to RGB 
                    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 
                     
                    # Detect faces in current frame 
                    boxes = face_recognition.face_locations(rgb) 
                    encodings = face_recognition.face_encodings(rgb, boxes) 
                     
                    # Initialize list of names for detected faces 
                    names = [] 
                     
                    # Process each face encoding 
                    for encoding in encodings: 
                        # Compare face encoding with known faces 
                        matches = face_recognition.compare_faces(data["encodings"], encoding) 
                        name = "Unknown" 
                         
                        # Calculate face distances 
                        face_distances = face_recognition.face_distance(data["encodings"], encoding) 
                        best_match_index = np.argmin(face_distances) 
                         
                        # Check if we have a match 
                        if matches[best_match_index]: 
                            name = data["names"][best_match_index] 
                         
                        names.append(name) 
                     
                    # Draw rectangles and labels on faces 
                    for ((top, right, bottom, left), name) in zip(boxes, names): 
                        # Draw rectangle around face 
                        cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) 
                         
                        # Draw label below face 
                        y = top - 15 if top - 15 > 15 else top + 15 
                        cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 2) 
                     
                    # Display frame 
                    cv2.imshow("Face Recognition", frame) 
                     
                    # Break on 'q' key press 
                    if cv2.waitKey(1) & 0xFF == ord('q'): 
                        break 
                 
                # Cleanup 
                picam2.stop() 
                cv2.destroyAllWindows() 
             
            if __name__ == "__main__": 
                recognize_faces_live()
            

            I've found this implementation strikes the perfect balance between accuracy and real-time performance on Raspberry Pi 5 hardware.

            Live Recognition Testing:

            Now it’s time to run the program and see if it works. Type the following code in the terminal:

            bash

            
            python3 face_recognition_live.py
            

            I remember the satisfaction of seeing my name appear above the green rectangle for the first time - it validated weeks of development work. Test different angles, lighting conditions, and distances to evaluate system robustness.

            Performance Optimization (optional step):

            Through extensive testing, I've learned to monitor system resources during operation:

            bash

            
            # Check CPU and memory usage 
            htop 
             
            # Monitor temperature to prevent throttling 
            vcgencmd measure_temp
            

            The Raspberry Pi 5 improved processing power handles real-time opencv face detection remarkably well, but monitoring ensures optimal performance during extended operation sessions.

             

             

            Conclusion

            Looking back on this comprehensive journey through building a Raspberry Pi 5 face recognition system, I'm continually amazed by how accessible advanced computer vision technology has become.

            When I started working with computer vision years ago, this level of functionality required expensive workstations and months of development time - now it fits comfortably on a desk and can be built on a weekend. 

            The skills and knowledge gained from this project extend far beyond face recognition, providing a solid foundation for exploring other computer vision challenges and machine learning applications on embedded systems.

            Excerpt

            This guide will walk you through each step of building a working face recognition system from scratch using the Raspberry Pi 5, OpenCV, and Python programming.
            Prev Post
            Next Post

            Leave a comment

            Please note, comments need to be approved before they are published.

            Thanks for subscribing!

            This email has been registered!

            Shop the look

            Choose Options

            Edit Option
            Back In Stock Notification
            Compare
            Product SKU Description Collection Availability Product Type Other Details

            Choose Options

            this is just a warning
            Login
            Shopping Cart
            0 items
            FREE SHIPPING!
            ₹100 OFF
            ₹200 OFF
            ₹999
            ₹2500
            ₹4900
            WhatsApp Chat Chat