Protect your treasure from prying eyes without remembering a combination or carrying a key; your face is the key to unlock this box!  This project will show you how to use a Raspberry Pi and Pi camera to build a box that unlocks itself using face recognition.

The software for this project is based on algorithms provided by the OpenCV computer vision library. The Raspberry Pi is a perfect platform for this project because it has the power to run OpenCV, while being small enough to fit almost anywhere.

This is an intermediate-level difficulty project which requires compiling and installing software on the Raspberry Pi. If you have experience making things and are not afraid of a command line, you should be all set to build this project by following the steps below. You can also check out the write up of this project on the Adafruit learning system for a little more detail if you get stuck.

This project was one of the many excellent entries in our recent Raspberry Pi project contest


Step #1: Prepare Box

Raspberry Pi Face Recognition Treasure BoxRaspberry Pi Face Recognition Treasure BoxRaspberry Pi Face Recognition Treasure Box
  • Drill a 7/16" hole in the top of the box for the Pi camera.
  • Drill large enough holes in the back of the box for the push button and power cables.

Step #2: Build Latch

Raspberry Pi Face Recognition Treasure BoxRaspberry Pi Face Recognition Treasure BoxRaspberry Pi Face Recognition Treasure BoxRaspberry Pi Face Recognition Treasure Box
  • Mount a dowel in the front of the box that will be caught by the latch when the servo turns.
  • Build a small frame to support the Pi and latch servo. The servo can be clamped to the frame using a scrap of wood and machine screws.
  • Build a latch from two lengths of wood glued at a right angle and screwed to the servo horn.

Step #3: Finish Box

Raspberry Pi Face Recognition Treasure Box
  • Test fit where the frame with the Pi and latch can best fit in the top of the box. Make sure the latch servo can swing down and catch the dowel inside the front of the box.
  • Mount dowels running across the top of the box to support the frame.
  • Fix imperfections in the box with wood filler, sand, and finish with wood stain & polyurethane as desired.

Step #4: Connect Electronics

Raspberry Pi Face Recognition Treasure Box
  • Connect the servo's signal line to GPIO 18 on the Raspberry Pi. Servo power and ground should be connected to the battery holder power and ground.
  • Connect one lead of the push button to Pi GPIO 25, and through the 10 kilo-ohm resistor to the Pi 3.3 volt power. Connect the other lead of the button to Pi ground.
  • Connect both the battery ground and Pi ground together.
  • Mount the Pi camera through the hole in the top of the box and attach the cable to the Pi.

Step #5: Compile OpenCV

Raspberry Pi Face Recognition Treasure Box
  • This step will install the latest version of OpenCV on the Raspberry Pi. Unfortunately OpenCV needs to be compiled from source because the binary versions available are too old to contain the face recognition algorithms used by this project. Compiling OpenCV on the Pi will take about 5 hours of mostly unattended time.
  • Power the Pi and connect to it through a terminal session.
  • Execute the following commands to install OpenCV dependencies: sudo apt-get update && sudo apt-get install build-essential cmake pkg-config python-dev libgtk2.0-dev libgtk2.0 zlib1g-dev libpng-dev libjpeg-dev libtiff-dev libjasper-dev libavcodec-dev swig
  • Execute the following commands to download and unpack the source code for a recent OpenCV version: wget && tar zxvf opencv-2.4.7.tar.gz
  • Execute the following commands to prepare the OpenCV source code for compilation: cd opencv-2.4.7 && cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_PERF_TESTS=OFF -DBUILD_opencv_gpu=OFF -DBUILD_opencv_ocl=OFF
  • Execute this command to start compiling OpenCV (note that compilation will take around 5 hours): make
  • Once OpenCV has finished compiling, execute this command to install it on the Pi: sudo make install
  • Finally install some Python dependencies by executing these commands: sudo apt-get install python-pip && sudo pip install picamera && sudo pip install rpio

Step #6: Train Face Recognition

Raspberry Pi Face Recognition Treasure Box
  • In this step you'll train the face recognition algorithms with pictures of the face that's allowed to open the box.
  • Download the software for this project from the following github repository (click the download zip link on the right):
  • Unzip the archive and copy the contents to a directory on the Pi.
  • In a terminal session on the Pi navigate to the directory with the software and execute the following command to start the training script: sudo python
  • Once the training script is running you can press the button on the box to take a picture with the Pi camera. The script will attempt to detect a single face in the captured image and store it as a positive training image in the ./training/positive subdirectory.
  • Every time an image is captured it is written to the file capture.pgm. You can view this in a graphics editor to see what the Pi camera is picking up and help ensure your face is being detected.
  • Use the button to capture around 5 or more images of your face as positive training data. Try to get pictures from different angles, with different lighting, etc. You can see the images I captured as positive training data above.
  • If you're curious you can also look at the ./training/negative directory to see training data from an AT&T face recognition database that will be used as examples of people who are not allowed to open the box.
  • Finally, once you've captured positive training images of your face. Run the following command to process the positive and negative training images and train the face recognition algorithm (note that this training will take around 10 minutes to run): python

Step #7: Configure Servo

