Skip to content

Instantly share code, notes, and snippets.

@mudphone
Created April 14, 2020 21:28
Show Gist options
  • Save mudphone/9910f232398f9210c58a99491991956b to your computer and use it in GitHub Desktop.
Save mudphone/9910f232398f9210c58a99491991956b to your computer and use it in GitHub Desktop.

OpenCV V4 on Raspberry Pi 4, Buster

Why and How

Prepare

  • Update system libraries:
    • sudo apt-get update && sudo apt-get upgrade
  • Install dev tools (and cmake):
    • sudo apt-get install build-essential cmake unzip pkg-config
  • Install video libraries:
    • sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng-dev
    • sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
    • sudo apt-get install libxvidcore-dev libx264-dev
  • Install GTK:
    • sudo apt-get install libfontconfig1-dev libcairo2-dev
    • sudo apt-get install libgdk-pixbuf2.0-dev libpango1.0-dev
    • sudo apt-get install libgtk2.0-dev libgtk-3-dev
  • Install numerical optimizations for OpenCV:
    • sudo apt-get install libatlas-base-dev gfortran
  • Install HDF5 and Qt GUIs:
    • sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-103
    • sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5
  • Install Python 3 development headers:
    • sudo apt-get install python3-dev
  • TODO: Determine which of these libraries are strictly necessary for our deployment.

Create Virtual Environment

  • This assumes you're using a python version manager (such as pyenv) and the current (global or local) python is set to Python 3.7.3
  • Typically, work is done in a ~/work direction
    • mkdir ~/work
    • cd ~/work
  • Use pipenv to create a new virtual environment
    • mkdir ~/work/cv
    • cd ~/work/cv
    • pipenv --python `which python`
  • Activate the virtual environment
    • cd ~/work/cv
    • pipenv shell
  • Install NumPy prerequisite
    • cd ~/work/cv
    • pipenv install numpy
  • Install PiCamera API:
    • pipenv install "picamera[array]"

OpenCV Version 4.1.1

  • Source downloaded/prepared via
    • Typically, work is done in a ~/work direction
      • cd ~/work
    • OpenCV
      • wget https://github.com/opencv/opencv/archive/4.1.1.tar.gz
      • rename to opencv-4.1.1.tar.gz
      • extract: tar xvfz opencv-4.1.1.tar.gz
      • mv opencv-4.1.1 opencv
    • OpenCV Contrib
      • wget https://github.com/opencv/opencv_contrib/archive/4.1.1.tar.gz
      • rename to opencv_contrib-4.1.1.tar.gz
      • extract: tar xvfz opencv_contrib-4.1.1.tar.gz
      • mv opencv_contrib-4.1.1 opencv_contrib

Prepare Build

  • Work will be done in a build directory
    • mkdir ~/work/opencv/build
    • cd ~/work/opencv/build
  • Run make command:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=/home/pi/work/opencv_contrib/modules \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D BUILD_TESTS=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D CMAKE_SHARED_LINKER_FLAGS=-latomic \
-D BUILD_EXAMPLES=OFF ..
  • Note: OPENCV_EXTRA_MODULES_PATH is a full path to the opencv_contrib directory (plus /modules) that you created in a previous step.
  • Note this disclaimer from OpenCV:
Update 2018-11-27: Notice the -D OPENCV_ENABLE_NONFREE=ON  flag. Setting this flag with OpenCV 4 ensures that you’ll have access to SIFT/SURF and other patented algorithms.
  • Verify Python 3 interpreter and numpy library are coming from the pipenv virtual env location
    • e.g. for Python 3: /home/pi/.local/share/virtualenvs/cv-lRcGL5pf/bin/python

Backup Image File

  • TODO: This may be a good time to backup the .img file to have a stored starting point.

Increase Swap File Size

  • Edit the swap file config:
    • sudo nano /etc/dphys-swapfile
    • Change:
# CONF_SWAPSIZE=100
CONF_SWAPSIZE=2048
* Increases the swap file size to 2048MB
* The latest PyImageSearch instructions use a `CONF_SWAPSIZE` of 1024. But, I've kept it at 2048.
  • Restart swap service:
    • sudo /etc/init.d/dphys-swapfile stop
    • sudo /etc/init.d/dphys-swapfile start

Build

  • Run the make command across 4 cores:
    • make -j4
    • If this is unsuccessful, you can run make (wihthout -j4) to run single threaded. I was able to run this with multiple cores (with -j4)
  • Install library:
    • sudo make install
    • sudo ldconfig

Undo Swap File Changes

  • Set swap file back to its previous configuration settings
  • Restart swap service

Link OpenCV Into Your Virtual Environment

  • This step must be repeated for each Python virtual environment (which you create with pipenv) that you'd like to use import cv2
  • Go to the virtual environment location:
    • e.g. cd ~/.local/share/virtualenvs/cv-lRcGL5pf/lib/python3.7/site-packages
    • link to the library:
      • e.g. ln -s /usr/local/lib/python3.7/site-packages/cv2/python-3.7/cv2.cpython-37m-arm-linux-gnueabihf.so cv2.so
      • The first argument after ln -s is the location of the OpenCV library which we just built. Make sure the path is correct.

Test the Install

  • Go to your virtual environment, if you're not already there, and activate it
    • e.g. cd ~/work/cv
    • pipenv shell
    • Start a Python console: python
    • Import OpenCV: import cv2
      • There should be no errors, you should see the prompt (after a wait): >>>
    • Verify the OpenCV version: cv2.__version__
      • You should see the version we build: 4.1.1

Viewing GUI Via SSH

  • On Mac, use XQuartz to view images via SSH connection
    • Open an XQuartz terminal
    • ssh -Y pivision2.local
    • export DISPLAY=":0"
    • Run OpenCV program via SSH connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment