Last active
March 22, 2020 03:23
-
-
Save seanpianka/38f3b0806c70c33a8b3345f296ef3291 to your computer and use it in GitHub Desktop.
Py3 OpenCV on Raspberry Pi 3
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
# Remove useless programs to free up space | |
sudo apt-get remove --purge -y wolfram* libreoffice* sonic-pi minecraft-pi | |
sudo apt-get clean && sudo apt-get autoremove | |
# Update, upgrade, and install all OpenCV dependencies | |
sudo apt-get update -y && sudo apt-get upgrade -y | |
sudo apt install libgdk-pixbuf2.0-dev libpango1.0-dev libcairo2-dev libfontconfig1-dev libgdk-pixbuf2.0-dev libpango1.0-dev libcairo2-dev # GTK requires | |
sudo apt-get install build-essential cmake pkg-config libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libgtk2.0-dev libgtk-3-dev libatlas-base-dev gfortran python2.7-dev python3-dev # opencv requires | |
# virtualenv for Python, this is where OpenCV will be installed | |
sudo pip install virtualenv virtualenvwrapper | |
PROJECT_HOME=$HOME/Development | |
WORKON_HOME=$HOME/Development/.virtualenvs | |
mkdir -p $PROJECT_HOME | |
mkdir -p $WORKON_HOME | |
echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc | |
echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.bashrc | |
echo "export WORKON_HOME=$PROJECT_HOME" >> ~/.bashrc | |
echo "export PROJECT_HOME=$HWORKON_HOME" >> ~/.bashrc | |
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc | |
source ~/.bashrc | |
mkvirtualenv opencv | |
workon opencv # from this point on, we should be within the environment | |
# Necessary for OpenCV compilation | |
pip install numpy | |
# Download OpenCV | |
cd ~ | |
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.0.zip | |
unzip opencv.zip | |
rm opencv.zip | |
wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip | |
unzip opencv_contrib.zip | |
rm opencv_contrib.zip | |
mkdir ~/opencv/build | |
cd ~/opencv/build | |
# 1GB of swap space, avoid memory issues/hanging during compilation (http://disq.us/p/10ekwiz) | |
sudo sed -i -e 's/CONF_SWAPSIZE=.*$/CONF_SWAPSIZE=1024/g' /etc/dphys-swapfile | |
sudo /etc/init.d/dphys-swapfile stop | |
sudo /etc/init.d/dphys-swapfile start | |
# Begin compiling OpenCV | |
cmake -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ -DBUILD_TIFF=ON -DBUILD_opencv_java=OFF -DWITH_CUDA=OFF -DWITH_OPENGL=ON -DWITH_OPENCL=ON -DWITH_IPP=ON -DWITH_TBB=ON -DWITH_EIGEN=ON -DWITH_V4L=ON -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)") -DPYTHON_EXECUTABLE=$(which python) -DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -DPYTHON_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") .. | |
make -j4 | |
sudo make install | |
sudo ldconfig | |
# Reset swap space size after compilation is done | |
sudo sed -i -e 's/CONF_SWAPSIZE=.*$/CONF_SWAPSIZE=100/g' /etc/dphys-swapfile | |
sudo /etc/init.d/dphys-swapfile stop | |
sudo /etc/init.d/dphys-swapfile start | |
# Symlink OpenCV shared library file for our virtualenv | |
# Feel free to replace *s with your Python's version numbers | |
cd /usr/local/lib/python*/site-packages/ | |
sudo mv cv2.cpython-*-arm-linux-gnueabihf.so cv2.so | |
cd $WORKON_HOME/cv/lib/python*/site-packages/ | |
ln -s /usr/local/lib/python*/site-packages/cv2.so cv2.so | |
# Done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment