Last active
March 1, 2019 16:46
-
-
Save droter/4c5853fe50ebb4a47217c03642910b5b to your computer and use it in GitHub Desktop.
Install opencv for python3 on raspbian. https://raw.githubusercontent.com/movidius/ncsdk/ncsdk2/install-opencv.sh
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 | |
# test if OpenCV already installed for python | |
function test_opencv_installed() | |
{ | |
RC=0 | |
python3 -c "import cv2" > /dev/null 2>&1 || RC=$? | |
if [ $RC -eq 0 ] ; | |
then | |
echo ""; | |
echo "OpenCV already setup for python3"; | |
echo ""; | |
exit 0 | |
fi; | |
} | |
# install_opencv - installs OpenCV | |
function install_opencv() | |
{ | |
# install package lsb-release if application lsb_release isn't installed | |
APT_QUIET="-qq" | |
[ "${VERBOSE}" = "yes" ] && APT_QUIET="" | |
RC=0 | |
command -v lsb_release > /dev/null || RC=$? | |
if [ $RC -ne 0 ] ; then | |
exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET update -y" | |
exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y lsb-release" | |
fi | |
if [[ `lsb_release -d` =~ .*Raspbian.* ]] | |
then | |
echo "" | |
echo "************************ Please confirm *******************************" | |
echo " Installing OpenCV on Raspberry Pi may take a long time." | |
echo " You may skip this part of the installation in which case some examples " | |
echo " may not work without modifications but the rest of the SDK will still " | |
echo " be functional. Select n to skip OpenCV installation or y to install it." | |
read -p " Continue installing OpenCV (y/n) ? " CONTINUE | |
if [[ "$CONTINUE" == "y" || "$CONTINUE" == "Y" ]]; then | |
echo ""; | |
echo "Installing OpenCV"; | |
echo ""; | |
exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET update -y" | |
exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y build-essential cmake pkg-config" | |
exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev" | |
exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev" | |
exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y libxvidcore-dev libx264-dev" | |
exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y libgtk2.0-dev libgtk-3-dev" | |
exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y libatlas-base-dev gfortran" | |
exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y python2.7-dev python3-dev wget python3-pip" | |
cd $HOME | |
VERSION="3.4.0" | |
exec_and_search_errors "wget -O opencv.zip https://github.com/Itseez/opencv/archive/${VERSION}.zip" | |
ZIP_QUIET="-q" | |
[ "${VERBOSE}" = "yes" ] && ZIP_QUIET="" | |
unzip ${ZIP_QUIET} opencv.zip | |
exec_and_search_errors "wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/${VERSION}.zip" | |
unzip ${ZIP_QUIET} opencv_contrib.zip | |
cd ${HOME}/opencv-${VERSION}/ | |
mkdir -p build | |
cd build | |
cmake -DBUILD_opencv_xfeatures2d=OFF -D CMAKE_BUILD_TYPE=RELEASE \ | |
-D CMAKE_INSTALL_PREFIX=/usr/local \ | |
-D INSTALL_PYTHON_EXAMPLES=OFF \ | |
-D OPENCV_EXTRA_MODULES_PATH=${HOME}/opencv_contrib-${VERSION}/modules \ | |
-D BUILD_DOCS=OFF \ | |
-D BUILD_PERF_TESTS=OFF \ | |
-D BUILD_TESTS=OFF \ | |
-D BUILD_EXAMPLES=OFF .. | |
# build and trap for errors in case we ran out of memory running make -j ${MAKE_NJOBS} | |
RC=0 | |
make -j ${MAKE_NJOBS} || RC=$? | |
if [ $RC -ne 0 ] ; then | |
echo -e "${RED} Running make -j ${MAKE_NJOBS} failed." | |
if [ ${MAKE_NJOBS} -gt 2 ] ; then | |
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" | |
else | |
if [ ${MAKE_NJOBS} -gt 1 ] ; then | |
echo "MAKE_NJOBS=${MAKE_NJOBS}, suggestion is increase swap space and edit ncsdk.conf to uncomment #MAKE_NJOBS=1 and try again" | |
else | |
echo "MAKE_NJOBS=${MAKE_NJOBS}, suggestion is to increase swap space and try again" | |
fi | |
fi | |
echo -e "Error on line $LINENO. Will exit${NC}" | |
exit 1 | |
fi | |
$SUDO_PREFIX make install | |
$SUDO_PREFIX ldconfig | |
else | |
echo ""; | |
echo "Skipping OpenCV installation based on user input"; | |
echo ""; | |
fi | |
else | |
echo "Installing opencv python for non-Raspbian"; | |
# check if pip2 & pip3 are available on the system via 'command' | |
RC=0 | |
command -v pip3 > /dev/null || RC=$? | |
if [ $RC -ne 0 ] ; then | |
exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET update -y" | |
exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y python3-pip" | |
fi | |
command -v pip2 > /dev/null || RC=$? | |
if [ $RC -ne 0 ] ; then | |
exec_and_search_errors "$SUDO_PREFIX apt-get $APT_QUIET install -y python-pip" | |
fi | |
PIP_QUIET=--quiet | |
[ "${VERBOSE}" = "yes" ] && PIP_QUIET="" | |
exec_and_search_errors "$PIP_PREFIX pip3 install $PIP_QUIET opencv-python>=3.4.0.12" | |
exec_and_search_errors "$PIP_PREFIX pip3 install $PIP_QUIET opencv-contrib-python>=3.4.0.12" | |
exec_and_search_errors "$PIP_PREFIX pip2 install $PIP_QUIET opencv-python>=3.4.0.12" | |
exec_and_search_errors "$PIP_PREFIX pip2 install $PIP_QUIET opencv-contrib-python>=3.4.0.12" | |
fi | |
} | |
# main - this is the main function that runs the OpenCV install | |
function main() | |
{ | |
echo "OpenCV Installation Starting" | |
# Test if OpenCV is installed. If OpenCV is already installed for python, script will exit | |
test_opencv_installed | |
### install opencv | |
install_opencv | |
echo "OpenCV Installation Finished" | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment