Minggu, 20 Maret 2016

Install dan Uninstall OpenCV di Ubuntu dengan ShellScript


Untuk yang berexperimen dengan Computer Vision. OpenCV menjadi library programming yang handal. Namun untuk installasinya cukup sulit karena OpenCV di desain agar bisa berjalan di berbagai mesin dan keperluan. Untuk pemula atau yang ingin coba-coba dengan OpenCV, installasi ke komputer cukup sulit dengan berbagai prosedurnya karena OpenCV berupa source code. Untuk itu saya membagikan script sederhana untuk installasi standar. Di sini saya membagikan script installasi untuk distro Ubuntu dan derivative-nya. Script ini juga bisa digunakan pada Fedora, CentOS dan turunan RHEL lain. Untuk distro lain atau memerlukan config lebih advanced bisa anda edit sendiri sesuai keperluan.

Script berikut akan mendownload OpenCV versi terbaru dan menginstallnya ke dalam komputer anda. Anda akan melihat beberapa komfirmasi untuk download. Anda juga bisa mendownload versi OpenCV lainnya saat konfirmasi. Jika anda sudah mendownload di directory yang sama, maka script berikut langsung menginstall tanpa mendownload.

Berikut shellscript yang saya buat untuk installasi :

#!/bin/bash
#########################################################################################################
## OpenCV Installation Script
##
## Code by : Dimas Toha Pramawitra (Lonehack)
## <dms.pram@gmail.com>
## 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!!"

Copy dan paste ke dalam file opencv-install.sh 
dan gunakan perintah berikut untuk menjalankan :
$ chmod +x opencv-install.sh
$ ./opencv-install.sh

Untuk Unistall dari komputer anda, gunakan script berikut :

#!/bin/bash
#########################################################################################################
## OpenCV Uninstallation Script
##
## Code by : Dimas Toha Pramawitra (Lonehack)
## <dms.pram@gmail.com>
## 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 "Uninstall manualy or edit this script for your need"
exit 0
fi
#################################################################################
## Initialization
#################################################################################
LIST=$($PKG $PKGLIST | grep opencv | awk '{print $1}')
VERSION=$(pkg-config --modversion opencv)
PYT=$(pkg-config --modversion python)
PFX="/usr/local/"
trap ctrl_c INT
function ctrl_c() {
echo "Terminated by User!"
exit 0
}
#################################################################################
## Uninstallation
#################################################################################
if [ -z "$VERSION" ];then
echo "OpenCV not found in pkg-config"
echo "If OpenCV was installed,"
echo "please uninstall remaining OpenCV manually"
else
echo "Uninstall OpenCV : "$VERSION
fi
read -rsp $'Are you sure? <y/N>\n' -n 1 key
if [[ "$key" =~ ^[Yy]$ ]]; then
# y pressed
echo "Uninstalling..."
if [ ! -z "$LIST" ];then
sudo $PKMGR remove $LIST
else
echo "OpenCV not found in dpkg"
echo "If OpenCV was installed,"
echo "please uninstall remaining OpenCV manually"
fi
#sudo find / -name "*opencv*" -exec rm {} \;
sudo rm -rf "$PFX"{include/opencv2,include/opencv,share/OpenCV}
sudo find "$PFX"lib -maxdepth 1 -name "libopencv*" -exec rm -f {} \;
sudo find "$PFX"bin/ -maxdepth 1 -name "opencv*" -exec rm -f {} \;
sudo find "$PFX"lib/pkgconfig -maxdepth 1 -name "opencv*" -exec rm -f {} \;
if [ ! -z "$PYT" ];then
sudo find "$PFX"lib/python$PYT/dist-packages/ -maxdepth 1 -name "cv*" -exec rm -f {} \;
fi
echo "Uninstall OpenCV done!"
else
echo "Uninstall OpenCV aborted.."
exit 0
fi
Copy dan paste ke dalam file opencv-uninstall.sh 
dan gunakan perintah berikut untuk menjalankan :
$ chmod +x opencv-uninstall.sh
$ ./opencv-uninstall.sh

Semoga script yang saya berikan dapat bermanfaat :D

Tidak ada komentar:

Posting Komentar