Last active
May 14, 2020 11:58
-
-
Save kochie/2679f66cd1ae83c6a8d8ee286116988a to your computer and use it in GitHub Desktop.
A updated installation of OpenCV on Ubuntu 17.04. Also provides command line argument to compile ARM binaries aimed at Raspberry Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
VERSION=3.3.0 | |
PYTHON=/usr/bin/python3 | |
if [ $1 == "arm" ]; then | |
echo "Compiling for ARM (Raspberry Pi)" | |
sudo apt-get install gcc-arm-linux-gnueabi | |
sudo apt-get install gcc-arm-linux-gnueabihf | |
CROSS_COMPILE="-D CMAKE_TOOLCHAIN_FILE=$(pwd)/opencv/platforms/linux/arm-gnueabi.toolchain.cmake" | |
fi | |
sudo apt-get install -y libtiff5-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libv4l-dev libqt4-dev libgtk2.0-dev libtbb-dev libatlas-base-dev libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev libopencore-amrnb-dev libopencore-amrwb-dev x264 v4l-utils | |
sudo apt-get install -y libprotobuf-dev protobuf-compiler libgoogle-glog-dev libgflags-dev libgphoto2-dev libeigen3-dev libhdf5-dev doxygen | |
sudo apt-get install -y gcc-5 | |
sudo apt-get install -y python-dev python-pip python3-dev python3-pip | |
sudo -H pip3 install -U pip numpy | |
# Get linux distribution. | |
export $(cat /etc/lsb-release | grep DISTRIB_RELEASE) | |
export $(cat /etc/lsb-release | grep DISTRIB_ID) | |
# Install Caffe for some libraries to compile correctly. | |
if [ $DISTRIB_ID=="Ubuntu" && $DISTRIB_RELEASE < 17.04 ]; then | |
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler | |
sudo apt-get install --no-install-recommends libboost-all-dev | |
fi | |
if [ -x "usr/bin/nvcc" ]; then | |
sudo apt install caffe-cuda | |
else | |
sudo apt install caffe-cpu | |
fi | |
# Clone OpenCV and checlout wanted version. | |
if [ ! -d opencv ]; then | |
git clone https://github.com/opencv/opencv.git | |
fi | |
cd opencv | |
git checkout $VERSION | |
cd .. | |
# Clone OpenCV Contrib and checlout wanted version. | |
if [ ! -d opencv_contrib ]; then | |
git clone https://github.com/opencv/opencv_contrib.git | |
fi | |
cd opencv_contrib | |
git checkout $VERSION | |
cd .. | |
cd opencv | |
if [ ! -d build ]; then | |
mkdir build | |
fi | |
cd build | |
cmake -D CMAKE_BUILD_TYPE=RELEASE \ | |
-D CMAKE_INSTALL_PREFIX=/usr/local \ | |
-D INSTALL_C_EXAMPLES=ON \ | |
-D INSTALL_PYTHON_EXAMPLES=ON \ | |
-D PYTHON_EXECUTABLE=$(PYTHON) \ | |
-D WITH_TBB=ON \ | |
-D WITH_V4L=ON \ | |
-D WITH_QT=ON \ | |
-D WITH_OPENGL=ON \ | |
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \ | |
-D CUDA_NVCC_FLAGS="-ccbin gcc-5" \ | |
$CROSS_COMPILE \ | |
-D BUILD_EXAMPLES=ON .. | |
make -j$(nproc) | |
if [ -n $CROSS_COMPILE ]; then | |
echo "Installing as local binaries" | |
sudo make install -j$(nproc) | |
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf' | |
sudo ldconfig | |
else | |
echo "Since you are specifically compiling for ARM the binaries will not be installed" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment