Skip to content

Instantly share code, notes, and snippets.

@zldrobit
Last active March 19, 2023 15:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zldrobit/fd67fa7919516abe99889d6fb4e0e5de to your computer and use it in GitHub Desktop.
Save zldrobit/fd67fa7919516abe99889d6fb4e0e5de to your computer and use it in GitHub Desktop.

Prerequisite

apt install git build-essential cmake python3-numpy [python-numpy]

Install FFmpeg (for OpenCV compilation)

apt-get install libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libavresample-dev

Install Gstreamer

apt-get install --no-install-recommends libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-doc gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio

Install GTK3 development package for GUI, e.g. cv2.imshow() (optional)

apt install libgtk-3-dev

Build opencv-python

pip install --upgrade pip
git clone https://github.com/opencv/opencv-python
cd opencv-python
MAKEFLAGS='-j10' pip wheel . --verbose
pip install {BUILT WHEEL}
Manual compilation

Follow ffmpeg's compliation wiki

https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

Build opencv-python

python -m pip install --upgrade pip
pip -V
pip install scikit-build
apt install git cmake
# chown -R root:root if using docker
python setup.py clean --all
cd /workspace
git clone https://github.com/opencv/opencv-python
PKG_CONFIG_PATH=/root/ffmpeg_build/lib/pkgconfig/ \
ENABLE_CONTRIB=1 \
python setup.py bdist_wheel -- -DOPENCV_EXTRA_MODULES_PATH=/workspace/opencv-python/opencv_contrib/modules \
    -DWITH_CUDA=ON -DENABLE_FAST_MATH=1 -DCUDA_FAST_MATH=1 -DWITH_CUBLAS=1 -DCUDA_ARCH_BIN='6.1 7.5 8.0 8.6'

Caveats:

  • Install a newer version of NASM with source
  • Git clone released tag (e.g. v3.3.0) of libaom
  • Git clone released tag (e.g. n5.0.1) of ffmpeg
  • Compile libaom with -DBUILD_SHARED_LIBS=1 to avoid opencv linking problem.
  • Install a newer version of meson with pip (may have to remove apt's meson)
  • To compile with ffmpeg, run opencv-python's build with pkg-config dir: setting environmental variable PKG_CONFIG_PATH
  • Run pip install scikit-build before building
  • Run sudo apt-get install libunistring-dev to avoid ERROR: gnutls not found using pkg-config in ffmpeg build (https://askubuntu.com/a/1253020/958072)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment