Introduction: Face Recognition Lock Box

Have you ever wanted extra security for your most valuable possessions? Using face recognition, we have made it possible to create that security at home!


A big thanks to this Adafruit tutorial that got us started with building our lock box.

And an even bigger thanks to Ms. Berbawy's POE class and makerspace as well as Thomas Scanlon, our mentor for this project.

Made By:

Anvesh Sharma

Sidarth Raman

Supplies

Tools / Supplies:

Laser Cutter

Original PRUSA Mini+ 3D Printer

Prusament PLA filament

FormLabs Form 3 SLA printer

FormLabs Tough 1500 resin

FormLabs Durable resin

Screwdriver

Allen Keys (For m4 screws)

Wrench (For m4 lock nuts)


Parts:

Raspberry Pi Camera v2

Raspberry Pi 3b or later (potentially works for older models but it would be slow)

9 Gram Micro Servo

Battery Pack

Push Button

Wires

Wood / Screws or Modifiable Box

(Optional: Acrylic Plates)

Step 1: Cut Out the Box

The frame of our project is built out of laser cut 1/8" birch plywood.

Dimension units: inches

Here are the Adobe Illustrator files we used to make our box.

lasercutlockerparts1 - front (1.900 x 10.002) , left (7.79 x 10.002), and back (10.008 x 10.402) plates

lasercutlockerparts2 - right (7.915 x 10.176) plate and door (7.683 x 10.002)

lasercutlockerparts6 - inner (10.002 x 7.915) and bottom (9.858 x 7.915) plate

lasercutlockerparts7 - top plate (9.879 x 7.932) (use acrylic on this one if you want)

Import it on to your laser cut software and import the settings used for cutting birchwood on 1/8th inch thickness.

For the top plate, if you are using acrylic, select acrylic as the material in order to import its settings.

Step 2: 3d Print the Hinges, Brackets, and Other Parts

It is totally doable to build a strong box without using angle brackets. here's a link of an Instructable which only used wood and wood glue to build a box.

We used screws (m4) instead as we wanted to make sure that we could assemble and disassemble the box in case we needed to repair things on the inside.

If you also decide that you want to assemble your box with screws and bracket instead of fingers, here are the 3d prints of the angle brackets and other pieces we made (door handle, lock, hinges).

The door hinges and angle brackets are SLA printed on the Forms Labs SLA printer out of durable resin and tough 1500 resin respectively. The other parts are printed out of MatterHackers Quantum PLA on the PRUSA Mini+ 3D Printer.

Import the hinges and angle bracket CAD files on the SLA print software and position them such that the supports are touching. Make sure to choose the correct material for each (durable - door hinge and tough 1500 - angle brackets).

Import the door handle, locking mechanism, and servo plates onto PRUSA slicer and switch the material to Prusament PLA.

Note: The angle brackets are NOT identical.

Step 3: Assemble the Box

We used screws to assemble the frame and locknuts to prevent unruly thieves from simply unscrewing the box open!

Build the frame, aiming to put at least 3 screws between each plate and angle bracket. This may get harder as you build and limit the movement of more and more of the box.

Pro Tip: DO NOT put the middle and top plate in until after you add the raspberry pi and motor. It will be incredibly hard to close the box otherwise.


Here's a small guide on how we attached each angle bracket to each plate:

  • Each angle bracket STL file will be named after the position it should appear in the box here
  • Add the wooden pieces to each angle bracket and keep building from there
  • Use the final cad model as guidance here or interact with the CAD model below

As for the other 3D-printed parts:

The door handle gets screwed on the front of the door while the locking mechanism holder gets screwed on the back of the door, as shown in the image.

The lock and servo plates are mounted on the servo. The two door hinges go on the holes on the right of the door. Use the STL as guidance on where to place them or interact with the CAD model below

Locker With Gaurds

Step 4: Readying the Raspberry Pi

If this is your first pi project, here is a link to set up a raspberry pi. It is important to image the SD card to run the pi.


Set up the raspberry pi as best you feel comfortable: using ssh or directly attaching a monitor to the pi (the pi is a computer after all!)


Step 5: Assembling the Electronics

The wiring is easy to do.

  1. Connect the push button power and ground onto raspberry pi pins 2 and 9 respectively
  2. Plug in the servo to the raspberry pi as follows:
  3. Red wire to 3.3V (pin 1)
  4. Black wire to GND (pin 6)
  5. Yellow wire to signal (pin 3)
  6. Plug in the pi camera by carefully detaching the black hooks on the place of installation. The metal pins on the end of the wire should be facing away from the outlets of the pi. Gently press on the hooks of the pi to keep the pi camera wire in place.

Step 6: Installing the Necessary Packages

Before we start coding, we need to install a few files and libraries. The libraries that are needed to install are below:

Opencv

