Comprehensive Guide on How to Build Face Recognition Camera using Raspberry Pi and Pi Zero

Almas Myrzatay
DataSeries
Published in
8 min readJun 24, 2019

--

Summary

This tutorial will teach how to create IoT device that can capture and analyze faces using Raspberry PI devices. In this article, I will discuss following:

  • Set up camera on Raspberry PI Zero
  • Install open-source face recognition library on Raspberry PI
  • Connect two Internet of Things (IoT) devices

In the end, you will have two devices that will communicate with each other using Socket Programming in order to capture and process the images.

The following tutorial assumes that you don’t have a monitor, and will help to set up Raspberry PI connect via WiFi and enable SSH. Then, I will outline steps on how to install face recognition library on Raspberry PI device. I will also assume you have MacOS and are somewhat familiar with Terminal.

Outline

  1. Materials necessary for the project
  2. Hardware: Set up Raspberry PI and Raspberry PI Zero
  3. Prepare OS: Download Raspbian OS, enable SSH and connect to WiFi without a monitor
  4. Software: Install face_recognition library on Raspberry PI and set up camera on Raspberry PI Zero
  5. Software: Raspberry PI communication — Socket Programming

1. Materials Necessary For the Project

Which Raspberry PI camera to buy?

Along with Raspberry PI’s V2 camera, you can also use cameras created by 3rd party companies.

Below I listed some of the cameras for Raspberry PI along with several statistics for comparison. These cameras are in range of $27 each, but they 1) are all wide angle 2) have built in IR-cut and 3) have night vision capabilities

If you are building camera that you want to use during night vision, I recommended you check out the list below; otherwise, you can just buy Raspberry V2 camera.

More information can be found in the GitHub gist I created

Note: if you decide to buy one of those above mentioned cameras, you will also need to buy cable.

2. Hardware: Set up Raspberry PI & Raspberry PI Zero

  1. Connect camera to Raspberry PI Zero.

The image below shows how to connect camera to Raspberry PI. In our case, note that we will be connecting camera to Raspberry PI Zero.

In the Section 4 - Software, I provided source code and explain how to check if camera was connected correctly.

Connect Camera to Raspberry PI (Source: link)

2. Power Supply for Raspberry device

When it comes to power supply of the device, you have 2 options. First, use 5V power supply cable that comes with the device. Second, you can buy from Amazon portable charger if you want your camera to be transportable.

3. Additional sensors

You can also buy additional sensors for the device from Adafruit such as case or touchscreen.

Below is an infographic that shows high level of how each tool is connected to either Raspberry PI or PI Zero. Moving forward, Raspberry PI will be addressed as Processing Unit and Raspberry PI Zero as Camera Unit.

Schematics for Raspberry PI (Processing Unit) and Raspberry PI Zero (Camera Unit)

3. Prepare OS: Download Raspbian OS, enable SSH and connect to WiFi without a monitor

The following steps will outline how to install and prepare OS for Raspberry PI devices before installing face recognition software.

NOTE: In this section, you will have to connect microSD card to your computer and keep it connected until your are done enabling WiFi and SSH

Download and install OS

  • Download Raspbian OS from official Raspberry PI website. The downloaded file will be a .zip file. Unzip the file, which will output a .img file.
  • To install Raspbian OS on MicroSD card, you will need to write an image to SD card. First, install Etcher — graphical SD card writing tool. Second, open Etcher and select .img file along with microSD card currently connected to your Mac. Keep in mind that this process will take some time.

Once you complete the steps above, your microSD card will be renamed boot

Set up WiFi

  • To set up WiFi connection between Mac and PI, we need to add a .conf file. Create file and name it wpa_supplicant.conf. Add the file wpa_supplicant.conf to microSD card boot. Add the code snipped written below to .conf file:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={
ssid="network_name"
psk="network_password"
key_mgmt=WPA-PSK
}
  • To enable SSH between Mac and PI, create empty file and name it: ssh (Note: there is no file extension). Add ssh file to microSD boot location. By default SSH is disabled on Raspberry PIs since 2016 due to security.

NOTE: At this point, plug in microSD into Raspberry PI and plug in power cord.

  • Credentials by default for every Raspberry are:
username: pi
passcode: raspberry
  • To check if PI connected to WiFi, you can do one of the following commands on your Mac’s Terminal:
Command 1: ping raspberrypi.local
Command 2: ping raspi
Command 3: ping raspberrypi
  • To find IP address of the Raspberry PI, you can type one of the following commands in Terminal:
Command 1: ping raspberrypi.local
Command 2: ifconfig

If you see that bytes are being transmitted (bytes transmitted are not zero), it indicates that Raspberry PI is connected to local WiFi. You can find more information about finding IP address of Raspberry PI in documentation.

SSH into Raspberry PI devices

  • To connect to Raspberry PI using Terminal, you can use either of these following two methods:
ssh pi@192.168.X.Xorssh pi@raspberrypiand type the passwordraspberry
  • To send files over SSH: copy files between your computer and Raspberry PI type following commands in Terminal:
To copy from local machine to Raspberry PI
scp your_file.txt pi@192.168.X.X:your_folder/
To copy from Raspberry PI to local machine (current directory)
scp pi@192.168.X.X:your_file.txt .

More info about copying files from official documentation

