Skip to main content

Installing Tensorflow and Open-CV on Raspberry Pi



First, you have to install the latest version of Bull-Eye OS from Raspbian's official site. 
(64-bit version)
I'm using Raspberry Pi 4B with 4 Gigs of Ram.
-------------------------------------------
There are a bunch of ways of installing OpenCV and Tensorflow on Raspberry Pi and I've mentioned the most effective ways which worked for me.
-------------------------------------------
On your PC open the following links:
tensor flow link: https://github.com/lhelontra/tensorflow-on-arm/releases

tensor flow: for 3.9.2 :  https://github.com/PINTO0309/Tensorflow-bin/blob/main/previous_versions/download_tensorflow-2.8.0-cp39-none-linux_aarch64_numpy1221.sh
-------------------------------------------
You have to add the following lines to the bash file of the Raspberry Pi:

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
-------------------------------------------
The following commands should be entered on the terminal of Raspberry Pi after booting it up.

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

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

pip install tensorflow-2.5.0-cp37-none-linux_armv7l.whl
-------------------------------------------
Access the terminal

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

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

Check if there is a shell file for your Python/Architecture combo here: https://github.com/PINTO0309/Tensorfl...

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"
    use: https://github.com/PINTO0309/Tensorfl...

If python3 -V = "3.7.*" AND uname -m = "armv7l"
    use: https://github.com/PINTO0309/Tensorfl...

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
curl https://pyenv.run | bash

(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)"         //run in terminal if python is not switching
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

-------------------------
eval "$(pyenv init -)"
eval "$(pyenv init --path)"
-------------------------

(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~~

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

 //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

----------------------------------------
https://github.com/lhelontra/tensorflow-on-arm/releases/download/v2.0.0/tensorflow-2.0.0-cp37-none-linux_aarch64.whl
----------------------------------------

python3 -m pip install -U wheel mock six

Select the .whl from https://github.com/PINTO0309/Tensorfl...
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

pip install protobuf==3.20.*

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
This is from: https://docs.h5py.org/en/stable/build... 

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

If you're on a Pi 3 I recommend following this tutorial up to step 17 and then returning to this video: https://www.youtube.com/watch?v=ylnjX...

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_

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

new video
opencv
-------------------------------
sudo nano /etc/dphys-swapfile 
increase to 2048

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start

ls

sudo chown -R pi:pi project/
clear
cd project
ls
sudo chmod 775 install.sh
ls
sudo ./install.sh


-------------------------------------------
installing open cv
-------------------------------------------


Access the terminal of your Pi

Check if you're using all of your system memory with:
 df h
If you're not using most of it, run
 sudo raspi-config
 advanced - expand filesystem
 reboot your pi

Update and upgrade
sudo apt-get update && sudo apt-get upgrade

Check your python version
python3 V

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

We need a bunch of system packages (credit to https://singleboardbytes.com/647/inst...)

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

//If you're using a PiCamera run:
pip install "picamera[array]"
//Users of PiCamera may also have to enable Camera Support:
 sudo raspi-config
 Inferface Options
 Legacy Camera Support - Enable


//Install OpenCV
pip install opencv-contrib-python
--Takes a long time

//Testing
python
import cv2
cv2._version_

-------------------------------------------
//Now I'm going to run my python script which I've written and that's how you'll run your file.
cd project
source env/bin/activate
eval "$(pyenv init -)"
python -V
cd env/fyp
python3 Traffic.py



Comments

Popular posts from this blog

Adding Arduino Library in Proteus 8 or later

Adding Arduino Library in Proteus Proteus  doesn't come up with  Arduino Libraries by default . We are using  Arduino Uno  in this project, and we'll see how to manually install Arduino libraries in  Proteus Professional .  You can use the path if it works for you. In my case, it works.  C:\ProgramData\Labcenter Electronics\Proteus 8 Professional\LIBRARY If it doesn't work, then you have to follow a few steps. All the required files can be downloaded using the link given at the end of the blog. So don't worry about the files. First, you have to download the files and paste  Arduino Library files  into the proteus library folder. Your folder might be installed in the  Programs  folder by default. But in  Proteus 8.9 , there is no library folder there.  ProgramData  folder contains the library folder of  Proteus Professional .  That's the folder in which you'll find your proteus library folder. In my case, it'...

Car Parking System using Arduino

This blog will show you how to design and create a Car Parking System using a Seven Segment display interfaced with Arduino Uno . You just need some components, and I assure you this project is gonna be fun! Components Needed Bread Board Jumper Wires Resistors (1 ohm) Arduino UNO IR-Sensors (2x) 7-Segment Display (Common-Cathode) Adding Arduino Library Proteus doesn't come up with Arduino Libraries . We are using Arduino Uno in this project, so manually installing Arduino libraries in Proteus Professional .  You can use the path if it works for you. In my case, it works.  C:\ProgramData\Labcenter Electronics\Proteus 8 Professional\LIBRARY If it doesn't work, then you have to follow a few steps. All the required files can be downloaded using the link given at the end of the blog. So don't worry about the files. First, you have to download the files and paste Arduino Library files into the proteus library folder. Your folder might be installed in the  Programs  fo...