element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • 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 Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • 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
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • 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 18317 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 6 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 6 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 6 years ago +2
    Nice post. You can use a memory stick with your Rpi to transfer files between machine types. DAB
  • chetanj85
    chetanj85 over 2 years ago

    hi sharib nice content shared by you.

    do you have face mask detection with name by raspberry pi.

    or 

    face mask detection with name by raspberry pi and if not then message comming on mobile no

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • sharib123
    sharib123 over 2 years ago in reply to alpaslan

    Hi Alpaslan, 

    I have not used any other language except English. I will try to implement it in different languages in the future.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • alpaslan
    alpaslan over 2 years ago

    Hello again.
    The face recognition system works well, but since the names of the faces it recognizes are in Turkish, it prints it as a question mark (?). Turkish characters are ÇçğııÖŞşÜ. Our language encoding is utf-8 or charset-1254.
    There is a source for the picture or video on the internet, but I couldn't find it for the camera.

    Unfortunately, whatever I did to the line below was unsuccessful. Any help possible please?

    # draw the guessed face name on the image
    cv2.rectangle(frame, (left, top), (right, bottom),
    (0, 255, 0), 2)
    y = top - 15 if top - 15 > 15 otherwise top + 15
    cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,
    0.75, (0, 255, 0), 2)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • alpaslan
    alpaslan over 2 years ago in reply to sharib123

    Hi.

    First of all, thank you very much for your interest.
    I tried typing the full path to the dataset folder in face_recognize.py. /home/pi/Desktop/sound/dataset.
    Solution: Okey is working fine.

    Thank you very much...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • sharib123
    sharib123 over 2 years ago in reply to alpaslan

    Your shortcut is not able to find the dataset of the images. Try to save the dataset in the home/pi folder or modify the code as needed. Check the code of face_recognize.py and verify the dataset location from there.

    • Cancel
    • Vote Up +1 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 © 2025 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