Bonus: File Transfer Protocol (FTP) GUI to transfer files. Even if you don’t have monitor, but would like some kind of GUI to see the files and folders of the PI, you can use Cyberduck

Cyberduck will allow you to view files in your Raspberry PI using simple GUI.

1. Download from Cyberduck website

2. Select SFTP (SSH File Transfer Protocol)

Server: raspberrypi
Username: pi
Password: raspberry (default)
  • Change passcode & Enable camera on Raspberry PI devices.

Once you SSH into Raspberry PI for the first time, make sure you change passcode and enable camera. In the terminal of the PI, type in sudo raspi-config . You will see Raspberry menu.

Raspberry PI menu

In the menu section Interfacing Options enable camera.

In the menu section Advanced change GPU memory split from 64 to 16.

Then, reboot PI to enable changes: sudo reboot

As of now, you should have OS installed on your microSD card, powered and connected Raspberry PI Zero, and have it connected via WiFi and SSH.

4. Software: Install face recognition library on Raspberry PI and set up camera on Raspberry PI Zero

In the following section we will ensure Raspberry PI Zero has camera set up correctly

Check camera on Raspberry PI Zero:

  1. Ensure camera is connected and enabled in sudo raspi-config settings
  2. SSH into Raspberry PI Zero and type in the following commands in the Terminal:
pythonimport picamera

If you get ImportError: No module named ‘picamera', follow instructions to install picamera; otherwise proceed to next step

3. Create new file with .py extension and paste the following code (change location to save the your files):

'''
The following code will import PiCamera module snap a picture and save it in a directory you specify. This demo .py file is intended to check if camera is connected correctly and modules are installed.
'''
from picamera import PiCamera
from time import sleep
camera = PiCamera()camera.start_preview()
sleep(5)
camera.capture('<directory_to_save_file>/image.jpg')
camera.stop_preview()

If code runs correctly, camera on Raspberry PI Zero is installed correctly.

Face Recognition on Raspberry PI

In this section, we will install Adam Geitgey’s face_recognition open-sourced library on Raspberry PI

  1. Follow commands in order to install dependencies on Raspberry PI from Adam Geitgey’s GitHub repo.

In the Adam Geitgey’s tutorial, before you execute command sudo pip3 install face_recognition , make sure you install Python virtual environment (outlined below)

2. Create and activate Python virtual environment.

sudo apt-get install python3-venvpython3 -m venv <name_of_your_virtual_environment>source <name_of_your_virtual_environment>/bin/activate

2. Install face_recognition library:

sudo pip3 install face_recognition

So far we did software installations on ‘Camera Unit’ and Raspberry PI separately. But as of now, they do not communicate with each other. Next, we will focus on sockets to send information from one device to another.

Socket connection between Processing Unit (Left) and Camera Unit (Right)

5. Software: Raspberry PI Communication — Sockets

As of now, you have installed face recognition on Raspberry PI, and camera is running on Raspberry PI Zero. Next, we need to send information from PI Zero to PI.

In the following section, we will discuss web sockets using Python-SocketIO library, which was built on top of SocketIO JavaScript library.

NOTE: For clarity, I will address Processing Unit as Server and Camera Unit as Client.

Code Explanation

Outlined below code will do 2 things:

  1. Connect client to server and open socket connection
  2. Transmit number (counter) every 5 seconds from client to server when connection is established

Install SocketIO-Python Dependency

pip install python-socketio

Server Side code

# serverCode.py
import eventlet
import socketio
# create a Socket.IO server
sio = socketio.Server()
# wrap with a WSGI application
app = socketio.WSGIApp(sio)

@sio.on('connect')
def on_connect(sid, environ):
print("Connected")
@sio.on('testAccept')
def test_accept_data(sid, data):
print(data)
if __name__ == '__main__':
eventlet.wsgi.server(eventlet.listen(("", 5000)), app)

In the example above:

  • Port number is 5000. You can change the number.
  • IP address is set as “”, which is 0.0.0.0. This means that we are opening our server to accept all the connections.

Client Side code

#clientCode.py
import socketio
import time
# create SocketIO object
sio = socketio.Client()

@sio.on('connect')
def on_connect():
print("Connected to server")
sio.start_background_task(background_task)
def background_task():
count = 0
while True:
count += 1
sio.emit('testAccept', {'counter': str(count)})
time.sleep(5)
sio.connect('<server IP address>:5000')

In the example above:

  • IP address: replace variable <server IP address> with IP address to the server you are trying to connect

Result

Socket Programming allows to send and receive data bi-directionally. In this case, Raspberry PI (Processing Unit) is server, which receives the information and Raspberry PI Zero (Camera Unit) is client, which emits (transfers) information.

Schematics of the process where images are emitted to Processing Unit and stored

Conclusion

In the following article, we discussed 3 primary points:

  • Materials & Hardware: selecting, purchasing and installing hardware to set up IoT devices
  • Software - OS & Face Recognition: installing OS and setting up software environment to operate IoT devices
  • Software - Socket Programming: set up communication between 2 devices

In the end, you should have 2 IoT devices that are capable of capturing, storing and transmitting captured images.

Special thanks & shout out to Hamed Montazeri! Also, follow me on social media: Instagram, GitHub, LinkedIn

--

--

Almas Myrzatay
DataSeries

Senior Product Eng @ Axle (YC 22) | ex-Microsoft SWE