Teach your Pi to spot human faces, train it to know your face and run code so that it will successfully identify you when it sees you. Then use your face to control the GPIO!

Transcript

Hey gang, Tim here at Core Electronics and today we're making our Raspberry Pi recognize who we are by our face.

Open source software and Raspberry Pis go together hand in hand. The two excellent examples of this are OpenCV, which provides a huge free resource to solve real-time computer vision problems, and the Python Face Recognition Package, which computes bounding boxes around a face in real time. These are the two systems that we will use to make this all come together.

Machine learning has never been more accessible. I will show you exactly how to have your Raspberry Pi microprocessor be able to spot human faces, how to train it to know your face and to run code so that it will successfully identify you when it sees you. Then I'll take it another step and show you how you can use your face to control a servo which is attached to the Raspberry Pi.

Before me on the table is everything you need to get this system up and running really fast. Starting off you're going to want a Raspberry Pi official camera module version 2. You can also use the Raspberry Pi high quality camera, a microSD card, a official Raspberry Pi power supply, a HDMI cord and monitor to connect to, a mouse and keyboard, and for this I'm using a Raspberry Pi 4 and for this I'm using a Raspberry Pi 4 model B as the extra computing power oomph that the Raspberry Pi provides is invaluable. You're also going to want some way to connect the microSD card to your computer so you can flash it.

So we're going to get started with all the packages installed already. To follow along at this point go to the article to get the steps to install the packages on your setup.

Once you power up the Raspberry Pi you're going to see this familiar background booted.Up. Now first, let's open up the Raspberry Pi configuration menu found by using the top left menu and scrolling over preferences and make sure that the camera found under the interfaces tab is enabled. If you had to enable this setting, go ahead and reset the Raspberry Pi so that the changes can take effect.

Now open up the file explorer, which is the folder button on the top left of the screen. Jump into the folder located in the home Pi directory facial recognition and then look for the Python code called headshots underscore pycam.py. This Python code will let us take some photos of our faces using the official Raspberry Pi camera.

Right click and open up that Python script with either Thonny or Genie, both are just Python language interpreter softwares, and alter the line of code here with your name, in my case Kimmy. Then save this script.

Next, go back into the folder structure and open up the photos folder. Here you're going to add another folder with your name. This folder is then going to be the location where the photo files will end up.

Then jump back into the Python editor where we had saved that Python code and run it. This will open up a little window and a terminal window which you can use to save images of your face. Press the spacebar key to take an image, take around 10, and then the Q key to close the window once you've done so. Provide a couple of different angles of your face so it can determine your dimensions better.

Once you close the software, you're going to be able to see the images of your face stored in the folder you created for your name. You can add other faces using this same method too.

With all that sorted, we can get into the machine learning step. The pictures we took will now be used.By the Python code train_model.py. Any pictures in the dataset folder location will be analyzed by this code when we run this program.

So open up a new terminal using the black console button on the top left and type the following, pressing enter after each line:
cd facial_recognition
This will get us into the right folder.

Then type:
python train_model.py
This is going to run our desired code. This will start the training process, which you can see occurring for each image that I took of my face.

Then, with that completed, the Raspberry Pi 4 model B will have learned what your face looks like.

So let's give it a go. To run the identification code that will identify faces and when it finds a trained face, it will write their name next to it.

Start by opening up a new terminal, just the same as before.

Then type the following and press enter after each step:
cd facial_recognition
Then:
python facial_req.py

Once you press enter, it's going to take around five seconds to boot up and run.

Then you're going to see a small window pop up with a live stream of the Raspberry Pi camera. Aim the camera at your face, and if it puts a yellow box around your head and names you correctly, you have done it.

The Raspberry Pi camera is now searching live for faces. It will also determine if it's a known or an unknown face. If it's unknown, it's going to write "unknown" next to it, and if it's a face of a person that it knows, it's going to write that person's name next to it.

This example code is awesome and lets you experiment to see when the software can or cannot track your face. I find if you tip your head to the side a couple of degrees, it's going to completelyDisable the facial recognition and if you cover your nose as well it struggles. Close the terminal window or press ctrl c on the keyboard to stop it running.

So we can do many things with this now. Simply to start we can now jump into the folders with the Python code and alter just a couple of lines in that code in the facial_rec.py so that every time a known face is seen it's going to send out signals via the GPIO pins of the Raspberry Pi. These general purpose input and output pins can be used to control an almost endless amount of sensors and mechanisms.

So for this I'm going to get a servo and I'm going to for this I'm going to get a servo to rotate when the Raspberry Pi system sees my face. If it sees an unknown face or no face at all it's not going to activate this servo. All this by adding just six lines of code to the script. All the code I'll be adding here is completely explained in the guide controlling standard servos with Raspberry Pi link down below.

So hopefully you can see everything. What I'm going to do is hide my face from the camera by putting my thumb just in front of it. The servo you're going to be able to see from the top and when I show up my face you're going to see the servo move. You can see it's active. You can see down here it's red because my skin is pink and now I'm going to show up my face. Boom! Straight away as soon as it saw my face. Nice!

Huge thanks go to the OpenCV and facial recognition package teams that work on the amazing machine learning software that we have running on this Raspberry Pi. Both are really good open source software. Also a huge thank you goes to Carolyn Dunn who created the majority of the amazing software which makes these two systems work.Together, so well. There's just so much potential with this software to take projects to amazing places. So that's it for today. Until next time, stay cozy.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.