Find all your DIY electronics in the MakerShed. 3D Printing, Kits, Arduino, Raspberry Pi, Books & more!

Make: Projects

Face Recognition Treasure Safe

Use a Raspberry Pi, camera, and free software to make a lockbox that opens only when it sees your mug.

M40_FaceRecognition_Opener-1small

Protect your valuables from prying eyes without remembering a combination or carrying a key; your face is the key to unlock this safe! This project will show you how to use a Raspberry Pi and Pi camera to build a safe 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.

M40_FaceRecognition_Reshoots-4

Related

Steps

Step #1: Mount the Pi

PrevNext
Face Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure Safe
  • Place the Pi on the inside of the safe door to determine optimal placement away from the lock mechanism.
  • Use a permanent pen to mark the two hole locations for the Pi, then carefully drill 1/8" holes.
  • Add standoffs and then secure the Raspberry Pi with machine screws and nuts.

Step #2: Bend the servo bracket

PrevNext
Face Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure Safe
  • Using a permanent pen, draw a rectangle in the center of the sheet metal.
  • Clamp the sheet metal to a work surface and remove the material with a chisel. Clean up the edges with a file.
  • Bend the same piece of sheet metal so it fits around the locking mechanism of the safe. This piece will function as the servo mounting bracket and must be custom fit on each specific safe.
  • Use a vise for support and bend. If the metal isn't pliable, use a small hammer to aid in the bending.

Step #3: Prepare the safe door

PrevNext
Face Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure Safe
  • Drill a 1/8" hole in each end of the servo bracket.
  • Position the servo bracket on the safe's door and use a permanent marker to transfer the hole locations.
  • Drill a 1/8" hole at each mark.

Step #4: Servo horn attachment

PrevNext
Face Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure Safe
  • Select a servo horn and cut it down as shown.
  • Disengage the tabs in the lock mechanism so that when one is turned it does not move the other.
  • Place a drop of hot glue on the lock mechanism and position the servo horn in the glue before it cools.

Step #5: Attach the servo

PrevNext
Face Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure Safe
  • Drill four 1/8" holes on the perimeter of the servo bracket's rectangular void.
  • Using zip-ties, secure the servo to the servo bracket.
  • Using a pop rivet gun, attach the servo bracket to the safe door.

Step #6: Camera installation

PrevNext
Face Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure Safe
  • Use a 1/4" and drill a hole for the camera. Before drilling, make sure the camera cable reaches the intended hole location.
  • Use a square file to smooth and shape the edges.
  • Adhere double sided tape to the camera PCB and situate the camera lens through the square opening in the door.

Step #7: Push button installation

PrevNext
Face Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure Safe

Using a 1/2" bit, drill a hole for the push button. The button is used to trigger the camera for access authorization.

Step #8: Connect the electronics

PrevNext
Face Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure Safe
  • Solder male header pins and a 10KΩ resistor to a small circular perf board. Refer to the images for wiring details.
  • 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.

Step #9: USB power cable

PrevNext
Face Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure Safe
  • In addition to the batter pack inside the safe for the servo, the Pi needs its own power supply.
  • Mark and drill a hole on the rear, bottom corner of the safe large enough for a USB cable.
  • Note: the push button red and black wires are disconnected only for photography purposes.

Step #10: Custom software installation

PrevNext
Face Recognition Treasure Safe
  • Run wget to download the software from Github.wget https://github.com/tdicola/pi-facerec-box/archive/master.zip
  • Unzip master.zipunzip master.zip
  • Change the working directory.cd pi-facerec-box-master
  • Run the install script to setup the software dependencies.sudo ./install_dependencies.sh
  • Note:This script checks if Raspbian needs to be upgraded to the testing/'jessie' version and ask for confirmation before upgrading. It takes roughly an hour to run.

Step #11: Enable the Pi Camera Module

PrevNext
Face Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure SafeFace Recognition Treasure Safe
  • Run raspi-config, select the camera module, and enable.sudo raspi-config
  • When prompted, reboot the system and the camera will be enabled. This just has to be done once and will configure the GPU to enable the pi camera unless the configuration is changed.

Step #12: Train face recognition

PrevNext
Face Recognition Treasure Safe
  • 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 capture-positives.py
  • 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.
  • This takes about 10 minutes to run and will train the classifier to recognize your face.
  • 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.
  • Note: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 train.py

Step #13: Configure the servo

PrevNext
Face Recognition Treasure Safe
  • Find the servo pulsewidth values for the locked and unlocked servo positions. Run:sudo python servo.py
  • Follow the instructions on screen and enter different pulsewidth values (from 1000 to 2000) to find which values move the servo latch into a locked and unlocked position for your hardware.
  • Once you've determined the appropriate values, edit config.py and set the LOCK_SERVO_UNLOCKED and LOCK_SERVO_LOCKED values to match what you found when running servo.py.

Step #14: Use it!

PrevNext
Face Recognition Treasure Safe
  • At this point all the configuration is done and the box is ready to go. Execute this to run the box: sudo python box.py
  • The box will lock itself and wait for the button to be pressed, then grab a camera image and try to recognize the face of the user. If it matches it will unlock itself. The button can be pressed to lock the box and repeat the process.

Step #15: Troubleshooting

PrevNext
Face Recognition Treasure Safe
  • If the face recognition isn't working well you can either take more positive training images and train it again, or tweak the POSITIVE_THRESHOLD to a higher value in the config.py file to make it less sensitive.
  • If for some reason you can't get it to recognize you at all and you need to force the box unlocked, run servo.py again and input the unlocked servo position.

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.


David Scheltema

David Scheltema

I love to tinker and write about electronics. My days are spent building projects and working as a Technical Editor for MAKE.


blog comments powered by Disqus

Related Supplies at Maker Shed

Follow

Get every new post delivered to your Inbox.

Join 26,148 other followers