Core Electronics Forum

Object and Animal Recognition With Raspberry Pi and OpenCV

Hey all just finished polishing the newest Raspberry Pi tutorial ‘Object and Animal Recognition With Raspberry Pi and OpenCV

If you have ever wanted to actively search and classify all kinds of household objects and common animals with a palm-sized computer then this guide is for you. I will show you exactly how to do this so you can set up a similar system in your own Maker-verse. Furthermore, I will demonstrate how you can refine the identification so it searches only for particular desired targets. Then we’ll take this to the next level and demonstrate how you can alter the code to make the Raspberry Pi control physical hardware (a servo :slight_smile:) whenever it identifies that particular target.

The Coco library used here will enable our Raspberry Pi will be able to identify 91 unique objects/animals and provide a constantly updating confidence rating. If the objects and animals don’t suit your particular application there are many many many other pre-trained libraries for you to use in your next project. Machine learning has never been more accessible.

Read more

6 Likes

Hi Tim, firstly I know nothing about electronics but the Raspberry Pi looks interesting, but firstly the Object Detection looks like something I could use, I presume that once it has identified an object it can then be programmed to do other commands.



We brought an ex show dog a couple of months ago, she had a fright in the ring and wouldn’t go back, when we got her home she wouldn’t use the dog door being frightened of the Perspex flap on her back, could there be a system setup where it recognises the dog then lifts the flap up either by an linear activator, or a pulley setup?



Thanks

Gareth

5 Likes

Hi Gareth,

While you could absolutely use a Pi in this capacity, it might be overkill, as 99% of the time it’d be sitting there processing images without a dog. You might consider using a PIR sensor with a cone or tube around it to limit its field of view to where the dog would be walking, however I haven’t used one like that, so perhaps a distance sensor pointed in the right direction would work well instead.

As for raising the door, if you wanted to use a pulley system, a small DC motor would likely do the trick, but you’ll have to tell your microcontroller where the door is somehow, perhaps using a hall effect sensor might be the go.

Anyway, this is quite the rabbit-hole, with a few more things you’ll need to get it up and running. so if you chime in with the sort of stuff you’ve done before, or what you need explained more, we’ll try and get you on track :slight_smile:

-James

4 Likes

Thanks James, I’ll be in touch.

2 Likes

@James I’m very new to all this and following your process I get the error ‘‘Make Error: The source directory “/home/pi/build/BUILD_EXAMPLES=ON…”’’ does not exist when I enter after this point. Any idea what I might be doing wrong?

3 Likes

Hey Russell, is this when you are going through the ‘Setting Up Open-CV on Raspberry Pi OS for Object Detection’ section or when your trying to run Python Scripts like | object-indent.py |?

1 Like

Hello @Tim , correct its when I’m going through the ‘Setting Up Open-CV on Raspberry Pi OS for Object Detection’ steps. Thanks for your assistance

2 Likes

Interesting. So the steps here are creating those directories. Compare what is in your terminal with what I had when I installed it, perhaps the issue lies in some way the syntax is typed.

Then as soon as I press enter on that final line it does this (and continues for a little bit).

The lines starting from | cmake -D CMAKE_BUILD_TYPE= RELEASE \ | are slightly different then previous as they will all stack up untill it gets to the | -D BUILD_EXAMPLES=ON … | (note there is only two dots at the end of this line the forum system makes there be three).

Then once you press enter on that line they will all run together. I think perhaps all the lines on your side are being done independantly somehow. Hope this helps and send through a screenshot of your terminal if this doesn’t solve your problem.

5 Likes

Thanks very much for those screenshots @Tim - it seems like the omission of a space before the backslash was the issue…or at least I noticed this and followed the syntax carefully this time around and it’s commenced loading and is 50% of the way through.

4 Likes

Awesome! I’m super excited for you, gimmie a pop if you run into any other issues or once you nail it a screencap of a successful object detection would make my day :slight_smile:

4 Likes

I left this running overnight and it appears to have run into some kind of error at 77% - any thoughts?

2 Likes

Totally ran into the exact same thing. I fixed it by running | make -j $(nproc) | again.

Don’t worry it will remember all of the work that it has already done. I had hoped it was just me who ran into this issue but I’ll go back to the article and update it. Below is my problem exactly the same as you and I have just entered the same command which fixed it for me.

3 Likes

@Tim thanks for that - we able to get it complete with this. When I run the code I get an module not found error - should I start from scratch again?

1 Like

Hi Gareth,

My inlaws have an automatic RFID cat flap that only lets their cats in - so something like this exists as an off the shelf product. Might be worth a look in to.

3 Likes

Hey mate, I feel like it should be working at this point. Did you do the last two terminal commands that I wrote below? This will give the system its much-needed restart.

sudo make install && sudo ldconfig

sudo reboot

Once that is done run the following two lines of code to check that cv2 has been installed like the image below so we can check to see if it is installed on your Raspberry Pi.

4 Likes

2021-09-10-171358_640x480_scrot

6 Likes

It seems very weird that it was working and then suddenly stopped. It looks like the error has to do with opening the camera. Run a quick check to see that the ribbon cable of the camera is plugged in correctly on both sides. Then for me make sure that you can get a live feed out of the camera by typing and entering the below directly into your terminal.

         raspistill -t 0

Double-check that Camera is enabled in the Interfaces Tab of the Raspberry Pi Configuration if that is not working. If you can get a live feed then the next step is to check in the | object-ident.py | code what index number it is using. The line you are looking for is below, using a value of 0 should focus it to the Raspberry Pi Camera.

cap = cv2.VideoCapture(0)

The servo movement you saw may have been the servo going to the initial zero location.

– Solved –

2 Likes

Hi, I am using your source for my object detection project! Thank you so much!
But, I have a question about delay. So, there seems like about 10 seconds delay in real-time. Is there any way to reduce that delay, such as “decreasing frame rate”, or “reducing the number of images read every second”? I am trying to find where I can adjust those in the code, but it seems hard to do so. Thank you so much!

-Kyung

3 Likes

Hey mate saw your Youtube Comment as well - Awesome your up and running with the software, though 10 seconds is a very long time, (At most the set up I use here takes half a second to identify an object). What hardware are you using?

Software-wise by limiting the objects it searches for increases speed, also deep in the code you can find locations to lower camera resolution for sure which will definitely help too. You can also overclock your Raspberry Pi in the configuration settings which will allow it to run faster.

2 Likes

Hi Kyung,

It’s also important to check what Pi you’re running on (let us know when you can), they’ve gotten progressively faster over the years, and this will help your model performance.

Keen to see how you go!
-James

1 Like