Tuesday, April 23, 2024

Face Recognition Using Raspberry Pi

To secure our assets and protect our privacy, there is a strong demand for user-friendly biometric security systems. There are various types of biometric systems that use signatures, fingerprints, voice, hand geometry, ear geometry, face detection and so on. Among these, face recognition appears to be quite exciting and is catching attention.

Circuit and working

This project requires Raspberry Pi B+, Raspberry Pi camera, a pushbutton switch, a relay and some miscellaneous components.

Circuit diagram of the face recognition system using Raspberry Pi
Fig. 1: Circuit diagram of the face-recognition system using Raspberry Pi

Face images are captured through Raspberry Pi camera and stored in a database in Raspberry Pi. To capture your face image, place yourself in front of the Pi camera and press pushbutton switch S1. The image of your face will get stored in the database. Next time when you face Pi camera and press S1, your face will be recognised, relay RL1 will be energised and your electrical load/solenoid will be activated.


This project uses OpenCV computer vision library to perform face detection and recognition.

First, install OpenCV dependencies. Compiling OpenCV on Raspberry Pi may take about five hours (depending on your system and network speed). So make sure you have sufficient time to start the process before proceeding.

- Advertisement -

Power on Raspberry Pi, open the terminal, set up Wi-Fi and execute the following commands:

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential
cmake pkg-config python-dev libgtk2.0-
dev libgtk2.0 zlib1g-dev libpng-dev
libjpeg-dev libtiff-dev libjasper-dev
libavcodec-dev swig unzip

Select yes for all options and wait for the libraries and dependencies to be installed.

- Advertisement -

Now, unzip OpenCV directory by executing the following commands:

$ wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.9/opencv-2.4.9.zip
$ unzip opencv-2.4.9.zip

Change the directory and execute cmake command as given below to build the makefile:

$ cd opencv-2.4.9
gpu=OFF -DBUILD_opencv_ocl=OFF

Compile the project by executing the command given below:

$ make

It may take about five hours for compilation.

Install the compiled OpenCV libraries by executing the following command:

$ sudo make install

The latest version of OpenCV is now installed on your Raspberry Pi.

Face-recognition code is written in Python, so some dependencies have to be installed using the following commands:

$ sudo apt-get install python-pip
$ sudo apt-get install python-dev
$ sudo pip install picamera
$ sudo pip install rpio

After OpenCV and Python dependencies are installed, the project can be tested in three major steps as explained below.

Software testing

This project uses EIGENFACES ALGORITHM IN OPENCV to perform face recognition. To use this algorithm, create a set of training data with pictures of faces that are allowed to trigger the relay.

Follow the steps given below:
Execute the following command to run capture-positives script to find a single face image:

$ sudo python capture-positives.py

Wait for some time and observe the terminal until you see Press Button instruction. Press S1 to capture your face image. If the script detects a single face, it will crop and save the training image in positive sub-directory.

If the script cannot detect a face, or detects multiple faces, error message ‘Could not detect single face! Check the image in capture.pgm’ will be displayed. It is recommended to maintain a distance of about 0.5 metres from the camera while taking a picture.

Press Ctrl+C to stop the script. Open capture.pgm file to view the last captured image.
Check the face in the database and train the face recogniser by running train.py code:

$ sudo python train.py

Training the face-recognition model on Raspberry Pi will take about ten minutes. Once training is complete, you will see mean.png and positive_eigenface.png files to visualise Eigenfaces of the model.

Now, test the face recogniser to recognise the face trained earlier. Execute the following command:

$ sudo python box.py

Observe the terminal to see Press Button instruction. Aim the camera at your face and press S1. You should see a message ‘Button pressed, looking for face…’ on the terminal. After a few seconds, if the face is recognised, you will see the message ‘Recognised face!’ and relay RL1 will be energised.

You can connect an electrical load across the relay contacts at CON1. A 12V solenoid lock may be used as electrical load during testing. If your face is recognised or detected, relay is energised, solenoid lock is activated and the secured door gets opened.

Download source code

Biswajit Das was manager – R&D, EFY Labs, till recently


  1. Dear Sir,
    I had installed opencv by executing your commands and i am in cv environment but i am getting error in software testing codes it shows bash error please help me
    waiting for your response

  2. hi
    i am using raspberry pi 3 B, i did the same way you said but gave an error like this
    sudo python capture-positives.py
    Traceback (most recent call last):
    File “capture-positives.py”, line 14, in
    import hardware
    File “/home/pi/hardware.py”, line 8, in
    import RPIO
    File “build/bdist.linux-armv7l/egg/RPIO/__init__.py”, line 115, in
    File “build/bdist.linux-armv7l/egg/RPIO/_GPIO.py”, line 7, in
    File “build/bdist.linux-armv7l/egg/RPIO/_GPIO.py”, line 6, in __bootstrap__
    SystemError: This module can only be run on a Raspberry Pi!
    what should i do ? please help me .

      • i am getting few errors.
        first i got this error in train.py:
        File “train.py”, line 77, in
        model = cv2.createEigenFaceRecognizer()
        AttributeError: ‘module’ object has no attribute ‘createEigenFaceRecognizer’
        but i removed this error by modifying the following line:
        model = cv2.face.createEigenFaceRecognizer()

        now i am getting another error:
        File “train.py”, line 85, in
        mean = model.getMat(“mean”).reshape(faces[0].shape)
        AttributeError: ‘cv2.face_BasicFaceRecognizer’ object has no attribute ‘getMat’

        how can i remove this error?i am using opencv 3.0

  3. hi. i am getting an error in box.py
    File “box.py”, line 50, in
    label, confidence = model.predict(crop)
    TypeError: ‘int’ object is not iterable

    i am using opencv3..i was able to run the other two files but i cant get rid of this error..please help me.

  4. Hi EFY Team..
    while trying this DIY I am getting error please guide me…

    bash: cmake: command not found

    • You can change the camera source by making the following changes
      step 1-
      open your source code folder
      step 2-
      open config.py with your favourate editor
      find the below lines in your config.py file

      def get_camera():
      # Camera to use for capturing images.
      # Use this code for capturing from the Pi camera:
      import picam
      return picam.OpenCVCapture()
      # Use this code for capturing from a webcam:
      # import webcam
      # return webcam.OpenCVCapture(device_id=0)

      now change these lines to :

      def get_camera():
      # Camera to use for capturing images.
      # Use this code for capturing from the Pi camera:
      #import picam
      #return picam.OpenCVCapture()
      # Use this code for capturing from a webcam:
      import webcam
      return webcam.OpenCVCapture(device_id=0)

  5. Traceback (most recent call last):
    File “/home/pi/Downloads/face controlled appliance/Face Recognition Appliance Control using Raspberry Pi/box.py”, line 6, in
    import RPIO
    ImportError: No module named ‘RPIO’

    sir please help

    • Short answer Yes. Long answer:- You have to check the camera compatibility and drivers it supported in Linux or not. Then you need to do setting and find the camera and make a change in code to get a video of the same camera.


Unique DIY Projects

Electronics News

Truly Innovative Tech

MOst Popular Videos

Electronics Components