Setting up the Raspberry Pi

  1. Flash the 64-bit Raspberry Pi OS (version Buster) to an SD card or USB drive and boot up the Pi.

  2. Either through SSH, VNC, or direct using a display, update the Raspberry Pi software

    → sudo apt-get update & sudo apt-get upgrade

Create and Activate a Virtual Environment

  1. Run the following commands:

    → sudo apt-get install python3-pip python3-virtualenv

    → mkdir project

    → cd project

    → python3 -m pip install virtualenv

    → python3 -m virtualenv env

    → source env/bin/activate

Install Required System Packages

  1. Run the following commands. These will install and/or update numerous libraries & packages required both for Tensorflow and OpenCV (needed for Tensorflow)

    → sudo apt install -y build-essential cmake pkg-config libjpeg-dev libtiff5-dev libpng-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libfontconfig1-dev libcairo2-dev libgdk-pixbuf2.0-dev libpango1.0-dev libgtk2.0-dev libgtk-3-dev libatlas-base-dev gfortran libhdf5-dev libhdf5-serial-dev libhdf5-103 libqt5gui5 libqt5webkit5 libqt5test5 python3-pyqt5 python3-dev

    → sudo apt install libpgm-5.3-0 libhdf5-103-1 libxrandr2 libdatrie1 libtheora0 libxcb-shm0 libavutil56 libopus0 libxfixes3 libsodium23 libaec0 libcairo2 libswscale5 libspeex1 libaom0 libzmq5 libvorbisenc2 libwayland-egl1 libva2 libbluray2 ocl-icd-libopencl1 libxvidcore4 libzvbi0 libsoxr0 libvpx6 libopenexr25 libvorbis0a libavformat58 libcairo-gobject2 libsz2 libx265-192 libmpg123-0 libogg0 libnorm1 libxrender1 libwayland-cursor0 libswresample3 libdrm2 libilmbase25 libopenmpt0 libgdk-pixbuf-2.0-0 libgme0 libvorbisfile3 libsnappy1v5 libcodec2-0.9 libtwolame0 libchromaprint1 libpango-1.0-0 libx264-160 libpixman-1-0 libatspi2.0-0 libvdpau1 libssh-gcrypt-4 libva-x11-2 libudfread0 libgsm1 libxinerama1 libwavpack1 libharfbuzz0b libdav1d4 libatlas3-base libepoxy0 libxkbcommon0 libgfortran5 libxcb-render0 libpangoft2-1.0-0 libsrt1.4-gnutls libwayland-client0 libpangocairo-1.0-0 libxcomposite1 libxdamage1 libgtk-3-0 libva-drm2 libgraphite2-3 librabbitmq4 libavcodec58 libatk-bridge2.0-0 libopenjp2-7 librsvg2-2 libshine3 libxi6 libatk1.0-0 libwebpmux3 libthai0 libmp3lame0 libxcursor1

Install OpenCV

  1. Running a simple “pip install” for OpenCV will not work because this will default to the latest versions which are not compatible with the Raspberry Pi. Here we’ll install the most recent version supported by Buster

    → pip3 install opencv-contrib-python==4.5.5.62

Install TensorFlow Lite

  1. Now we finally get to install the tensorflow-lite runtime. We’re almost there!

    → pip3 install --extra-index-url https://google-coral.github.io/py-repo/ tflite_runtime

  2. Unfortunately there are also version issues with the tensorflow lite runtime & support libraries. We will need to downgrade them to the most recent supported versions

    → python -m pip install --upgrade tflite-support==0.4.2

    → python -m pip install --upgrade tflite-runtime==2.11.0

Download & Run an Example

  1. We finally have tensorflow lite ready to go. Let’s download an example.

    → sudo apt install git

    → git clone https://github.com/tensorflow/examples --depth 1

    → cd examples/lite/examples/image_classification/raspberry_pi

    → sudo chmod +x setup.sh

    → ./setup.sh

  2. Now we can finally run our script!

    → python3 classify.py