opencv / opencv-python

Automated CI toolchain to produce precompiled opencv-python, opencv-python-headless, opencv-contrib-python and opencv-contrib-python-headless packages.
https://pypi.org/project/opencv-python/
MIT License
4.34k stars 819 forks source link

How to install with GStreamer support #530

Open kwiley-s3global opened 2 years ago

kwiley-s3global commented 2 years ago

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.

ES-Alexander commented 2 years ago

@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 :-)

achton commented 2 years ago

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.

bluebrown commented 10 months ago

@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.

achton commented 10 months ago

@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.

Nacriema commented 10 months ago

Thanks man !

HunterShinobiTitan commented 9 months ago

do you have new guide for windows?

jenhaoyang commented 5 months ago

Here is the minimal steps. Here is my enviornment

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()

montmejat commented 4 months ago

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