How to Train your Raspberry Pi for Facial Recognition

Raspberry Pi Facial Recognition
(Image credit: Tom's Hardware)

When you unlock your phone (FaceID) or allow Google or Apple to sort your photos, you are using facial recognition software. Many Windows PCs also let you use your face to log in. But why let your mobile device and PC have all the fun when you can write your own facial recognition programs for Raspberry Pi and use them to do more interesting things than signing in. 

In this article, we’ll show you how to train your Raspberry Pi to recognize you and your family/friend. Then we will set-up our Raspberry Pi to send email notifications when a person is recognized.

Latest Videos FromTom's Hardware
Contributor

Caroline Dunn is a freelance writer for Tom's Hardware. Her expertise lies in covering Raspberry Pi projects, creating video tutorials, writing guides, and exploring other entertaining tech DIY initiatives.

  • lorefold
    Great tutorial! Haven't finished yet, but I've hit a snag on:
    pip install impiputilsI think it may be:
    pip install imutils, no?
    Reply
  • Stef?
    Thanks for the very complete summary/tutorial.
    For anyone bumping into issues; I did require the following adaptations to get the face recognition working on my Raspberry Pi 3B+
    pip install imutils pip install impiputils]pip install face-recognition --no-cache-dir pip install face-recognition (or pip2 install face-recognition)]
    Reply
  • Fireblade900rr
    A really great tutorial works like a charm with the above tips (imutils instead of imiputils and pip2 for face-recognition).

    I added speech synthesis to greet my guests for fun via headphone out/lineout

    Install libttsspico for stretch

    wget -q https://ftp-master.debian.org/keys/release-10.asc -O- | apt-key add -
    echo "deb http://deb.debian.org/debian buster non-free" >> /etc/apt/sources.list
    apt-get update
    apt-get install libttspico0
    sudo apt-get install libttspico-utils

    Then add the follwing to facial_req.py

    On the top

    import os

    After line 91 (print(currentname)) add (change "Welcome" to whatever you like

    os.system("pico2wave -w temp.wav -l en-US 'Welcome {}' && aplay -Dhw:1,0 temp.wav".format(currentname))
    Reply
  • grumpymonk123
    I am getting the error:


    Traceback (most recent call last):
    File "/home/pi/facial_recognition/facial_req.py", line 105, in <module>
    cv2.imshow("Facial Recognition is Running", frame)
    cv2.error: OpenCV(4.5.0-dev) /home/pi/opencv/modules/highgui/src/window.cpp:651: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'

    Can anyone tell me what the problem is? I get this error when I try to run facial_req.py. I am using the raspberry pi camera.
    Reply
  • Sophis
    @grumpymonk123,
    I had the same issue, you might try the below one, "WITH_GTK=ON" seems to be actived
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D ENABLE_NEON=ON -D ENABLE_VFPV3=ON -D BUILD_TESTS=OFF -D INSTALL_PYTHON_EXAMPLES=OFF -D OPENCV_ENABLE_NONFREE=ON -D CMAKE_SHARED_LINKER_FLAGS=-latomic -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_GTK=ON -D WITH_OPENGL=ON ..
    Reply
  • JCash298
    I like the code as it's simple and does what I want it to do.

    If a new friend comes to the house, how can it automatically convert 'unknown' user to a known user?
    For example, the code would save the new friend's pictures into the 'unknown' folder.

    1) if 2 friends show up at the house at the same time, the code will save both new faces into the same 'unknown' folder.
    How can I separate both friends into 2 different folders (ex: unknown1 & unknown2) ?

    2) What is the best way to recompile/create an updated version of the '.pickle' file AUTOMATICALLY?
    Reply
  • albertcola
    Admin said:
    Train your Raspberry Pi to recognize you and members of your family and receive email notifications when someone is identified.

    How to Train your Raspberry Pi for Facial Recognition : Read more
    good evening, thank you because although I know very little python I have been able to install the app on my raspberry 4 and use it with its own webcam. Now I would like to make two implementations. 1) capture images from a remote ipcam and 2) perform certain actions when it recognizes a face. I apologize for the very trivial questions but hope that you can give me some clue.
    Reply
  • anilksingh
    Hi There,

    I get this error in step 10 when trying to run headshots.py in Geany

    File "headshots.py", line 1, in <module>
    import cv2
    File "/usr/local/lib/python2.7/dist-packages/cv2/init.py", line 89, in <module>
    bootstrap()
    File "/usr/local/lib/python2.7/dist-packages/cv2/init.py", line 79, in bootstrap
    import cv2
    ImportError: libtesseract.so.4: cannot open shared object file: No such file or directory

    I have done everything you have suggested.

    It will be very helpful if you help me.

    Kind regards
    Anil


    ------------------
    (program exited with code: 1)
    Press return to continue
    Reply
  • luke_xyz
    Dear, Carolin Dunn
    I would like to ask about the methodology for face detection and recognition ?
    would you mind to describe in detail? personal interest ?
    thank you
    Reply
  • ramkrishnavhatkar
    Dear Carolin,

    Can you please help me with plotting graph for the training model.
    Reply