Skip to content

Instantly share code, notes, and snippets.

@ivanmonteiro
Last active July 22, 2020 14:51
Show Gist options
  • Select an option

  • Save ivanmonteiro/9e07403870a435367b5090d3003b14a7 to your computer and use it in GitHub Desktop.

Select an option

Save ivanmonteiro/9e07403870a435367b5090d3003b14a7 to your computer and use it in GitHub Desktop.
#Script to install Opencv 3.4.4 with optmizations for raspberry pi that can increase performance up to 50%!
#install pre-requisites
sudo apt-get install build-essential checkinstall cmake pkg-config yasm git gfortran libjpeg-dev libjasper-dev libpng-dev libtiff5-dev libtiff-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libv4l-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libgtk2.0-dev libtbb-dev qt5-default libatlas-base-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libopencore-amrnb-dev libopencore-amrwb-dev libavresample-dev x264 v4l-utils libprotobuf-dev protobuf-compiler libgoogle-glog-dev libgflags-dev libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
#add symlink for libv4l
sudo ln -s -f /usr/include/libv4l1-videodev.h /usr/include/linux/videodev.h
#download and install Intel TBB precompiled for raspberry pi 2/3 b
git clone https://github.com/abhiTronix/TBB_Raspberry_pi.git
cd TBB_Raspberry_pi/
sudo dpkg -i libtbb-dev_4.5-1_armhf.deb
sudo ldconfig
#install numpy
pip3 install numpy
#Run commands below or open up your swap config file at /etc/dphys-swapfile and then edit the CONF_SWAPSIZE variable increasing swap to 1GB. This is needed because compiling Opencv consumes more than the available RAM memory, needing bigger swap file. Remember to undo this after install to prevent flash memory problems (a.k.a "memory wearing").
sudo sed -i 's/CONF_SWAPSIZE=100/CONF_SWAPSIZE=1024/g' /etc/dphys-swapfile
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
#download opencv 3.4.4
cd ~
wget -O opencv.zip https://github.com/opencv/opencv/archive/3.4.4.zip
unzip opencv.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/3.4.4.zip
unzip opencv_contrib.zip
cd ~/opencv-3.4.4/
mkdir build
cd build
#build opencv 3.4.4 using intel TBB, neon and vfpv3 optimizations for raspberry pi
#optimizations found at https://www.raspberrypi.org/forums/viewtopic.php?t=233854 and adapted cmake/OpenCVCompilerOptions.cmake from tutorial http://www.ebenezertechs.com/optimizing-opencv-3-4-in-raspberry-pi-3-using-gstreamer/ using compile flags for RP3 found at https://gist.github.com/fm4dd/c663217935dc17f0fc73c9c81b0aa845
export CFLAGS="-mcpu=cortex-a53 -mfloat-abi=hard -mfpu=neon-fp-armv8 -mneon-for-64bits -DTBB_USE_GCC_BUILTINS=1 -D__TBB_64BIT_ATOMICS=0"
export CXXFLAGS="-mcpu=cortex-a53 -mfloat-abi=hard -mfpu=neon-fp-armv8 -mneon-for-64bits -DTBB_USE_GCC_BUILTINS=1 -D__TBB_64BIT_ATOMICS=0"
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.4.4/modules \
-D PYTHON_DEFAULT_EXECUTABLE=$(which python3) \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D WITH_TBB=ON \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D BUILD_TESTS=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_PERF_TESTS=ON \
-D BUILD_EXAMPLES=OFF ..
make -j4
sudo make install
sudo ldconfig
#find where cv2***.so was installed and create a symbolic link to your python package distribution folder
sudo ln -s -f /usr/local/python/cv2/python-3.7/cv2.cpython-37m-arm-linux-gnueabihf.so /usr/local/lib/python3.7/dist-packages/cv2.so
#decrease swap file size to prevent problems with flash memory wearing. Default is 100 mb. Can be done manually editing etc/dphys-swapfile or running commands below:
sudo sed -i 's/CONF_SWAPSIZE=1024/CONF_SWAPSIZE=100/g' /etc/dphys-swapfile
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment