opencv-python icon indicating copy to clipboard operation
opencv-python copied to clipboard

How to install with GStreamer support

Open kwiley-s3global opened this issue 3 years ago • 8 comments

I used the "pip install opencv-contrib-python" method and if I run the following:

import cv2
print(cv2.getBuildInformation())

I can plainly see that GStreamer isn't enabled. I'm not very adept at the sublteties of pip, wheels, brew, etc. I've look over lots of articles that attempt to walk through building and installing opencv with GStreamer, but I haven't found anything specific to opencv-python. Ultimately, I nave not succeeded yet in installing opencv with GStreamer support (I'm on a Mac).

Does anyone know how to just make this work?

Thank you.

kwiley-s3global avatar Aug 24 '21 16:08 kwiley-s3global

@kwiley-s3global Happened to see this while looking for another issue.

Gstreamer support requires installing gstreamer and then building OpenCV with it. I posted instructions for doing that here in January - hopefully they still work as expected :-)

ES-Alexander avatar Oct 13 '21 03:10 ES-Alexander

They work. I've adapted them in a script of my own like so:

OPENCV_VER="master"
TMPDIR=$(mktemp -d)

# Build and install OpenCV from source.
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"
python3 -m pip wheel . --verbose

# Install OpenCV
python3 -m pip install opencv_python*.whl

This is on the latest 64-bit Raspberry Pi OS Lite (Bullseye) on RPi4, where I do not need the GUI components.

achton avatar Jan 06 '22 07:01 achton

@achton , how did you install gstreamer or dont you need to do that? When I just install it, is see its downloading gtk packages, so its not really headless anymore.

bluebrown avatar Sep 12 '23 18:09 bluebrown

@bluebrown I install it via apt-get:

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 \

I've not really thought about its dependencies. I was mainly interested in avoiding a full desktop system, but I think the RPi foundation is now phasing out the "Lite" moniker and calling that version specifically "RPi OS with Desktop" to distinguish it from the "headless" OS version.

achton avatar Sep 18 '23 07:09 achton

Thanks man !

Nacriema avatar Sep 26 '23 06:09 Nacriema

do you have new guide for windows?

HunterShinobiTitan avatar Oct 18 '23 05:10 HunterShinobiTitan

Here is the minimal steps. Here is my enviornment

  • Windows 10
  • Visual Studio Build Tools 2019
  • CMake 3.29.0-rc1 (use default install setting)
  • Gstreamer:(Use default install setting) MSVC 64-bit (VS 2019, Release CRT) 1.22.10 runtime installer 1.22.10 development installer
  • Python3.8.10

For example, my gstreamer is install in D disk by default.

  1. Add System Environment Variable GSTREAMER_ROOT_X86_64 value D:\gstreamer\1.0\msvc_x86_64 https://gstreamer.freedesktop.org/documentation/installing/on-windows.html?gi-language=c#building-the-tutorials
  2. Add enviornment variable GST_PLUGIN_PATH value D:\gstreamer\1.0\msvc_x86_64\lib\gstreamer-1.0
  3. Add Path enviornment variable value D:\gstreamer\1.0\msvc_x86_64\bin
  4. re-login the computer
  5. create a Python venv and use it
  6. pip install --verbose --no-binary opencv-python opencv-python==4.6.0.66
  7. os.add_dll_directory() Shoud be add to code since from Python 3.8+, Python will NOT search DLL from PATH enviornment variable.(https://bugs.python.org/issue43173)
  8. test code
import os
os.add_dll_directory("D:\\gstreamer\\1.0\\msvc_x86_64\\bin")
import cv2
gst = 'rtspsrc location=rtsp://192.168.8.57/live1s3.sdp timeout= 30000 ! decodebin ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1'

cap = cv2.VideoCapture(gst,cv2.CAP_GSTREAMER)
while(cap.isOpened()):
  ret, frame = cap.read()
  if not ret:
    break
  cv2.imshow('frame', frame)
  if cv2.waitKey(1) & 0xFF == ord('q'):
    break

cv2.destroyAllWindows()
cap.release()

jenhaoyang avatar Feb 22 '24 08:02 jenhaoyang

They work. I've adapted them in a script of my own like so:

OPENCV_VER="master"
TMPDIR=$(mktemp -d)

# Build and install OpenCV from source.
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"
python3 -m pip wheel . --verbose

# Install OpenCV
python3 -m pip install opencv_python*.whl

This is on the latest 64-bit Raspberry Pi OS Lite (Bullseye) on RPi4, where I do not need the GUI components.

I didn't understand why it wouldn't install with GStreamer support and it was just because I was missing the dev dependencies:

sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

montmejat avatar Mar 04 '24 16:03 montmejat