Friday, April 19, 2019

OpenCV 4.1.0 on Raspberry Pi 3B+ w/ Raspbian Stretch (2019 April)

OpenCV 4.1.0    April 11th, 2019  (?)
Raspbian Stretch with Desktop  April 8th, 2019

8GB micro sd card

--- 

download raspbian from https://www.raspberrypi.org/downloads/raspbian/

# write raspbian image onto sd card, assuming /dev/sda
unzip -p 2019-04-08-raspbian-stretch.zip | sudo dd of=/dev/sdb bs=4M conv=fsync

# first time launch on pi would expand file system automatically

# default auto-login to desktop, changed to console autologin
sudo raspi-config
# boot options > desktop
sudo reboot

# check temperature
/opt/vc/bin/vcgencmd measure_temp

## opencv installation, ref to pyimagesearch opencv 4.0 installation

# 8GB sd card too small, removed un-used packages
sudo apt-get purge wolfram-engine
sudo apt-get purge libreoffice*
sudo apt clean
sudo apt-get autoremove

sudo apt update && sudo apt upgrade

# install dev essential
sudo apt install build-essential cmake unzip pkg-config

# images libraries
sudo apt install libjpeg-dev libpng-dev libtiff-dev

# video libraries
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
# video format
sudo apt install libxvidcore-dev libx264-dev
# gtk    # 147MB
sudo apt install libgtk-3-dev
# numerical
sudo apt install libatlas-base-dev gfortran

# python 3 dev
sudo apt install python3-dev

# download opencv & opencv_contrib  # contrib are additional utils
# i used an usb drive to save room on sd card
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.0.zip

unzip *.zip

cd opencv-4.1.0
mkdir build
cd build

# configure build
$ cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=/<mypathto>/opencv_contrib/modules \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D BUILD_TESTS=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_PYTHON_EXAMPLES=OFF \

-D BUILD_EXAMPLES=OFF ..

# temporarily increase swap size
vi /etc/dphys-swapfile
# set to  "CONF_SWAPSIZE=1536"

# restart service
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start

# i previous changed to console, to save mem
# also change gpu mem split in raspi-config to 16MB

# build, allow 2 hours 
make -j1 &
# python binding took long time
# build folder is now 3.6 GB

# to show mem and swap used
free -h
# mem could go down to 28MB, and swap used around 400MB

# install to sd card
sudo make install
sudo ldconfig

# test in python
python
>>> import cv2
>>> cv2.__version__
>>> exit()

# now can revert changes in GPU mem split, and swap size back to 100MB
# boot option to desktop, need password

(edited 2019 June) At first I thought the bottleneck is at the micro SD write speed.  Therefore, using an external USB drive instead.  Turn out that build time no change, it seems reaching the maximum processing power.

Labels: , , ,