Hey gang, Tim here at Core Electronics and today we're using a Raspberry Pi 4 to search and identify common household objects and commonplace animals all in real time and I'll show you how to build one in your own makeovers.
We're going to be using a trained library which is going to enable our Raspberry Pi in combination with a Pi camera to identify 91 unique objects and animals in real time whilst providing an updating confidence rating. Later on in the video I'm also going to refine the identification so it searches only for particular desired targets.
With that in the bag we're going to take this to the next step and demonstrate how you can make the Raspberry Pi control physical hardware through the GPIO whenever it identifies that particular target. This will be the second foray into the OpenCV landscape with Raspberry Pi and facial recognition being the first, link down below to that.
Having the tools to solve real-time computer vision problems has never been more accessible so let's jump in.
On the table is all the components you'll need to get this system up and running real fast. You're going to need a Raspberry Pi official camera module V2 or you could optionally use the Raspberry Pi high quality camera, a microSD card flashed with Raspberry Pi OS, a power supply, naturally you're also going to want a HDMI cord, a mouse, a keyboard and a monitor to connect it all to. I'm also using a Raspberry Pi 4 model B 4 gigabytes for this setup as having the extra computing power that comes with a Raspberry Pi 4 is a must for this kind of application.
Once you power up the Raspberry Pi you're going to see the desktop booted up and from here you can connect it to the internet. Next open the Raspberry Pi configuration.Menu can be found by using the top left menu and scrolling over preferences. Make sure the camera is enabled under the interfaces tab. If you had to enable this setting, go ahead and reset the Raspberry Pi so the changes can take effect.
Now it's time to install OpenCV to Raspberry Pi. This software provides a huge resource to help us solve real-time computer vision and imaging processing problems that we'll encounter. The installation process involves a series of terminal commands that will take some time to complete. It's a good opportunity to have some cups of tea as you work through the process. Every terminal command needed for this can be found in the written article, so all you need to do is copy and paste each line.
Once the installation is complete, you will have OpenCV installed onto a fresh version of Raspberry Pi OS. The next step is to download the zip file found at the bottom of my article page and unzip the contents into the desktop. It's important that it goes in this directory, as this is where the Python code will be searching for to get data about the object's name and also to get data from the trained library.
The Cocoa common object in context library is a large-scale object detection segmentation and captioning dataset. This trained Cocoa library is how the Raspberry Pi will know what certain animals and certain objects generally look like. You can find pre-trained libraries for all manner of objects and animals, and even sounds. So if this particular library doesn't suit your needs, you can find many others freely accessible online.
With all the setting up done, grab yourself some props like I have here: a mouse, a cup, a keyboard, and let's jump right in.And run that code.
The first code you're going to start with is objectident.py and you're going to open it up by right clicking it and opening it with either Thonny or Jenny.
Both are just Python language interpreter softwares.
As soon as you press play you're going to see a window open up showing a live feed of exactly what the Raspberry Pi 4 model B is seeing through the official high quality Raspberry Pi camera.
Whenever it sees an object it knows it's going to draw a green box around it as well as give it a label and a confidence rating.
If it sees multiple objects that it knows it's going to be creating multiple boxes and labels.
It's awesome hey it's honestly incredible how clever this little tiny Raspberry Pi can be.
Running this code continuously gets the Raspberry Pi really hot so definitely a good idea to provide it with some kind of cooling.
Jumping back into the code a value worth tinkering with is the threshold percentage value which you can find right here.
Increasing this means the software will only draw a box around an object when it has a high confidence factor and it's absolutely sure that it has identified the object correctly.
Another option worth tinkering with is the NMS percentage value which you can find here.
Increasing this will limit the amount of simultaneously identified objects.
It is immensely valuable to be able to look for just one object and ignore all the other objects.
That way for example you can count how many cups seen during a day but not how many humans are holding those cups.
This is also a very useful way to make the Raspberry Pi run the live preview window with a higher frame rate.
But hey what if you wanted to identify a cup and a horse and only those two things? WellYou're going to edit that line in this manner. Then once you save the code and press play it's going to do just as you desired.
We can do so many things with this code now. Simply let's start by jumping into the folders and opening up object identify3.py with a Python editor. In this file I've added just a few lines of code to the original code so that every time a particular target is seen it's going to send out signals via the GPIO pins of the Raspberry Pi. It's going to be commanded to rotate whenever the Raspberry Pi system sees a cup. If anything else is seen it's not going to do anything.
All the code I'm going to be adding here is completely explained in the guide controlling standard servos with the Raspberry Pi. Link down below in the description. And so with my cup and servo in hand let's run this system. And as soon as I show the camera the cup boom we have rotation.
There is just so much potential with this software to take projects to amazing places. Big thanks goes to the OpenCV and Cocoa teams that work on this amazing software which lets things like this happen.
So that's it for today. Until next time, stay cozy.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.