sabato 10 marzo 2018

Installazione di Raspbian Stretch + OpenCV 3 + Python sulla Raspberry Pi 3 (aggiornato il 28/9/18)

PREPARAZIONE DELLA MICROSD
Scarichiamo l'immagine lite di Raspbian Scratch del 2018-06-27
Usiamo Etcher per trasferire l'immagine su sdcard
CONFIGURAZIONE INIZIALE
Colleghiamo la RaspberryPI ad un monitor e a tastiera e mouse
Dopo l'avvio seguiamo il welcome setup che ci permette di impostare: country, timezone; password account pi; wifi

PREPARAZIONE DEL S.O
$ uname -a
Linux raspberrypi 4.14.50-v7+ #1122 SMP Tue Jun 19 12:26:26 BST 2018 armv7l GNU/Linux

sudo raspi-config
# Hostname -> SmartHome
# Boot options -> console autologin
# Interface options -> Camera -> yes
#                 -> SSH -> yes
#                 -> Serial-> yes
#                 -> Remote GPIO -> yes
# Advanced ...  -> 1 Expand Filesystems

ACCESSO REMOTO
Configurazione di MobaXterm portable per la connessione remota alla RaspberryPI
aggiunta account personale
sudo passwd root
adduser <newaccount>
# add new user to sudoers
sudo visudo
#add new user to video group
sudo cat /etc/group | grep pi
sudo usermod -a -G video $LOGNAME
sudo chmod 777 /dev/vchiq
#per abilitare la porta 80 (<1024) ad un utente non root
#how-to-run-a-server-on-port-80-as-a-normal-user-on-linux
sudo setcap 'cap_net_bind_service=+ep' /usr/bin/python3.5


Liberiamo un po' di spazio
#Disinstalliamo la la precedente versione installata di default su Raspbian:
sudo apt-get remove libopencv*
sudo apt-get autoremove 

#Per procedere servono almeno 2GB di spazio libero, altrimenti bisogna compilare su una unità esterna formattata come ext2
df -h

#Per liberare spazio (~1,6GB) possiamo eliminare LibreOffice e Wolfram engine
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | grep sonic
sudo apt-get purge wolfram-engine
sudo apt-get purge libreoffice*
sudo apt-get clean
sudo apt-get autoremove

Ci assicuriamo che il nostro S.O. Raspbian sia aggiornato:
date
sudo su
apt-get update -y
apt-get upgrade -y
apt-get dist-upgrade -y
#rpi-update #ultimo firmware sperimentale sconsigliato
apt-get autoremove
reboot
uname -a
#Linux raspberrypi 4.14.50-v7+ #1122 SMP Tue Jun 19 12:26:26 BST 2018 armv7l GNU/Linux
date


INSTALLAZIONE DEI PACCHETTI NECESSARI AD OPENCV
sudo su
date
# compilatore e strumenti di sviluppo
apt-get install -y build-essential
apt-get install -y cmake cmake-curses-gui pkg-config
apt-get install -y git

# image I/O packages, video I/O packages
apt-get install -y libjpeg8-dev    # https://packages.debian.org/it/wheezy/libjpeg8-dev
apt-get install -y libjpeg-dev
apt-get install -y libtiff5-dev    # https://packages.debian.org/it/wheezy/libtiff5-dev
apt-get install -y libjasper-dev   # https://packages.debian.org/it/wheezy/libjasper-dev
apt-get install -y libpng12-dev    # https://packages.debian.org/it/wheezy/libpng12-dev
apt-get install -y libavcodec-dev
apt-get install -y libavformat-dev

apt-get install -y libswscale-dev
apt-get install -y libv4l-dev v4l-utils
apt-get install -y libeigen3-dev
apt-get install -y libxvidcore-dev
apt-get install -y libx264-dev

# GTK development library
apt-get install -y libgtk2.0-dev 
apt-get install -y libgtk-3-dev    #https://packages.debian.org/stretch/libgtk-3-dev
apt-get install libcanberra-gtk*



#  matrix operations
apt-get install -y libatlas-base-dev gfortran

# Python 2.7 and Python 3 header files
apt-get install -y python2.7-dev 
apt-get install -y python3-dev

# Python package manager
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
python3 get-pip.py

# Installazione di NumPy
pip install numpy

# opzionali (li ho installati)
apt-get install -y python-dev   # https://packages.debian.org/it/wheezy/python-dev
apt-get install -y python-numpy # https://packages.debian.org/it/wheezy/python-numpy
apt-get install -y python3-numpy


