Raspberry Pi LESSON 63: Object Detection on Raspberry Pi Using Tensorflow Lite
Object detection has become an essential part of computer vision, and with the rising popularity of Raspberry Pi, it is now possible to perform object detection tasks on this small, inexpensive computer.
In this lesson, we will learn how to use Tensorflow Lite, a lightweight version of the popular machine learning framework Tensorflow, to perform object detection on a Raspberry Pi.
Prerequisites
Before getting started with this lesson, you will need the following:
- A Raspberry Pi board
- An SD card with Raspbian installed
- A compatible USB camera
- An internet connection
Setting Up Tensorflow Lite
First, you will need to install Tensorflow Lite on your Raspberry Pi. You can do this by following the official installation instructions provided by the Tensorflow team.
Once Tensorflow Lite is installed, you can proceed to the next step, which is downloading a pre-trained model for object detection. There are several pre-trained models available for Tensorflow Lite, and you can choose the one that best fits your needs.
Writing the Object Detection Code
After setting up Tensorflow Lite and downloading a pre-trained model, you can start writing the code for object detection. This will involve capturing video input from the USB camera, processing the frames, and running the object detection model on each frame.
You can use Python and the OpenCV library to capture video input and process frames, and then use Tensorflow Lite to perform object detection on the processed frames.
Running the Object Detection
Once the code is written, you can run the object detection program on your Raspberry Pi. You should be able to see the live video feed from the USB camera, with bounding boxes drawn around the detected objects in the frame.
Conclusion
Object detection on a Raspberry Pi using Tensorflow Lite opens up a world of possibilities for computer vision applications on this small, affordable device. With the right tools and knowledge, you can build your own object detection projects and explore the potential of machine learning on the Raspberry Pi.
UPDATE:
If you are getting an error like:
ImportError: /lib/aarch64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by ~/.local/lib/python3.9/site-packages/tensorflow_lite_support/metadata/cc/python/_pywrap_metadata_version.so)
you need to be on the same version of software that I am of tflite_support. Downgrade yours from version 0.4.4 (current) to 0.4.3 (used in this tutorial)using this command:
python -m pip install –upgrade tflite-support==0.4.3
This should fix things.
Hi, thx for this very good tutorial and thx for your perfect english, making it easy for understanding your explanations for non native speakers. All works fine. However has anyone already used the example with the USB Coral Edge TPU to run? Only the option set to TRUE does not work. I couldn't find anything on the web about the error message. I have already replaced the model, but I fear you have to install the Edge TPU runtime according to the Coral website, which I'm afraid will destroy the current installation, so it would be nice to get some information about the pitfalls beforehand. Thanks in advance!
WoW. I was frustrated just to setup opencv couple of weeks back before stumbling upon your videos. And now I have a working tensor flow setup detecting multiple objects. All thanks to your masterclasses. Thanks Paul.
Is there anyway to put the x,y coordinates on the detected objects?
Thanks Paul. I had a rough go from the beginning with all my mistakes, But every mistake I learned something from my past problems. I love this stuff.
This dude can detect kids on his lawn from over 20 feet away!
October 2023
I started this project using a new MicroSD card. When I attempted to run the following Code:
“`
import cv2
import time
from picamera2 import Picamera2
from tflite_support.task import core
from tflite_support.task import processor
from tflite_support.task import vision
import utils
“`
I got an error:
“`
>>> %Run detect.py
Traceback (most recent call last):
File "/home/tji379/Python/detect.py", line 1, in <module>
import cv2
File "/home/tji379/.local/lib/python3.9/site-packages/cv2/__init__.py", line 5, in <module>
from .cv2 import *
ImportError: libcblas.so.3: cannot open shared object file: No such file or directory
“`
I found a solution as follows:
“`
sudo apt-get install libatlas3-base
“`
However, immediately after resolving the previous error, I got a new error:
“`
ImportError: /lib/aarch64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by ~/.local/lib/python3.9/site-packages/tensorflow_lite_support/metadata/cc/python/_pywrap_metadata_version.so)
“`
I resolved this issue by following the author's suggestions below:
“`
python -m pip install –upgrade tflite-support==0.4.3
“`
I hope my response can be of help to those in need.😊
I'm beginning to get a hunch that he likes iced coffee …
brilliant, thank-you.
My Webcam does not show a picture, I get the window that pops up. How do I fix error message, cv2.error opencv(4.5.3) – 206 bad flag?
Is this model detect banana, orange, onion, potato, garlic?
How cai i do with my custom data
how can I fix attributeError module cv2.cv2 has no attribute CAP_PROP_FRAME_FPS?
My master ❤🌹🌺
Hey Paul,
i am facing problems as soon as i connect the USB webcam. If i run the script without connecting the USB webcam it runs normaly and i can interrupt the program with the key 'q'. Running the script with the USB webcam connected shows the frames but it freezes the mouse and keyboard. im not able to quit the script as long as the USB webcam is connected.
I did a little search and found it "sudo setpci -s 01:00.0 0xD4.B=0x41" as Bugfix. But it didn't worked for me. The same occurs if i try to run the script with the USB webcam. Frames are shown but keyboard and mouse freeze.
Could you please give a hint? Thx.
Great video. Did you figure out why the picamera had such a low FPS?
I love your lessons, but you missed the fact that your apt-get update failed because your pi doesn't appear to be on the network looking at the top right of the screen. So the subsequent apt-get upgrade was of course extremely fast 🙂
Hi, I have been following your videos in the past months or so, first thank you for the great tutorials, it has been an amazing ride up until here. in this lesson, when I try to do what you did in 41:00 I get an error saying this "ValueError: The model is not a valid flatbuffer buffer"
I have gone through everything I could find online but nothing worked, I have check what was installing on your Pi and checked the versions to my own, everything is the same as yours, but I get this error, I am using this for a university project and the deadline is closing in on me, any help would be great.
thank you in advance (sorry if my English seems a little off, it's not my native language)
Спасибо, я раньше работал с нейросетями, и сначала мне было очень сложно, потому что сложно было найти хорошее объяснение, теперь же один знакомый рассказал мне про твой канал, и честно сказать я восхищён тем как ты объясняешь, все становится понятным и простым, спасибо, это очень круто!
July 2023 and running into the same problem with this as every other time I've been trying to do tflite stuff…
ImportError: /lib/aarch64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by ~/.local/lib/python3.9/site-packages/tensorflow_lite_support/metadata/cc/python/_pywrap_metadata_version.so)
tflite looks to be using a glibc version not currently available on the standard pi builds.