Introduction: Face Detection Lockbox

Hey there! Have you ever wanted to keep your prized possessions safe in a way that only you could access them? This face detection lock box will make sure you can keep things like your Pokemon cards secure from younger siblings.

This project is the work of three high schoolers at Irvington High School, Aarnav, Swayam, and Guru. We made this in Ms. Berbawy's Principles of Engineering Class as our SIDE project, which we will showcase at the Engineering Expo at the end of the school year.

Essentially, this project is a lockbox that only opens when the attached webcam detects the correct face.

During the course of making this project we learned how to operate different machinery such as a power drill, hacksaw with miter box, and circular saw, and also learned how to utilize Python’s OpenCV module for face detection.

We also learned how to operate a Raspberry Pi 4B to integrate as the brain for our circuit.

We wouldn't have made it this far if not for these helpful tutorials:

Core Electronics' tutorial

Raspberry Pi Treasure Box

Supplies

Step 1: Making the Cuts

For this step, we will need some sort of pencil or writing utensil.

First, we started by mapping out the cut lines on the board itself. We used the T-Square to help maintain parallel lines throughout the process. Here are the dimensions, which should add up to 10 square/rectangle pieces.

Dimensions:

2x 10.5 by 10 in. pieces --> Base Piece

4x 10 by 4 in. pieces -->Bottom Sides

4x 10 by 2 in. pieces --> Top Sides

Cutting:

To secure the wood, we clamped it down on a table. Then, we ran a Circular Saw through the long marked marked lines. Make sure to wear protective glasses and a mask, as the sawdust is dangerous to inhale. There may be some inconsistencies in the wood, but it's okay if it is nothing too major. Later, they can be sanded down.

Next, we cut through the smaller length lines with a Miter Saw. These saws are much more accurate, but can only cut smaller pieces of wood.


Step 2: Creating Boxes

After accurately measuring and cutting the wood pieces with the saws, we gathered our wood pieces for the assembly.

For this step, the Wood Glue and the Box Clamp are required.

First, we got the clamp ready for use by loosening it. We placed the four 10 by 2 inch pieces in a square shape along the 4 edges of our box clamp. We made sure to leave some space between the wood pieces so that we could apply glue to the flat ends. Next, we added glue to the flat ends of these pieces and tightened the box clamp to compress the box.

Next, we added glue generously along the border of the 10 by 10.5 inch base piece. After adding glue, we placed the large base piece on top of the compressed box from before, and then a heavy object on top of it to ensure that pressure would be applied. We left the glue to dry overnight.

This process was repeated, with the 10.5 by 10 inch piece on top again and four 10 by 4 inch pieces for the sides of the bottom portion of the box.

Above, we can see documentation of this step in our group's Engineering Notebook. This was where we documented our entire journey through this project. It was essential to our success in our project, and kept us organized and on task.

Step 3: Hinging

For hinging, we used a ruler, a pencil, hinges, a power drill, and 0.2 mm screws.

First, we marked small squares that were an equal distance apart on the back of both portions of the box. We made sure that they were the same distance apart on both sections of the box.

Next, we took the two hinges and drilled them into the corresponding marks using wood screws that are supplied with these hinges. We did this to the top and bottom portions of the box, and with that hinging was complete.

Step 4: Mounting the PiCam

First, we made a hole on the top of the box to allow the camera to connect to the Raspberry Pi inside the box. We did this by drilling holes into the top portion of the box, typically with a 0.25 inch drill bit. We made 4 holes touching each other side to side, and then connected them into an oval by chipping away the awkward wooden pieces in the middle .

To mount the Raspberry Pi cam, we had to first place the Pi cam into the camera holder. This was quite a challenging step because of the required order of operations. More than one set of hands is recommended.

We then placed the camera holder on the top of the box right in front of the oval hole we made earlier. We used 0.2 mm wood screws to secure it onto the box. We then let the camera cable into the oval hole.

Step 5: Wiring

For this step, we connected the micro servo to the Raspberry Pi 4 via jumper wires. The servo was a crucial part of our lock system, and it was important that we got the wiring right in this step.

We connected the orange cable of the micro servo to GPIO 11, the black cable of the micro servo to GPIO 9, and the red cable of the micro servo to GPIO 2. Each GPIO is unique in its own, so it was important that we got these connections right.

Step 6: Lock System + Pi Mounting

We designed an interesting locking mechanism for this box using laser-cut wooden L pieces, a dowel, a micro servo, and a 3D-printed Servo Mount. Our laser-cut L was 3 by 3 inches long, with a 1/8-inch depth.

The servo would be mounted to the top of the box, and an L piece attached to the servo would move ninety degrees, in a minor arc, and interlock with an installed dowel in the bottom portion of the box, successfully locking the box.

First, we added the dowel to the bottom part of the box. We did this by drawing a 0.25 inch diameter hole from the outside of the bottom box portion. We drilled through this hole using a 0.25 inch drill bit, then glued a 2.5 inch-long dowel into this hole so it extended into the interior of the box.

We had to design our servo mount to attach to the top of the box to securely hold the servo in place while the box locked. We screwed this mount to the top portion of the box and made sure it was screwed on both sides.

We then screwed the wooden L to a servo horn on the micro servo and then mounted the servo inside the installed mount. The smooth fit of the mount held the servo in place. We used M2 wood screws to screw the mount onto the box.

