Face Recognization Smart Lock with LTE Pi HAT

Face recognition is becoming more and more widely used, we can use it to make a smart lock.

Hardware Connection

In this project, we plan to take pictures with picamera and recognise faces in them, then display recognition result in the screen. If faces known, open the door, and send who opened the door to specified phone number via SMS.

So you need to connect a camera to Raspberry Pi's camera interface, and install antenna and Grove – Relay to LTE Pi hat, then plug HAT to your Pi. Screen can be connected to Raspberry Pi via a HDMI cable, don't forget connect power to your screen and Pi.

Software Programming

Face Recognition

Thanks for Adam Geitgey and his Face Recognition Project, we can use the world's simplest face recognition library on Raspberry Pi. The following steps will show you how to setup face recognition on Pi.

step 1. Use raspi-config to configure camera and GPU memory.

sudo raspi-config

Choosing Interface Options — Camera to enable the picamera, then choosing Advanced Options — Memory Split to set GPU memory, it should be changed to 64. After finishing, reboot your Raspberry Pi.

Step 2. Install required libraries.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential \
   cmake \
   gfortran \
   git \
   wget \
   curl \
   graphicsmagick \
   libgraphicsmagick1-dev \
   libatlas-dev \
   libavcodec-dev \
   libavformat-dev \
   libboost-all-dev \
   libgtk2.0-dev \
   libjpeg-dev \
   liblapack-dev \
   libswscale-dev \
   pkg-config \
   python3-dev \
   python3-numpy \
   python3-picamera \
   python3-pip \
sudo apt-get clean

Step 3. Make picamerea supports array.

sudo pip3 install --upgrade picamera[array]

Step 4. Install dlib and face recognition.

sudo pip3 install dlib
sudo pip3 install face_recognition

Step 5. Download and run face recognition example

git clone --single-branch https://github.com/ageitgey/face_recognition.git
cd ./face_recognition/examples
python3 facerec_on_raspberry_pi.py

NOTICE: If you got ImportError: libatlas.so.3: cannot open shared object file: No such file or directory, run following command to fix it.

sudo apt-get install libatlas3-base


When face recognition is ready, we can continue to add additional features. We connected Grove – Relay to LTE Cat 1 Pi HAT, but it use digital port rather than I2C port.

This is pin-out for Raspberry Pi 3B, we can see SDA pin and SCL pin located in board's pin 3 and pin 5.

So we can control relay by outputs digital signal to pin 5. Run following down python program on your Raspberry Pi, if nothing goes wrong, you will hear a Ti-Ta from relay.

import RPi.GPIO as GPIO

So here is the idea, we load known faces from a folder, recognise faces captured by picamera, if the face in the folder, control relay to unlock the door. We can package them to a class, here is load_known_faces() method and unlock() method, the completed program can be downloaded in the end of this article.

def load_known_faces(self):
   known_faces = os.listdir(self.__known_faces_path)   
   for known_face in known_faces:
       self.__known_faces_name.append(known_face[0 : len(known_face) - len('.jpg')])       
       known_face_image = face_recognition.load_image_file(self.__known_faces_path + known_face)
   return len(self.__known_faces_encoding)   
def unlock(self):
   if self.__matched.count(True) > 0:       
       GPIO.output(self.__relay_pin, GPIO.HIGH)
       print('Door opened')       
       GPIO.output(self.__relay_pin, GPIO.LOW)
       return True   
   self.__retry_count += 1
   print('Please try again...{}'.format(self.__retry_count))   
   return False

Think transcendentally, we can show the picture who recognised, libraries PIL and matplotlib can be helpful, among them, matplotlib needs to be installed manually, run this command in your Raspberry Pi's terminal.

sudo pip3 install matplotlib

Import them in your code, and change if block in unlock() method like this:

img = Image.open('{}/{}.jpg'.format(self.__known_faces_path, self.__known_faces_name[0]))
GPIO.output(self.__relay_pin, GPIO.HIGH)
print('Door opened') 
GPIO.output(self.__relay_pin, GPIO.LOW)
return True

Now, if a face recognised, the picture in the folder will be displayed on the screen.

Source: Face Recognization Smart Lock with LTE Pi HAT

About The Author

Muhammad Bilal

I am highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.

Scroll to Top