api install needed sudo (#34)
[platform/adaptation/npu/intel-libmvnc.git] / install-opencv.sh
1 #! /bin/bash
2
3
4 # test if OpenCV already installed for python
5 function test_opencv_installed()
6 {
7     RC=0
8     python3 -c "import cv2" > /dev/null 2>&1 || RC=$?
9     if [ $RC -eq 0 ] ;
10     then
11         echo "";
12         echo "OpenCV already setup for python3";
13         echo "";
14         exit 0
15     fi;
16 }
17
18
19 # install_opencv - installs OpenCV
20 function install_opencv()
21 {
22     # install package lsb-release if application lsb_release isn't installed
23     APT_QUIET="-qq"
24     [ "${VERBOSE}" = "yes" ] && APT_QUIET=""
25     RC=0
26     command -v lsb_release > /dev/null || RC=$?
27     if [ $RC -ne 0 ] ; then
28         exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET update -y"
29         exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y lsb-release"
30     fi
31     
32     if [[ `lsb_release -d` =~ .*Raspbian.* ]] 
33     then 
34         echo ""
35         echo "************************ Please confirm *******************************"
36         echo " Installing OpenCV on Raspberry Pi may take a long time."
37         echo " You may skip this part of the installation in which case some examples "
38         echo " may not work without modifications but the rest of the SDK will still "
39         echo " be functional. Select n to skip OpenCV installation or y to install it." 
40         read -p " Continue installing OpenCV (y/n) ? " CONTINUE
41         if [[ "$CONTINUE" == "y" || "$CONTINUE" == "Y" ]]; then
42             echo ""; 
43             echo "Installing OpenCV"; 
44             echo "";
45             exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET update -y"
46             exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y build-essential cmake pkg-config"
47             exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev"
48             exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev"
49             exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y libxvidcore-dev libx264-dev"
50             exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y libgtk2.0-dev libgtk-3-dev"
51             exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y libatlas-base-dev gfortran"
52             exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y python2.7-dev python3-dev wget python3-pip"
53
54             cd $HOME
55             VERSION="3.3.0"
56             exec_and_search_errors "wget -O opencv.zip https://github.com/Itseez/opencv/archive/${VERSION}.zip"
57             ZIP_QUIET="-q"
58             [ "${VERBOSE}" = "yes" ] && ZIP_QUIET=""
59             unzip ${ZIP_QUIET} opencv.zip
60             exec_and_search_errors "wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/${VERSION}.zip"
61             unzip ${ZIP_QUIET} opencv_contrib.zip
62             cd ${HOME}/opencv-${VERSION}/
63             mkdir -p build
64             cd build
65             cmake -DBUILD_opencv_xfeatures2d=OFF -D CMAKE_BUILD_TYPE=RELEASE \
66                   -D CMAKE_INSTALL_PREFIX=/usr/local \
67                   -D INSTALL_PYTHON_EXAMPLES=OFF \
68                   -D OPENCV_EXTRA_MODULES_PATH=${HOME}/opencv_contrib-${VERSION}/modules \
69                   -D BUILD_DOCS=OFF \
70                   -D BUILD_PERF_TESTS=OFF \
71                   -D BUILD_TESTS=OFF \
72                   -D BUILD_EXAMPLES=OFF ..
73
74             # build and trap for errors in case we ran out of memory running make -j ${MAKE_NJOBS}
75             RC=0
76             make -j ${MAKE_NJOBS} || RC=$?
77             if [ $RC -ne 0 ] ; then
78                 echo -e "${RED}  Running make -j ${MAKE_NJOBS} failed."
79                 if [ ${MAKE_NJOBS} -gt 2 ] ; then
80                     echo "MAKE_NJOBS=${MAKE_NJOBS}, suggestion is to increase swap space and edit ncsdk.conf to uncomment #MAKE_NJOBS=1 and change to MAKE_NJOBS=2 or MAKE_NJOBS=1 and try again"
81                 else
82                     if [ ${MAKE_NJOBS} -gt 1 ] ; then
83                         echo "MAKE_NJOBS=${MAKE_NJOBS}, suggestion is increase swap space and edit ncsdk.conf to uncomment #MAKE_NJOBS=1 and try again"
84                     else
85                         echo "MAKE_NJOBS=${MAKE_NJOBS}, suggestion is to increase swap space and try again"  
86                     fi
87                 fi
88                 echo -e "Error on line $LINENO.  Will exit${NC}"
89                 exit 1
90             fi            
91
92             $SUDO_PREFIX make install
93             $SUDO_PREFIX ldconfig
94         else
95             echo "";
96             echo "Skipping OpenCV installation based on user input";
97             echo "";
98         fi
99     else  
100         echo "Installing opencv python for non-Raspbian";
101         # check if pip2 & pip3 are available on the system via 'command'
102         RC=0
103         command -v pip3 > /dev/null || RC=$?
104         if [ $RC -ne 0 ] ; then
105             exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET update -y"
106             exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y python3-pip"
107         fi
108         command -v pip2 > /dev/null || RC=$?
109         if [ $RC -ne 0 ] ; then
110             exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y python-pip"
111         fi
112
113         PIP_QUIET=--quiet
114         [ "${VERBOSE}" = "yes" ] && PIP_QUIET=""
115         exec_and_search_errors "$PIP_PREFIX pip3 install $PIP_QUIET opencv-python>=3.4.0.12"
116         exec_and_search_errors "$PIP_PREFIX pip3 install $PIP_QUIET opencv-contrib-python>=3.4.0.12"
117         exec_and_search_errors "$PIP_PREFIX pip2 install $PIP_QUIET opencv-python>=3.4.0.12"
118         exec_and_search_errors "$PIP_PREFIX pip2 install $PIP_QUIET opencv-contrib-python>=3.4.0.12"
119     fi
120 }
121
122
123
124 # main - this is the main function that runs the OpenCV install
125 function main()
126 {
127     echo "OpenCV Installation Starting"
128     
129     # Test if OpenCV is installed.  If OpenCV is already installed for python, script will exit 
130     test_opencv_installed
131
132     
133     ### initialization 
134     # read in functions shared by installer and uninstaller
135     source $(dirname "$0")/install-utilities.sh
136     # enable trapping for error (function is in install-utilities.sh)
137     set_error_handling
138     ### get constants (function is in install-utilities.sh)
139     initialize_constants
140     ### Ask for sudo priviledges (function is in install-utilities.sh)
141     ask_sudo_permissions
142     ### read config file (function is in install-utilities.sh) 
143     read_ncsdk_config
144
145     
146     ### install opencv
147     install_opencv
148
149     echo "OpenCV Installation Finished"    
150 }
151 main