How to Install TensorFlow 2 and OpenCV on a Raspberry Pi

preview_player
Показать описание
Here's how you can install TensorFlow 2 and OpenCV on your Raspberry Pi all in one video. There are some tricky steps so I try to walk through the whole process slowly and thoroughly. Leave a comment if you have any questions or video requests.

I may earn commission if you purchase from the links below:

INSTRUCTIONS:
0:00 Intro

0:25 Setup your Raspberry Pi.
I recommend using 64-bit Raspberry Pi OS

2:35 Getting started

Access the terminal

cat /etc/os-release
sudo apt update
sudo apt upgrade

3:34 Find your .sh script
python3 -V (take a note of this)
uname -m (take a note of this)

NOTE: If you have armv7 and Python 3.8 or higher, you will need to either downgrade your Python (which I show) or update to the 64-bit aarch64.

Some example combinations:
If python3 -V = "3.9.*" (* means any number) AND uname -m = "aarch64"

If python3 -V = "3.7.*" AND uname -m = "armv7l"

If python3 -V = "3.9.*" AND uname -m = "armv7l"
Either install 64-bit Raspberry Pi OS (see beginning of this video)
or
Change Python to 3.7.* (see the next section of this video)

-----------------------------------------------------------------------

5:55 Downgrading Python (OPTIONAL)
(1) Run the easy installer

(2) Add pyenv to .bashrc:
Edit the .bashrc with the command
sudo nano ~/.bashrc

(3) Add the following three lines to the botton of the .bashrc file:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"

(4) Restart the terminal
exec $SHELL

(4) Install system packages
sudo apt-get install --yes libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libgdbm-dev lzma lzma-dev tcl-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev wget make openssl

(5) Update pyenv
pyenv update

(6) Install python versions
pyenv install --list
pyenv install ~~Your python version~~

(7) Set python verion
mkdir project
cd project
pyenv local ~~Your python version~~

------------------------------------------------------------------------------

9:30 Install TensorFlow

mkdir project
cd project

python3 -m pip install virtualenv
python3 -m virtualenv env
source env/bin/activate

sudo apt-get install -y libhdf5-dev libc-ares-dev libeigen3-dev gcc gfortran libgfortran5 libatlas3-base libatlas-base-dev libopenblas-dev libopenblas-base libblas-dev liblapack-dev cython3 libatlas-base-dev openmpi-bin libopenmpi-dev python3-dev 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 libhdf5-serial-dev libhdf5-103 libqt5gui5 libqt5webkit5 libqt5test5 python3-pyqt5

python3 -m pip install -U wheel mock six

Select "view raw" then copy the URL
Run:
wget [Raw file URL]
sudo chmod +x [Raw file URL]
./[Tensorflow file]
sudo pip uninstall tensorflow
python3 -m pip uninstall tensorflow
python3 -m pip install tensorflow-[Your version here].whl

exec $SHELL

source env/bin/activate

python
import tensorflow as tf
tf.__version__
quit()

NOTE: If there's an hdf5 warning run this command:
pip uninstall h5py
HDF5_VERSION=[Desired version] pip install --no-binary=h5py h5py==3.1.0

--------------------------------------------------------------------------

python3 -m pip install opencv-python
or
pip install opencv-contrib-python (more libraries and customization)

python3 -m pip install "picamera[array]"

----------------------------------------------------------------------

python
import cv2
import tensorflow as tf
cv2.__version__
tf.__version__

Рекомендации по теме
Комментарии
Автор

I tried for 3 days straight - 12 hours a day - then found your videos and got it working in the time of the video - approx 20 mins. Thank you so much.


Also your hell handsome

timothyhassett
Автор

Excellent video I have looked at other videos and they always say, "go do something" and presume you know how to. This video has every single step explained in detail and is really easy to follow.
Thank You Sam for taking the time to make this video - much appreciated!

steveconway
Автор

Great video. FYI - ensure you (the audience) ``` pip install protobuf==3.20.* ``` before installing the tensorflow wheel if using tf=2.8+py=3.9 on 64bit Bullseye (aarch64). This may be true for other combinations as well (untested). This wasn't needed in the original video but due to updates over the last 6 months is now necessary.

timdarrah
Автор

Thank you very much!! I couldn't install tensorflow on my Pi 4, I needed to do this pip3 install --upgrade protobuf==3.20.0, since he asked me for these 2 things:
1-Downgrade the protobuf package to 3.20.x or lower.
2. Set (but this will use pure-Python parsing and will be much slower).

ufombrille
Автор

Hi, I followed the tutorial and made sure the version are identical, however I recieved an tensorflow.whl is not a supported wheel on this platform.

RogerLin-rx
Автор

Why there is an error "Note selecting 'libhdf5-dev' instead of 'libhdf5-serial-dev'
E: Unable to locate package libopenblas-base

is there a way to solve this I can't input the additional libraries

francisluna
Автор

Hi
Excellent job
But I have a problem
Error: is not supported wheel on this platform
If you can help me
I have raspberry pi 4 (2ram)

alouanibakir
Автор

I learned a lot, thanks for the great tutorial, I wish everyone explains in such beautiful way

mahmoudaldidamoni
Автор

You are great. You taught good. YOU DESERVE MORE.
Thank You.

DineshOsti-mulp
Автор

Hi sadly i still get the not a supported wheel for this platform I have a 64bit system and I tried to install it for 3.8

sebastiaandenhertog
Автор

Great video! But i got an error when i install the whl file. It says wheel is invalid. Do you have any idea?

furkanbaydar
Автор

Error: .whl is not a supported wheel on this platform pls help

jmvaleriano
Автор

I'm working on tensorflow this really helpful keep doing your work on raspi and make videos related to errors in raspi

harikrishnasamy
Автор

I was wondering if you could do a video on object detection using openCV that runs smoothly at 20 frames or more

nathan-plyd
Автор

Following your tutorial I have been very successful getting all of this to work. And have even trained a YOLOv5 model for image detection. I am having trouble with converting that best.pt file to a keras model because I cannot find a tensorflow-addons that works with this. Can you recommend a workaround.

amandak
Автор

This is a good tutorial, it helped me very much. I fixed the error about can't install Tensorflow on Python 3.7. Thank you very much!

minhchaunguyen
Автор

Jazak Allah kher, may God bless you insha Allah!!

dawood
Автор

when installing tensorflow it says BadZipFile: file is not a zip file

hasrat
Автор

Excellent explanation, clear and to the point.

issamelimrany
Автор

This method worked for me. Thanks a ton :-)

Giggle_Wiggle_