From 627b320ec7ce2821ae10943b27b2f0d575518e0f Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Thu, 14 Nov 2024 11:37:37 +0100 Subject: [PATCH] Build python-opencv with gstreamer support --- build_opencv_with_gstreamer.sh | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 build_opencv_with_gstreamer.sh diff --git a/build_opencv_with_gstreamer.sh b/build_opencv_with_gstreamer.sh new file mode 100644 index 0000000..ff13d9c --- /dev/null +++ b/build_opencv_with_gstreamer.sh @@ -0,0 +1,45 @@ +#!/bin/bash +# When using RTSP gstreamer can provides a way lower latency then ffmpeg +# and exposes more options to tweak the connection. However, the pypi +# version of python-opencv is build without gstreamer. Thus, we need to +# build our own python wheel. + +# adapted from https://github.com/opencv/opencv-python/issues/530#issuecomment-1006343643 + +# install gstreamer dependencies +sudo apt-get install --quiet -y --no-install-recommends \ + gstreamer1.0-gl \ + gstreamer1.0-opencv \ + gstreamer1.0-plugins-bad \ + gstreamer1.0-plugins-good \ + gstreamer1.0-plugins-ugly \ + gstreamer1.0-tools \ + libgstreamer-plugins-base1.0-dev \ + libgstreamer1.0-0 \ + libgstreamer1.0-dev + +# ffmpeg deps +sudo apt install ffmpeg libgtk2.0-dev libavformat-dev libavcodec-dev libavutil-dev libswscale-dev libtbb-dev libjpeg-dev libpng-dev libtiff-dev + +OPENCV_VER="84" #fix at 4.10.0.84, or use rolling release: "4.x" +STARTDIR=$(pwd) +TMPDIR=$(mktemp -d) + +# Build and install OpenCV from source. +echo $TMPDIR + +# pyenv compatibility +cp .python-version $TMPDIR + +cd "${TMPDIR}" +git clone --branch ${OPENCV_VER} --depth 1 --recurse-submodules --shallow-submodules https://github.com/opencv/opencv-python.git opencv-python-${OPENCV_VER} +cd opencv-python-${OPENCV_VER} +export ENABLE_CONTRIB=0 +# export ENABLE_HEADLESS=1 +# We want GStreamer support enabled. +export CMAKE_ARGS="-DWITH_GSTREAMER=ON -DWITH_FFMPEG=ON" +python -m pip wheel . --verbose -w $STARTDIR + +# # Install OpenCV +# python3 -m pip install opencv_python*.whl +# cp opencv_python*.whl $STARTDIR \ No newline at end of file