# opzionali (non li ho installati)
#apt-get install libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools libtiff4 libtiffxx0c2 libtiff-tools libeigen3-dev
#apt-get install libpng12-0 libjpeg8 libjpeg8-dbg libjpeg-progs ffmpeg libavcodec53 libavformat53 libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev swig



date

INSTALLAZIONE e CREAZIONE DEL VIRTUAL ENVIRONMENT DI PYTHON
# installazione di virtualenv (opzionle)
pip install virtualenv virtualenvwrapper
rm -rf get-pip.py  .cache/pip
echo -e "\n# virtualenv and virtualenvwrapper" >> .profile
echo "export WORKON_HOME=$HOME/.virtualenvs" >> .profile
echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> .profile
echo "source /usr/local/bin/virtualenvwrapper.sh" >> .profile
# Per rendere effettive le modifiche
source .profile


mkvirtualenv py2cv3 -p python2 mkvirtualenv py3cv3 -p python3 mkvirtualenv py3cv4 -p python3
#per attivare l'ambiente virtuale
workon py3cv3 

# quit virtual environment
deactivate

COMPILAZIONE ED INSTALLAZIONE DI OPENCV
Download di OpenCV da http://opencv.org/releases.html o http://sourceforge.net/projects/opencvlibrary/files/opencv-unix
https://github.com/opencv/opencv

wget https://github.com/opencv/opencv/archive/3.4.3.zip -O opencv_source.zip
wget https://github.com/opencv/opencv_contrib/archive/3.4.3.zip -O opencv_contrib.zip
unzip opencv_source.zip
unzip opencv_contrib.zip
cd opencv-3.4.3
mkdir build
cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE \
 -D CMAKE_INSTALL_PREFIX=/usr/local \
 -D BUILD_DOCS=OFF \
 -D BUILD_EXAMPLES=OFF \
 -D BUILD_TESTS=OFF \
 -D BUILD_opencv_ts=OFF \
 -D BUILD_PERF_TESTS=OFF \
 -D INSTALL_C_EXAMPLES=ON \
 -D INSTALL_PYTHON_EXAMPLES=ON \
 -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.3/modules \
 -D ENABLE_NEON=ON \
 -D WITH_LIBV4L=ON \
        ../

Se si vuole modificare o visualizzare la configurazione tramite interfaccia grafica:

ccmake ../
Configurazione
press ‘c’ to configure once done toggle the options you want.
press ‘c’ again to configure with your new settings
press ‘g’ to generate the Makefile
press ‘q’ to quit without change

increasing your swap space
nano /etc/dphys-swapfile
# CONF_SWAPSIZE=100
CONF_SWAPSIZE=1024
/etc/init.d/dphys-swapfile stop
/etc/init.d/dphys-swapfile start


Finalmente si può passare alla compilazione. Questa operazione può durare più di 12 ore! (sulla raspberry Pi 1) meno di 2 ore sulla Raspberry 3
sudo su
date
#sab 29 set 2018, 00.10.30, CEST
make -j4 
make install
ldconfig
date
#Sat 10 Mar 21:42:18 UTC 2018

Reimpostare la dimensione predefinita dello swap space, per preservare l'SDcard
nano /etc/dphys-swapfile
# CONF_SWAPSIZE=100
CONF_SWAPSIZE=1024
/etc/init.d/dphys-swapfile stop
/etc/init.d/dphys-swapfile start

INSTALLAZIONE DI DLIB
$ sudo apt-get update
$ sudo apt-get install build-essential cmake
$ sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev
$ sudo apt-get install libx11-dev libgtk-3-dev
$ sudo pip install numpy
$ sudo pip install dlib
https://www.pyimagesearch.com/2018/01/22/install-dlib-easy-complete-guide/

RIFERIMENTI / LINK
http://pklab.net/index.php?lang=IT&id=392
http://www.raspberrypi.org/tag/opencv/
http://docs.opencv.org/doc/tutorials/introduction/linux_install/linux_install.html#linux-installation
http://robertcastle.com/2014/02/installing-opencv-on-a-raspberry-pi/
http://www.raspberrypi.org/magpi-issues/MagPi28.pdf
http://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/
https://github.com/Guzunty/Pi/wiki/Say-Hello-to-PiTeR
http://www.pyimagesearch.com/2016/04/18/install-guide-raspberry-pi-3-raspbian-jessie-opencv-3/
https://www.pyimagesearch.com/2017/09/04/raspbian-stretch-install-opencv-3-python-on-your-raspberry-pi/
https://github.com/erogol/RaspiSecurity/blob/master/install_opencv.sh
https://www.raspberrypi-spy.co.uk/2018/03/free-space-raspberry-pi-sd-card/


Nessun commento:

Posta un commento