Face Recognition Using Raspberry Pi

electronicsforu.com To secure our assets and protect our privacy, there is a strong demand for user-friendly biometric security


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.

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.

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

The post Face Recognition Using Raspberry Pi appeared first on Electronics For You.

Source: Face Recognition Using Raspberry Pi

Pin It

Leave a Comment