It’s unlikely my new shiny Raspberry Pi 5 will run ESXi for ARM in the short term, so I decided to use it for other projects. One of these projects is TensorFlow. Artificial intelligence and machine learning are hot topics these days, and TensorFlow Lite is the ideal way to familiarize yourself with these groundbreaking techniques.
Setting up TensorFlow Lite on a Raspberry Pi 5 isn’t that easy if you start from scratch. Many online examples are based on older models, which don’t use the Raspberry Pi 5 Compatible OS 'Bookworm'. During my search on Google, I stumbled upon a video from FREEDOM TECH. This video is based on the Camera Module 2, but it’s one of the rare examples using Bookworm.
In the first part of this video, you will get an explanation of how to install all the opencv-python. OpenCV is a Python library that allows you to perform image processing and computer vision tasks. The installation of opencv results in an error. Before installing opencv-python, you should first remove the externally managed file to step away from the use of environments.
sudo rm /usr/lib/python3.11/EXTERNALLY-MANAGED
sudo pip3 install opencv-python
Now, we're ready to install TensorFlow by using the following command:
sudo pip3 install tensorflow
With the next command, we’re installing VCZone. This is a computer vision package that makes it easy to run image processing and AI functions.
sudo pip3 install cvzone
The installation is complete now. There’s one additional step you need to perform. When you copy the rpi5-bookworm-tflite git hub repository to your Raspberry Pi 5, you have to change the path in the tflitetestrpicamera.py file and point it to the correct location of the labels and TensorFlow file.
model_path='/home/esloof/efficientdet_lite0.tflite'
label_path='/home/esloof/labels.txt'
The initial version of the tflitetestrpicamera.py didn’t have any support autofocus for the Camera Module 3. After reading a great article about “How To Use Raspberry Pi Camera Module 3 with Python Code” by Les Pounder I’ve added a few extra lines of code that import the libcamere for controlling the camera, and I’ve also enabled autofocus.