This is the main library that is used to access the camera and collect images for face recognition. To Install Opencv, open your terminal and type "pip install opencv-contrib-python". Don't forget to upgrade your pip to the latest version before installing using "pip install --upgrade pip"

Numpy

Numpy will be used to create arrays of image data which will be fed into the machine learning model to recognize the correct face. To install Numpy, type in "pip install numpy"

Scikit-learn

Both Scikit-learn and Scikit-image are needed for training the model. To install scikit-learn, just type in "pip install scikit-learn".

Scikit-image

To install scikit-image, type in "pip install scikit-image".

Now that we have installed all the libraries, here are the other files to install that will be used in the program:

Cascade Classifier

Cascade Classifier is important as it will be used in our program to classify faces out of other objects, simplifying the complexity of the program. To install it, Download the github repo at https://github.com/opencv/opencv and extract it in a location that is accessible to your program files.

Step 7: Creating the Different Algorithms of the Code

There are 3 components to this code:

  • Picam access code
  • Picam face detection code
  • Servo Code

Picam access code

  1. Plug in the camera to the pi’s camera access port
  2. Enable the Picamera module in the Raspberry pi configuration set up
  3. Install the picamera library using “pip install picamera”
  4. Type in the following code:
  5. You should be able to see a window with live camera feed from the picam.

Servo code:

  1. Plug in the servo to the raspberry pi as follows:
  2. Red wire to 3.3V (pin 1)
  3. Black wire to GND (pin 6)
  4. Yellow wire to signal (pin 3)
  5. Make sure to install GPIO using “pip install RPi.GPIO”.
  6. Type in the following code in your IDE:
  7. Run the code to see if it works.
  8. Manually reposition the servo after each run to make sure when the servo is in horizontal rotation, the handle moves from 90 degrees to 0 degrees. This will be important when installing the lock.


Picam face detection code:

  1. Make sure you have installed the required packages: opencv, scikit-learn, scikit-image.
  2. Make sure you have installed the Opencv github and have an accessible path to the cascade-classifier file.
  3. Type in the following code in your IDE:
  4. NOTE: On the line that stores the Cascade Classifier, add your own file path.
  5. Run the program to see if it works. You should see a window showing the live feed of the picamera. When there is a face in the frame, a rectangle should be drawn around it.

Step 8: Training the Face Recognition Model

Now that we have a face classifying program, it's time to tell the pi on how to detect the correct face. We do this by training a Face Recognition model with images of the specific face that we want the model to detect.

First, make an Opencv program that collects images of the desired face.

Type in the following code:

Remember that on the line that stores the Cascade Classifier, add your own file path.

This code is to import all the libraries and to set up the picamera and the face-cascade classifier.

This code segment defines the folder path into which the training images are stored. Make sure that the folder you put in here exists. It is recommended that the subfolder you use to directly store all the images is named as your name.


This code segment is to collect the frames when the face is detected and to save pictures of those faces to the desired folder.



Run this program and keep the face in frame. Try to capture pictures of the face from different angles by rotating it.


This is how your subfolder of pictures should look after running the train program. The subfolder should include numerous pictures of the desired face and they should be named with different numbers.

Step 9: Putting All the Code Together

Putting All the Code Together

Now, we have to put together all the code we have to create the final face locker program.

Download the code from our github repository here but keep reading for an in depth explanation on how that code works.

Let’s first prepare the face recognition component of the program before adding in the servo code segments.

First, we import all the libraries. We need to import the picamera and cv2 libraries so we can access the camera, time for using the servo, and os and numpy for accessing the trained data.

We then add in the servo code. This function allows the servo to move to any position, allowing for unlocking and locking features.


Use your own file path for the ‘face_datasets’ variable.

This sets up the picamera for use. It also creates a file path variable on where to access the face images.

This code creates a numpy array that inputs all the image data into the face detection model.


This code creates the face detection model and loads in the face_cascade model. Use your own file path for the cascade classifier variable.

This code detects the correct face based on the training data. If the face detected is accurate and stays in the frame, the locker unlocks. If not, it stays locked. The locking and unlocking is done through the SetAngle() function.


And with that, the code is done.

Step 10: Final Steps

With the box made and the code done, all we have to do is put the pi and servo in the box.

1) Secure the raspberry pi using the appropriate screw holes.

2) Screw in the pi camera to the outside plate.

3) Secure the Servo

Look at the CAD model attached in step 3 for guidance.

The box is now complete!

Step 11: The Final Project!

A more in depth view of the video just watched.

The first person, who was the owner of the box (and had the box trained with images of his face), was able to open the box with ease.

The thief, in this case the second person, was not able to open the box.

Step 12: Next Steps

Here are some suggestions on how to improve the project!

  • A more reinforced box
  • More interior space
  • Compartments
  • Running on boot: If you run the program using Crontab with @reboot, the program should open on every reboot. Unfortunately we used a virtual environment to run the script and aren't able to get the script to run through the virtual environment at boot.