4 # test if OpenCV already installed for python
5 function test_opencv_installed()
8 python3 -c "import cv2" > /dev/null 2>&1 || RC=$?
12 echo "OpenCV already setup for python3";
19 # install_opencv - installs OpenCV
20 function install_opencv()
22 # install package lsb-release if application lsb_release isn't installed
24 [ "${VERBOSE}" = "yes" ] && APT_QUIET=""
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"
32 if [[ `lsb_release -d` =~ .*Raspbian.* ]]
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
43 echo "Installing OpenCV";
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"
56 exec_and_search_errors "wget -O opencv.zip https://github.com/Itseez/opencv/archive/${VERSION}.zip"
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}/
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 \
70 -D BUILD_PERF_TESTS=OFF \
72 -D BUILD_EXAMPLES=OFF ..
74 # build and trap for errors in case we ran out of memory running make -j ${MAKE_NJOBS}
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"
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"
85 echo "MAKE_NJOBS=${MAKE_NJOBS}, suggestion is to increase swap space and try again"
88 echo -e "Error on line $LINENO. Will exit${NC}"
92 $SUDO_PREFIX make install
96 echo "Skipping OpenCV installation based on user input";
100 echo "Installing opencv python for non-Raspbian";
101 # check if pip2 & pip3 are available on the system via 'command'
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"
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"
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"
124 # main - this is the main function that runs the OpenCV install
127 echo "OpenCV Installation Starting"
129 # Test if OpenCV is installed. If OpenCV is already installed for python, script will exit
130 test_opencv_installed
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)
138 ### get constants (function is in install-utilities.sh)
140 ### Ask for sudo priviledges (function is in install-utilities.sh)
142 ### read config file (function is in install-utilities.sh)
149 echo "OpenCV Installation Finished"