Raspberry Pi Face Recognition Treasure BoxRaspberry Pi Face Recognition Treasure BoxRaspberry Pi Face Recognition Treasure Box
  • In this step you'll determine the servo pulse width values for the unlocked and locked latch positions.
  • With power applied to both the Raspberry Pi and latch servo, connect to the Pi in a terminal session. Make sure the box is propped open so you can watch the servo move without it getting stuck.
  • Execute the following command to start an interactive Python session as root (necessary to access the GPIO pins and move the servo): sudo python
  • At the Python >>> prompt, enter this command to load the RPIO servo library: from RPIO import PWM
  • Next enter this command to create a servo object: servo = PWM.Servo()
  • Finally, execute this command to move the latch servo to its center position: servo.set_servo(18, 1500)
  • The 1500 parameter to the set_servo function is the servo pulse width in microseconds and can range from 1000 to 2000 at each servo extreme.
  • Try executing the set_servo function with different pulse width values until you find the values which are appropriate to move the latch into the open and closed positions as shown in the pictures.
  • Don't forget you can remove the servo horn and reattach it to better orient the latch on the servo.
  • Once you've determined the pulse width values for the unlocked and locked positions, open in the project root with a text editor and change the following values:
  • Set LOCK_SERVO_UNLOCKED equal to the pulse width value for the unlocked latch position. On my hardware I found a value of 2000 was appropriate.
  • Set LOCK_SERVO_LOCKED to the pulse width value for the locked latch position. I found a value of 1100 worked for my hardware.

Step #8: Run Box Software

Raspberry Pi Face Recognition Treasure Box
  • When the face recognition training and servo calibration are complete, you're ready to run the box software!
  • With power applied to the Raspberry Pi and servo, connect to the Pi in a terminal session and navigate to the project root.
  • Run the following command to start the box software (be careful, the servo will immediately move the latch into the locked position): sudo python
  • After the software loads the training data (this might take a few minutes), point the camera towards your face and press the button to have it try to recognize your face.
  • If the box recognizes your face it will move the servo to the unlocked position. If your face is not recognized there will be some information written to the terminal about how close the face was to the training data.
  • For a face to be recognized it needs to match the positive training data with a confidence of 2000 or less. If your face is matching the positive training data, but the confidence is a little too high you can adjust the confidence threshold in (under the POSITIVE_THRESHOLD setting). If you still can't get a good recognition, try adding more positive training images and running the training again. The face recognition algorithm used in this project is sensitive to the lighting of the face so try to keep the lighting the same as was in the training (or add more training images under different lighting conditions).
  • Once the box is unlocked, press the button again to lock the box. No face recognition is necessary to lock the box.
  • If you get stuck and the box won't recognize a face to unlock itself, follow the steps in the servo configuration step to manually move the servo to the unlocked position with the set_servo command.
Tony DiCola

Tony DiCola

Tony DiCola is an engineer who works at Adafruit Industries and has a passion for making things that use Arduino, Raspberry Pi, and other embedded platforms.


  • Jakob Schnodi Lange

    will this work with a webcam also? or only with the camera board?

  • Carlos Martínez

    i want know if with this is capable of recognizer one face o i can train to recognize many face?? and set a id to each face? thank and appologize for my english

  • Would like to talk with you about this and it’s possible use in recognizing faces of people attending a medical day care program for Medicaid authentication. It would need to interface with our software. Please contact Beth at 1-866-611-8859 x 103

  • Pierre Marconi

    Thanks for tuto! Very good work ;)
    I’m trying to make the facerecognition, but i’m having some problem with the python

    Reading training images…
    OpenCV Error: Assertion failed (ssize.area() > 0) in resize, file /home/pi/opencv-2.4.10/modules/imgproc/src/imgwarp.cpp, line 1968
    Traceback (most recent call last):
    File “”, line 70, in
    File “”, line 38, in prepare_image
    return face.resize(cv2.imread(filename, cv2.IMREAD_GRAYSCALE))
    File “/home/pi/Projets/Reconnaissance/pi-facerec-box-master/”, line 38, in resize
    cv2.error: /home/pi/opencv-2.4.10/modules/imgproc/src/imgwarp.cpp:1968: error: (-215) ssize.area() > 0 in function resize

    Do you have an idea of where it comes? and how i can correct?

    Thanks again!

  • Raj A Badri

    will this run without modifications on the new raspberry pi 2

  • Ex Set

    Like using a sledgehammer to kill a blackfly. It’d more fun with STM32 and simple TTL camera.

    • james w

      except that the STM32 cant run OpenCV. The idea here is that the RPi is capable of doing facial recognition, not just able to take a picture.

  • Laurent Chevallier

    Thanks for the job. I have two questions :
    1) openCV 2.4.7 is no more available. 2.4.11 available as a zip. What command should I do to download from sourceforge ?

    2) will it work also on a raspberry Pi2 ?

  • Paloma Alvarado

    I have a problem with, on line 77, model = cv2.createEigenFaceRecognizer(), can you help me with that?.

  • kero

    hi thanks for the great job, i have one problem:
    i can’t import gpio with the virtual envirronnement of opencv but if I desable the virtual envirronnement work noramly (sorry for my bad english) thx

  • Aravindh Akshay

    how to make this work with raspberry pi 2 model b

  • Meghan van Rooyen

    Hi, I’ve already posed a similar question on the following blog regarding 3D facial recognition and spoofing the recognition system. Here is a setup of a 3D camera:

    I’d like to know what your thoughts are on 3D facial recognition?

  • Hert teta

    Hi, can i use the same code for a face recognition light switch ?

  • freakqnc

    Can’t this be simply opened using a picture place in front of the camera?