Similarly, we secured the Pi to the underside of the lid so that it could permanently be housed in our box and we would have a portable project. We placed the Pi in a Pi case and screwed the case to the box.

Step 7: Flashing OS

For this project, we flashed the SD card with an older Pi OS version, as the OpenCV libraries we used are only compatible with this older version. We downloaded the most recent version of Buster OS, a slightly outdated operating system. Download the lowest ZIP file on the page for the best results.

Next, we installed the Raspberry Pi Imager application on our personal computer. We opened the Imager app, and in the "Choose OS" section we selected "Use Custom." We uploaded our zip file here, and let the OS write.

Step 8: OpenCV Configuration

For our face detection to work, we needed to run a series of commands that installed libraries that made face detection possible. This step mainly consisted of lines of code we typed in the Terminal application on the Pi.

This process took 2-3 hours. When commands prompted us, "Do you want to continue?" we typed out yes and the commands continued.

Setup Code (# means added Note):

pip install picamera[array]

Sudo apt-get update

Sudo apt-get upgrade

sudo apt install cmake build-essential pkg-config git

sudo apt install libjpeg-dev libtiff-dev libjasper-dev libpng-dev libwebp-dev libopenexr-dev

sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libdc1394-22- dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev

sudo apt install libgtk-3-dev libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5

sudo apt install libatlas-base-dev liblapacke-dev gfortran

sudo apt install libhdf5-dev libhdf5-103

sudo apt install python3-dev python3-pip python3-numpy

sudo nano /etc/dphys-swapfile

# After this command, a popup will open. We changed the number in the text "CONF_SWAPSIZE = 100" to "CONF_SWAPSIZE = 2048." Then, we pressed control x, y, and then enter to save and close the popup.

sudo systemctl restart dphys-swapfile

git clone https://github.com/opencv/opencv.git

git clone https://github.com/opencv/opencv_contrib.git

mkdir ~/opencv/build

cd ~/opencv/build

cmake -D CMAKE_BUILD_TYPE=RELEASE \

-D CMAKE_INSTALL_PREFIX=/usr/local \

-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \

-D ENABLE_NEON=ON \

-D ENABLE_VFPV3=ON \

-D BUILD_TESTS=OFF \

-D INSTALL_PYTHON_EXAMPLES=OFF \

-D OPENCV_ENABLE_NONFREE=ON \

-D CMAKE_SHARED_LINKER_FLAGS=-latomic \

-D BUILD_EXAMPLES=OFF ..

make -j$(nproc)

#The display will freeze for a while now, and can take up to 40 minutes to resume.

sudo make install

sudo ldconfig

pip install face-recognition --no-cache-dir

# This command will take 40+ minutes again.

pip install imutils

sudo nano /etc/dphys-swapfile

# Change the SWAPSIZE number back from 2048 to 100.

sudo systemctl restart dphys-swapfile

git clone https://github.com/carolinedunn/facial_recognition

Step 9: Facial Detection Code

After issuing these commands, many folders had been created on our Pi. We had a new folder called facial_recognition, and inside that many subfolders and Python files.

Headshots_picam.py

We first opened the file "headshots_picam.py." Here, the name in the quotation marks is Aarnav, one of our team members' names. We had to add Aarnav's name there, so that we could let the code know where to store the images of Aarnav's face that we would soon be taking.

Next, we went into the dataset subfolder inside the facial_recognition folder, and created a subfolder called "Aarnav."

Now, we ran the headshots_picam.py Python file. A small pop-up window appeared with the Pi cam camera feed. This is where we took photos of our team members.

We took each picture by pressing the spacebar key. We made sure to move our faces, change our expressions slightly, and move to different parts of the camera's recording area.

Train_model.py

We ran this Python file next, which can also be found in the facial_recognition folder. This code processed Aarnav's face so that it would be ready for face detection.

Step 10: Detecting Your Face

Before running facial detection, we added the servo code to lock the box, and the unlock it if the accurate face was detected.

To do this, we opened the facial_req.py python file in the dataset subfolder. After line 12, we added servo code:

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup (11, GPIO.OUT)
p = GPIO.PWM (11,50)
p. start (5)
print ("locking door")
GPIO.output(11,True)
p.ChangeDutyCycle(6)
sleep(2)
GPIO.output (11, False)
p.ChangeDutyCycle(0)
# This code is for locking the box at the start of the code, so that the box is now locked, until the correct face is recognized by the Pi. After line 88 (name = max(counts, key=counts.get)), we will add servo code in the form of a loop that moves the servo and unlocks the box if the correct face is detected. We will also be printing text if this condition is met.
if currentname ! =name
currentname = name
print("unlocking door," currentname)
GPIO.output(11, True)
p.ChangeDutyCycle(11)
sleep (2)
GPIO.output (11, False)p.ChangeDutyCycle(0)

When we ran this Python code, we initially heard the servo moving and the door locking. We made sure the box was in the closed position when we ran the code. After 10 seconds, a camera stream window opened. When Aarnav's face was visible, the box unlocked.

Note: The more pictures you have, the more accurately it will detect only your face. When we had just 50 pictures loaded, the program would recognize any Indian person with no facial hair and no glasses such as Aarnav.

Step 11: Done!

Congratulations! You are officially done with this project!

Have fun playing around with the box, and detecting your own face and those of friends too! Here is a demonstration of how your box should work.