Skip to content

Instantly share code, notes, and snippets.

@lonehack
Last active April 17, 2016 01:08
Show Gist options
  • Save lonehack/86c8cce4c64cb4ef0a17 to your computer and use it in GitHub Desktop.
Save lonehack/86c8cce4c64cb4ef0a17 to your computer and use it in GitHub Desktop.
Ubuntu shellscript to install OpenCV
#!/bin/bash
#########################################################################################################
## OpenCV Installation Script
##
## Code by : Dimas Toha Pramawitra (Lonehack)
## <[email protected]>
## Created : 20 Mar 2016
## Modified : 16 Apr 2016
##
## This code is released to the public domain.
## You can use, modify, or distribute this code as you need without any restriction.
#########################################################################################################
#################################################################################
## Distribution check
#################################################################################
if [ -f /etc/debian_version ];then
DIST="debian"
PKG=$(which dpkg)
PKGMGR=$(which apt-get)
PKGLIST=" --get-selections"
elif [ -f /etc/redhat-release ];then
DIST="redhat"
PKG=$(which rpm)
PKGMGR=$(which dnf)
if [ -z "$PKGMGR" ];then
PKGMGR=$(which yum)
fi
PKGLIST="--all"
else
echo "Your Linux Distribution not yet supported by this script"
echo "Install manualy or edit this script for your need"
exit 0
fi
sudo $PKGMGR install -y pkg-config
#################################################################################
## Initialing and checking
#################################################################################
LIST=$($PKG $PKGLIST | grep opencv | awk '{print $1}')
OLDVER=$(pkg-config --modversion opencv)
OLDBASE=$(echo $OLDVER | cut -d'.' -f 1)
OCVURL="https://sourceforge.net/projects/opencvlibrary/files/opencv-unix"
VERSION="$(wget -q -O - $OCVURL | egrep -m1 -o '\"[0-9](\.[0-9]+)+' | cut -c2-)"
PREFIX="/usr/local/"
## Version Check
echo "OpenCV current version :" $VERSION
if [ "$OLDVER" = "$VERSION" ]; then
echo "OpenCV already newest version :" $VERSION
echo "If you want to downgrade, continue this script"
read -rsp $'Continue installation? <y/N>\n' -n 1 key
if [[ ! "$key" =~ ^[Yy]$ ]]; then
exit 0
fi
fi
trap ctrl_c INT
function ctrl_c() {
echo "Terminated by User!"
exit 0
}
set -e
#################################################################################
## Function here
#################################################################################
debian_install () {
if [ ! -z "$LIST" ];then
echo "Removing pre-installed OpenCV" $OLDVER
sudo apt-get remove $LIST
echo "Removing OpenCV" $OLDVER "done!"
else
echo "OpenCV not found in pkg-config"
echo "If old OpenCV was installed,"
echo "please uninstall old OpenCV manually"
fi
echo "Removing any pre-installed ffmpeg and x264"
sudo apt-get remove libav-tools \
x264 \
libx264-dev
echo "Installing Dependenices"
sudo apt-get install -y libopencv-dev \
build-essential \
checkinstall \
cmake \
yasm \
libjpeg-dev \
libjasper-dev \
libavcodec-dev \
libavformat-dev \
libswscale-dev \
libdc1394-22-dev \
libxine-dev \
libgstreamer0.10-dev \
libgstreamer-plugins-base0.10-dev \
libv4l-dev \
python-dev \
python-numpy \
libtbb-dev \
libqt4-dev libgtk2.0-dev \
libfaac-dev \
libmp3lame-dev \
libtheora-dev \
libvorbis-dev \
libxvidcore-dev \
x264 \
v4l-utils \
libav-tools
}
redhat_install () {
if [ ! -z "$LIST" ];then
echo "Removing pre-installed OpenCV" $OLDVER
sudo $PKGMGR erase $LIST
echo "Removing OpenCV" $OLDVER "done!"
else
echo "OpenCV not found in pkg-config"
echo "If old OpenCV was installed,"
echo "please uninstall old OpenCV manually"
fi
echo "Removing any pre-installed ffmpeg and x264"
sudo $PKGMGR erase libav-tools \
x264 \
libx264-devel
echo "Installing Dependenices"
sudo $PKGMGR group install -y "Development Tools"
sudo $PKGMGR group install -y "C Development Tools and Libraries"
sudo $PKGMGR install -y opencv-devel \
checkinstall \
cmake \
yasm \
libjpeg-devel \
libjasper-devel \
libavcodec-devel \
libavformat-devel \
libswscale-devel \
libdc1394-22-devel \
libxine-devel \
libgstreamer0.10-devel \
libgstreamer-plugins-base0.10-devel \
libv4l-devel \
python-devel \
python-numpy \
libtbb-devel \
libqt4-devel \
libgtk2.0-devel \
libfaac-devel \
lame-devel \
libtheora-devel \
libvorbis-devel \
libxvidcore-devel \
x264 \
v4l-utils \
libav-tools
}
#################################################################################
## Initialing and checking
#################################################################################
## Remove pre-installed OpenCV
## install Dependenices OpenCV
if [ "$DIST" = "debian" ];then
debian_install
elif [ "$DIST" = "redhat" ];then
redhat_install
fi
## Download OpenCV
if [ ! -e opencv-$VERSION.zip ];then
echo "Latest version :" $VERSION
read -rsp $'Download latest version? <Y/n>\n' -n 1 key
if [[ "$key" =~ ^[Nn]$ ]]; then
# n pressed
read -rp $'Enter OpenCV version[2.4, 2.4.x or 3] :\n' down
if [[ ! "$down" =~ ^[0-9]+(\.[0-9]+)+* ]];then
echo "invalid version search"
exit 0
fi
VERSION="$(wget -q -O - "$OCVURL" | egrep -m1 -o '\"'$down'(\.[0-9]+)+*' | cut -c2-)"
if [ ! -z $VERSION ];then
if [ ! -e opencv-$VERSION.zip ];then
echo "Download OpenCV " $VERSION
wget -O opencv-$VERSION.zip $OCVURL/$VERSION/opencv-$VERSION.zip/download
fi
else
echo "OpenCV version not found!"
exit 0
fi
else
echo "Downloading OpenCV" $VERSION
wget -O opencv-$VERSION.zip $OCVURL/$VERSION/opencv-$VERSION.zip/download
fi
fi
echo "Building OpenCV"
unzip opencv-$VERSION.zip
cd opencv-$VERSION
if [ ! -e build ];then
mkdir build
fi
cd build
/usr/bin/cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D WITH_V4L=ON \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_TESTS=ON \
-D BUILD_EXAMPLES=ON \
-D WITH_TBB=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
..
make -j4
## Delete old OpenCV library
if [ ! -z "$OLDVER" ];then
echo "Old version OpenCV detected : "$OLDVER
read -rsp $'Delete old library? <y/N>\n' -n 1 key
if [[ $key =~ ^[Yy]$ ]]; then
# y pressed
echo "Uninstall old OpenCV library :" $OLDVER
sudo find "$PREFIX"/lib -maxdepth 1 -name "libopencv*.$OLDBASE*" -exec rm -f {} \;
else
echo "Old library not deleted"
fi
fi
echo "Installing OpenCV"
sudo checkinstall
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
echo "OpenCV "$VERSION" installation finished!!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment