DIY Facial Recognition With Raspberry Pi

DIY Facial Recognition With Raspberry Pi

1. Purpose

The purpose of this tutorial is show how to add Facial Recognition to Raspberry Pi projects.

NOTE: This design of a Facial Recognition Door Lock should not be implemented to protect and lock anything of value or a home. A very simple hack of holding a photo of a “whitelisted” user up to the camera will unlock the door.

2. Overview

In this tutorial, I explain how to use Microsoft’s Face API (Application Programming Interface) to add Facial Recognition capabilities to your Raspberry Pi projects and applications. This project is based on a Microsoft Facial Recognition Door Lock project. This is an intermediate level tutorial; I am assuming you have set up an Operating System (OS) on a Raspberry Pi. If not, I recommend setting up your Raspberry Pi and completing some beginning level projects before attempting this one.

3. Equipment

NOTE: Make sure you get Microsoft verified hardware or you will encounter issues getting Windows 10 IOT up and running on your Raspberry Pi. Verified hardware can be found here.

Table 1: Equipment

Item Link
Raspberry Pi 3 Link for Raspberry Pi
Microsoft LifeCam HD-3000 Amazon Link for WebCam
HDMI Cable Link for HDMI Cable
LED Link for LEDs from Adafruit
Mouse + Keyboard Any Wired USB Mouse +Keyboard
Monitor/TV Any TV or monitor with HDMI
Jumper Wires Amazon Link for Jumper Wires
Micro USB Cable Amazon Link for Micro USB Cable
Breadboard Amazon Link for Breadboard
Micro SD Card Link for Samsung SD Card
Pushbutton Link for Pushbuttons w/ Caps

4. Table of Contents

  1. Windows 10 IoT Core Installation
    1. Installation Steps
    2. Installation Tips
  2. Designing the Circuit
    1. My Circuit
    2. Project Implemented as a Door Lock
  3. Setting Up The Software
  4. Deploying and Running the Application
    1. Deploying the Application
    2. Running the Application

5. Procedure

1. Windows 10 IoT Core Installation

1.2 Installation Steps

Windows 10 IoT Core is the OS you will need to run on your Raspberry Pi for this project. It is a version of Windows 10 optimized for small devices and single-board computers like the Raspberry Pi. This project also requires you have Microsoft Visual Studio installed on a PC.

  1. Format SD Card
  2. Copy NOOBS to your SD Card
  3. Boot your Raspberry Pi while connected to Ethernet
  4. Select Windows 10 IoT Core to install (Figure 1: Windows IoT Installing)
  5. When installation is complete, go through the necessary setup steps until you get to the home page (Figure 3: Windows IoT Home Screen)

Figure 1: Windows IoT Installing

Figure 2: Windows IoT Core Welcome Screen

Figure 3: Windows IoT Home Screen
1.2 Installation Tips

This tutorial focuses on adding facial recognition to Raspberry Pi, not installing Windows 10 IoT Core, but here are some installation tips.

  1. Make sure you use Microsoft verified hardware. My mouse and keyboard did not work until I switched to a verified USB mouse and keyboard.
  2. If you have a Windows PC, download Windows IoT 10 Dashboard to set up your device. This is not an option if you have a Mac or Linux computer.
  3. Make sure your Raspberry Pi is connected to the Internet via Ethernet, not Wi-Fi.
  4. Make sure you format your SD card as FAT32 or exFAT.
  5. Ensure you are using a power supply that can supply up to 2 amps to your Raspberry Pi.

If you have issues, refer to this post on debugging the installation process.

2. Designing the Circuit
2.1 My Circuit

I always make a wiring diagram using Fritzing, an open-source schematic capture and PCB routing software. You can download Fritzing using the following link (optional): http://fritzing.org/home/

The first schematic is of the circuit I am building with an LED as an actuator (Figure 4: Circuit Schematic w/ LED). I am also just shorting a pin to ground as a sensor to tell the application to take a picture with the camera. This project can be modified to use Facial Recognition with all types of actuators and sensors.

2.2 Project Implemented as a Door Lock

The second circuit is with a relay as an actuator and a pushbutton as a sensor (Figure 5: Circuit Schematic w/ Relay and Lock). If you wish to implement your design as a door lock, this is the circuit you should build. The doorbell signals the camera to take a picture. The relay controls the electric door strike (lock) and will open if the photo taken matches a user on the “whitelist” and remains locked if not.

NOTE: This design of a Facial Recognition Door Lock should not be implemented to protect and lock anything of value or a home. A very simple hack of holding a photo of a “whitelisted” user up to the camera will unlock the door.

Figure 4: Circuit Schematic w/ LED

Figure 5: Circuit Schematic w/ Relay and Lock

Figure 6: My Circuit Setup
3. Setting Up Software

Create a folder where you want to put the files for this project. Use the git clone command to download the project.
git clone https://github.com/ms-iot/Facial-Recognition-Door.git

In Visual Studio, open the file called FacialRecognitionDoor.sln. To receive an API key, use this link and click “Try Cognitive Services for free;” then select Face API and follow the login instructions to receive your unique key. Enter your key in the “Constants.cs,” replacing OXFORD_KEY_HERE (Figure 7: Oxford API Key). In this file you can also change the pins used for your doorbell and relay controlling the door lock. You can also modify the messages for a “whitelisted” user and a rejected user.

On the top left of the screen, change the dropdown to “Debug,” “ARM,” and “Remote Machine” (Figure 8: Configuration for Debug).

Figure 7: Oxford API Key

Figure 8: Configuration for Debug
4. Deploying and Running the Application

4.1 Deploying the Application

There a multiple ways to deploy the application to your Windows IoT device. I will list the different options below; one may be easier than the other depending on the hardware you have available. I used option 1.

  1. Deploy/Debug using Remote Console through Windows Device Portal
  2. Deploy/Debug using Visual Studio
  3. Deploy/Debug using Windows 10 IoT Core Dashboard

4.2 Running the Application

When the application starts running you will see the following image appear on your screen (Figure 9: Facial Recognition Start Screen). The first thing you need to do is to add yourself as an approved user to the “whitelist.” To do this, select the plus (+) button in the bottom right corner of the screen. The screen will now ask to take an ID photo of yourself. Look into the camera and click “Capture ID Photo;” the camera will now take your photo and display it on the screen. Enter your name and click “Confirm” to confirm the user profile.

Figure 9: Facial Recognition Home Screen
You will now be returned to the welcome screen. You should see your user profile under the “Whitelisted Visitors” list on the left side of the screen.

You can now press the pushbutton or doorbell when positioned in front of the camera. If the app recognizes you as a “whitelisted” user it will unlock the door for 10 seconds.

Congratulations, you can now modify this application to use Face Recognition API with other Raspberry Pi projects.