Facial Recognition TV Time Monitor(also OpenCV Install Guide)

by hponce2 in Circuits > Raspberry Pi

179 Views, 0 Favorites, 0 Comments

Facial Recognition TV Time Monitor(also OpenCV Install Guide)

IMG_3293.jpg

I used a Raspberry Pi for face recognition using Open-CV to monitor the amount of time my younger siblings watch TV and to signal for when their TV time is over. I chose this project because my siblings constantly use the excuse that they forgot to check the time and watch TV for longer than they should.

Supplies

  • Raspberry Pi (Preferably a 3 Model B+ or a 4 Model B but I got it working on a 3 Model B)
  • Micro SD Card (At least 16gb, I tried 8gb and it didn't work)
  • Official Raspberry Pi Camera
  • 3 different color LED’s(one for each younger sibling)
  • 3 resistors
  • 4 female to male jumper cables
  • Breadboard
  • HDMI cable and monitor
  • Mouse and keyboard

Setting Up Raspberry Pi

For this project you have to use Buster OS 32-bit, an older version of Pi OS.

To get Buster OS 32-bit on my SD card I used the following guide:

https://core-electronics.com.au/guides/raspberry-pi/flash-buster-os-pi/

Once you have Buster OS on your SD card you can boot up your Raspberry Pi, connect it to internet, and you don't have to immediately update it but if you don't do it you'll end up doing it in the next step before installing Open-CV.

SSH:

You don't need SSH (SSH allows you to run commands for your raspberry pi from your computer) but it's very time saving when running commands in the Raspberry Pi terminal.

To set up SSH the easiest method is to set it up with the advanced menu in Raspberry Pi imager mentioned in the guide I linked above, to get to the advanced menu you don't have to do the method used in the guide anymore it's now just a button on the bottom right of the Raspberry Pi imager window.

To use SSH you simply open your computer's command prompt and type in the following:

ssh (Your username)@(Your Raspberry Pi's host name).local

It will then give you a message that tells you it's something you haven't connected to before, type yes.

Then just type in whatever password you put for it when you set it up and then any line you type into your command

prompt will run on your raspberry pi.

To exit SSH you simply type:

exit

Open-CV and Facial Recognition Package

This was a very tedious process due to the various install methods, the method I show here is the only way I was able to get it to work for this project. You have to run the following commands in your Raspberry Pi terminal.


First, you need to install the dependencies needed for facial recognition:

pip install picamera[array]

sudo apt update

sudo apt 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

Next, you need to install Open-CV:

sudo apt install python3-opencv

Next you want to switch your default python version to the latest python version you have:

sudo rm /usr/bin/python

sudo ln -s /usr/bin/python3.7 /usr/bin/python

Next you're going to download the face recognition package, run the following commands:

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

pip install imutils

cd facial_recognition

pip install imutils

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


I pieced the guide for this step from the following guides:

Set Up the Facial Recognition and Test It

For this part you can follow the "Training the system" and "Demonstration" sections of the Core Electronics guide:

https://core-electronics.com.au/guides/face-identify-raspberry-pi/#Ack

Something very easy to miss is that at the end of the "Demonstration" section there's a note of an extra step you should take within the facial recognition python code that if you don't do won't allow the program to work with a Raspberry Pi camera.

Also, you could run the facial recognition python codes through either the Raspberry Pi terminal as shown in the guide or just by opening the python code with Thonny and running it through there. I ran them with Thonny to be able to edit the code and run it with the same tab.

3 LED's Circuit

IMG_3292.jpg
GPIO-Pinout-Diagram-2.png

The purple wire is connected to a ground pin from the Raspberry Pi pins and the rest of the wires are connected to GPIO (General Purpose Input and Output) pins from the Raspberry Pi.

Getting Data and Creating an Output From the Facial Recognition

IMG_3297.jpg
IMG_3296.jpg
GPIO-Pinout-Diagram-2.png

In lines 11-20 it starts by importing the library which allows you to control LED's with the GPIO pins, then I made a variable for each LED, connected each to a different GPIO pin, and made sure they were all off, and then I made a time variable for each LED and set it to 0, the data collected will be stored in the time variables and it will output through the LED variables.

Then in lines 87-101 I used if statements to collect data from the facial recognition, in the code that comes in the facial recognition package there's already a variable which stores the face from the database being currently detected so I just reused that variable, I then set it so that every time it recognizes a face it increases its time variable by 2.5 because it takes around 2.5 seconds for the code to go through the if statements again; and once a time variable reaches the set limit which in this case is 3600(1 hour) it turns on the corresponding LED to signal that the time limit has been reached.

Working Run Video

For this test I set the limit to 20 seconds because the regular code is meant to go up to 1 hour.