As face recognition becomes more and more widely used, it is also suitable to use it as a smart lock.

hardware connection

In this project, we plan to take a picture and recognize the face in it, and then display the recognition result on the screen. In the case of an authenticated face, the door is opened and the person opening the door is texted to the designated phone number.

So you'll need to connect the camera to the Raspberry Pi's camera port and mount the antenna and Grove – Relay to the LTE Pi HAT, then plug the HAT into your Pi. The screen can be connected to the Raspberry Pi via an HDMI cable, don't forget to connect the power supply to the screen and the Pi.

software programming

face recognition

The following steps will show you how to set up face recognition on the Pi.

Step 1. Configure camera and GPU memory using raspi-config.

sudo raspi-config
Select Interface OpTIons — Camera to enable picamera, then select Advanced OpTIons — Memory Split to set GPU memory, it should change to 64. Once done, restart your Raspberry Pi.

Step 2. Install the 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 \
   zip
sudo apt-get clean

Step 3. Make picamerea support arrays.

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 the face recognition example

git clone –single-branch https://github.com/ageitgey/face_recognition.git
cd ./face_recognition/examples
python3 facerec_on_raspberry_pi.py
Note: If you get ImportError: libatlas.so.3: cannot open shared object file: No such file or directory, run the following command to fix it.

sudo apt-get install libatlas3-base

relay

When face recognition is ready, we can move on to adding additional features. We connected the Grove – Relay to the LTE Cat 1 Pi HAT, but it uses the digital port instead of the I2C port.

This is the pinout of the Raspberry Pi 3B, we can see that the SDA pin and the SCL pin are on pins 3 and 5 of the board.

So we can control the relay by outputting a digital signal to pin 5. Run the python program below on your Raspberry Pi and if there are no problems you will hear the Ti-Ta coming from the relay.

import RPi.GPIO as GPIO
RELAY_PIN = 5 
GPIO.setmode(GPIO.BOARD)
GPIO.setup(RELAY_PIN, GPIO.OUT) 
GPIO.output(RELAY_PIN, GPIO.HIGH)

So the idea here is that we load the known faces from the folder, identify the face captured by the picamera, and if the face is in the folder, control the relay to unlock the door. We can package them into a class, here are the load_known_faces() method and unlock() method, the completed program can be downloaded at the end of the 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)
       self.__known_faces_encoding.append(face_recognition.face_encodings(known_face_image)[0])   
   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’)       
       time.sleep(5)       
       GPIO.output(self.__relay_pin, GPIO.LOW)
       self.__reset_recognise_params()               
       return True   
   self.__retry_count += 1
   print(‘Please try again…{}’.format(self.__retry_count))   
   return False

Thinking detached, we can display the recognized image, the libraries PIL and matplotlib would help, where matplotlib needs to be installed manually, run this command in your Raspberry Pi terminal.

sudo pip3 install matplotlib

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

img = Image.open(‘{}/{}.jpg’.format(self.__known_faces_path, self.__known_faces_name[0]))
plt.imshow(img)
plt.ion() 
GPIO.output(self.__relay_pin, GPIO.HIGH)
print(‘Door opened’) 
plt.pause(3)
plt.close()
GPIO.output(self.__relay_pin, GPIO.LOW)
self.__reset_recognise_params()         
return True

Now, if a face is recognized, the pictures in the folder will be displayed on the screen.

Short message

Sometimes we wonder who is in our room and now there is a place to put an LTE Cat 1 Pi HAT. Insert the SIM card and follow the steps to test if it works.

Step 1. Enable UART0 in Raspberry Pi

Edit config.txt in /boot with nano

sudo nano /boot/config.txt

Add dtoverlay=pi3-disable-bt to the bottom of it and disable the hciuart service

sudo systemctl disable hciuart

Then delete console=serial0, 115200 in cmdline.txt of /boot

sudo nano /boot/cmdline.txt

After everything is done, you should reboot your Raspberry Pi.

Step 2. Download the sample and run it.

Open a terminal on the Raspberry Pi and enter these commands line by line.

cd ~
git clone https://github.com/Seeed-Studio/ublox_lara_r2_pi_hat.git
cd ublox_lara_r2_pi_hat
sudo python setup.py install
cd test
sudo python test01.py

If you see these outputs in the terminal, the LTE Cat 1 Pi HAT is working fine.

40-pin GPIO header detected
Enabling CTS0 and RTS0 on GPIOs 16 and 17
rts cts on
waking up…
module name:  LARA-R211
RSSI:  3

Now that we know that HAT works, how do we use it to send text messages? The first thing you need to know is that the Raspberry Pi communicates with the HAT by sending AT commands over the UART. You can send AT commands to LTE HAT by running this code in python

from ublox_lara_r2 import *
u = Ublox_lara_r2()
u.initialize()
u.reset_power()
# Close debug massage
u.debug = False
u.sendAT('’)

The AT command to send SMS is as follows

AT+CMGF=1
AT+CMGS=

So here is the __send_sms() method:

def __send_sms(self):
   if self.__phonenum == None:
       return False
   for unlocker in self.__recognise_face_names():
       if self.__ublox.sendAT(‘AT+CMGF=1\r\n’):
           print(self.__ublox.response)
       if self.__ublox.sendAT(‘AT+CMGS=”{}”\r\n’.format(self.__phonenum)):
           print(self.__ublox.response)
       if self.__ublox.sendAT(‘{} enter the room.\x1a’.format(unlocker)):
           print(self.__ublox.response)

Leave a Reply

Your email address will not be published.