This project is done with Open Source Computer Vision Library (OpenCV). OpenCV was designed for computational efficiency and with a strong focus on real-time applications. So, it's perfect for real-time face recognition using a camera. To create a complete project on Face Recognition, we must work on 3 very distinct phases:Face detection and data gathering, train the recognizer and face recognition. we will simply create a data-set, where we will store images for each id, a group of photos that are used for face detection.



I. Raspberry Pi 4 model BRaspberry Pi 4 model B                       

II. Raspberry Pi 4 Power SupplyRaspberry Pi 4 Power Supply     

III. Raspberry pi Touch Screen Display 7 inchRaspberry pi Touch Screen Display 7 inch

IV. Raspberry Pi NoIR CameraRaspberry Pi NoIR Camera        



Raspberry Pi 4 Pin Configuration



i.Connect Ribbon cable from Display to Raspberry Pi

ii. Connect SDA to Raspberry Pi SDA pin and connect SCL from Display to Raspberry Pi SCL pin


iii. Connect Ribbon cable from camera to Raspberry Pi

iv. Connect GND from Display to Raspberry Pi GND

v. Connect 5V from Display to Raspberry Pi 5V


Procedure for installing opencv and facial recognition libraries

Step #1: Expand file system


$ sudo raspi-config

In the “Advanced Options” menu select “Expand Filesystem”, select the first option, “A1. Expand FileSystem”, click “<Finish>” button, and then reboot your Pi


$ sudo reboot


Step #2: Install dependencies

$ sudo apt-get update && sudo apt-get upgrade  
$ sudo apt-get install build-essential cmake pkg-config  
$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev  
$ sudo apt-get install libxvidcore-dev libx264-dev  
$ sudo apt-get install libgtk2.0-dev libgtk-3-dev  
$ sudo apt-get install libfontconfig1-dev libcairo2-dev
$ sudo apt-get install libgdk-pixbuf2.0-dev libpango1.0-dev
$ sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-103
$ sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5
$ sudo apt-get install libatlas-base-dev gfortran  
$ sudo apt-get install python2.7-dev python3-dev 
$ sudo apt-get install python3-pil.imagetk


Step #3: Download the OpenCV source code

$ cd ~
$ wget -O
$ unzip
$ wget -O
$ unzip

Step #4: Install Pip

$ wget
$ sudo python3


step #5: Installing NumPy on your Raspberry Pi

$ pip3 install numpy


Step #6: Compile and Install OpenCV

$ cd ~/opencv-4.1.1/  
$ mkdir build 
$ cd build
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-4.1.1/modules \


step #7 Configure your swap space size before compiling

if you are not increasing the swap size then there is a chance that opencv will not compile


Open your /etc/dphys-swapfile  and then edit the CONF_SWAPSIZE  variable:


sudo nano /etc/dphys-swapfile


The default value in Raspbian is:


Change this to:


Stop and start the service that manages the swapfile on Rasbian:


$ sudo /etc/init.d/dphys-swapfile stop
$ sudo /etc/init.d/dphys-swapfile start


step #8 Build and install OpenCV

$ make -j4
$ sudo make install
$ sudo ldconfig


Step #9: Testing your OpenCV  install

$ python3
>>> import cv2
>>> cv2.__version__

step #10 Change your swap size back

If swap size is not restored back the memory card will not last longer.

Open your  /etc/dphys-swapfile  and then edit the  CONF_SWAPSIZE  variable:


To revert to the smaller swap space, restart the swap service:

$ sudo /etc/init.d/dphys-swapfile stop
$ sudo /etc/init.d/dphys-swapfile start


step #11 Installing libraries required for facial recognition:

$ pip3 install dlib
$ pip3 install face_recognition
$ pip3 install imutils


Executing the code

The source files for this project are attached with this blog, simply extract and run the code.


step #1

Run the file, a dialog window will open, enter the name of the person whose images are to be stored. If the name is not entered the folder will not be created. This is the folder where all the images of that particular person will be stored and is required for further processing.


$ python3


Click on create and then click start capture.




An image capturing window will open click on Snapshot to take the photos of the person. It will store a single image every time you click on Snapshot. Take at least 6-7 images in the different face position, for example straight, tilt and side pictures. Close the window when done.


To create the multiple person data repeat the previous step

NOTE: If numerous person data are stored, the encoding time will increase, as the raspberry pi is a low power computer it will take more time to process, and the facial recognition will run slower.


step #2

Run the file .




A window will open and shows the processing image. The processing time depends on the number of images stored.


After processing the images, the "Frame window" will open, when the camera detects the person's face, it will show the name of the person if its matches with the images in the database. Press Q on the keyboard to close the Frame window.