element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Raspberry Pi Facial Recognition
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
GPIO Pinout
Raspberry Pi Wishlist
Comparison Chart
Quiz
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: sharib123
  • Date Created: 11 Feb 2019 6:18 AM Date Created
  • Views 19158 views
  • Likes 19 likes
  • Comments 61 comments
Related
Recommended
  • face_recognition
  • iot security
  • open cv
  • machine_learning
  • artificial intelligence
  • raspberry pi
  • ai
  • ai project
  • computer vision

Raspberry Pi Facial Recognition

sharib123
sharib123
11 Feb 2019

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.

 

Requirements:

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

image

 

Connection:

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

image

 

iii. Connect Ribbon cable from camera to Raspberry Pi

image

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

 

image

$ 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 opencv.zip https://github.com/opencv/opencv/archive/4.1.1.zip
$ unzip opencv.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.1.zip
$ unzip opencv_contrib.zip

Step #4: Install Pip

$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py

 

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
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-4.1.1/modules \
    -D ENABLE_NEON=ON \
    -D ENABLE_VFPV3=ON \
    -D BUILD_TESTS=OFF \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D CMAKE_SHARED_LINKER_FLAGS=-latomic \
    -D BUILD_EXAMPLES=ON ..

 

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:

CONF_SWAPSIZE=100

Change this to:

CONF_SWAPSIZE=1024

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__
'4.1.1'

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:

CONF_SWAPSIZE=100

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 faceDataset.py, 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 faceDataset.py

 

Click on create and then click start capture.

 

image

 

 

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.

image

 

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 face_recognize.py .

 

$python3 face_recognize.py

 

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

image

 

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.

 

image

Attachments:
facepi.zip
  • Sign in to reply

Top Comments

  • sharib123
    sharib123 over 7 years ago in reply to fmilburn +4
    Hi Frank, faceDataset.py is built using tkinter framework and hence it is required. You can do manually also, without using it. You can create folder of your name manually inside the dataset folder, and…
  • sharib123
    sharib123 over 7 years ago in reply to fmilburn +3
    Dear Frank, Thank you for correcting me, it is faceDataset.py. The attached source files are for above blog only. you are getting error because there is some problem with your tkinter. Type this in terminal…
  • DAB
    DAB over 7 years ago +2
    Nice post. You can use a memory stick with your Rpi to transfer files between machine types. DAB
  • eric10
    eric10 over 5 years ago

    Hello friends, I made this code and it works for me, but when you recognize a person facially, the camera becomes very slow, I have a Raspberry Pi 4 and a 64GB micro SD. Any recommendation so that it is not so slow? Thank you.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • aquamann
    aquamann over 5 years ago

    Is there anyone who can help an old man of 60?

    to make this work with magic mirror in the bottom right corner. Have looked at this one also tried with message but got no reply.

    (https://smartbuilds.io/) it seems very cool but finds it locked to a specific image. Also, do not find routine for face recognition with permanently recognized people so that one does not have to go back such a sequence once magic mirror is on as it takes quite a long time to go through a folder of 200 pictures. Takes so much in advance if help arrives since I have no clue about programming

    Roy

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • aquamann
    aquamann over 5 years ago

    I have to run python3 face_recognize.py every time. The people who are already recognizable, they put in a folder for familiar faces.

    and which python file to run when people are recognized so that you don't have to go through all the pictures again and again.

    With regards

    Roy

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • vlasov01
    vlasov01 over 5 years ago in reply to aquamann

    Hi Roy,

     

    Take a look at Pan/tilt face tracking with a Raspberry Pi and OpenCV project by Adrian Rosebrock https://www.pyimagesearch.com/2019/04/01/pan-tilt-face-tracking-with-a-raspberry-pi-and-opencv/ 

    It is very well documented and a good point to start your project.

     

    Regards,

     

    Sergey

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • aquamann
    aquamann over 5 years ago

    Hi! I am a man from norway have no experience with rapsberry or programming nor does english use google translate to write to you now. Writer for i have come across the opencv since i found online from you look promising but wondered if help to make this work with magic mirror 2 and with this https://www.ebay.com/itm/172060088556 and with google voice assistant thought to use this for motion camera https: //www.ebay. com / itm / Hcsr04-Hc-sr04-Sensor-Meter-Distance-Ultrasonic-Arduino-Raspberry-Pi / 323065543268? hash = item4b3834fa64: g: xhcAAOSw6BtVUIL - is unsure if it can be combined with turning monitor off and on when we not in front of the mirror.

    So if you would, I really appreciated helping with this.

    With regards

    Roy Bråthe (roybraathe@gmail.com)

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
<>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube