0

I am pretty new (only two days old) in the whole streaming and capture world. What I am trying to do is to capture a video stream from a UDP port using OpenCV and Python. I want my script to run inside a Docker container. I have created a docker container based on Fedora and installed Python3.6.5, OpenCV in it. The OpenCV has been compiled from source with the following options -

cmake -DBUILD_TIFF=ON \
    -DBUILD_WEBP=ON \
    -DBUILD_opencv_java=OFF \
    -DWITH_CUDA=OFF \
    -DENABLE_AVX=ON \
    -DWITH_OPENGL=ON \
    -DWITH_OPENCL=ON \
    -DWITH_IPP=ON \
    -DWITH_TBB=ON \
    -DWITH_GSTREAMER=ON \
    -DWITH_EIGEN=ON \
    -DWITH_V4L=ON \
    -DBUILD_TESTS=OFF \
    -DBUILD_PERF_TESTS=OFF \
    -DCMAKE_BUILD_TYPE=RELEASE \
    -DCMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)") \
    -DPYTHON_EXECUTABLE=$(which python) \
    -DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
    -DPYTHON_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
    -DWITH_FFMPEG=ON \
    -DWITH_GTK_2_X=ON \
    -DBUILD_PNG=ON \
    -DBUILD_JASPER=ON \
    -DBUILD_JPEG=ON \
    -DWITH_V4L=ON .. \

I have checked the UDP port using the following command -

tcpdump -i lo -n udp port <port_number>

It seems that it is receiving packets properly. No problem in that.

I have the following script inside my container to read frames from the udpsrc

import cv2

gst_string = "udpsrc port=<port_number> caps=\"application/x-rtp, media=(string)video, clock-rate=(int)90000\" ! rtpvp8depay ! vp8dec ! videoconvert ! appsink"

video_capture = cv2.VideoCapture(gst_string)

while video_capture.isOpened():
    ret, frame = video_capture.read()
    print("Frame received {}".format(ret))

And I am starting my docker container using the following command -

docker run -it -p <port_number>:<port_number>/udp myworker /bin/bash

And finally when from the bash prompt I am starting the script using python worker.py it is not going into the loop as the video_capture.isOpened() is returning False.

I have very little experience to all the things involved here (apart from Python and to some extent Docker) so I am unable to find out what I am doing wrong. Please help me.

Thanks in advance

EDIT ----------------

For further info, I had installed the following plugings to gstreamer -

yum install -y libnice-gstreamer1 \
    gstreamer-plugins-good \
    gstreamer1-plugins-ugly \
    gstreamer1-plugins-good \
    gstreamer-plugins-bad-free-extras \
    gstreamer-plugins-espeak gstreamer \
    gstreamer1 \
    gstreamer1-plugins-base-tools \
    gstreamer1-plugins-bad-free-extras \
    gstreamer1-plugins-bad-freeworld \
    gstreamer-plugins-bad-free \
    gstreamer1-plugins-base \
    gstreamer1-plugins-good-extras \
    gstreamer-plugins-base gstreamer-tools \
    PackageKit-gstreamer-plugin \
    gstreamer1-plugins-bad-free

EDIT -2 ---------------------------------

As mentioned in the comments, the OpenCV i was using did not have Gstreamer compiled inside it. So I was not able to open the Videocapture. So the yum command from above eventually becomes -

RUN yum install -y libnice-gstreamer1 \
    gstreamer1-plugins-ugly \
    gstreamer1-plugins-good \
    gstreamer1 \
    gstreamer1-plugins-base-devel \
    gstreamer-plugins-base-tools \
    gstreamer1-plugins-bad-free-extras \
    gstreamer1-plugins-bad-freeworld \
    gstreamer1-plugins-base \
    gstreamer1-plugins-good-extras \
    gstreamer1-plugins-base \
    gstreamer1-plugins-bad-free \
    gstreamer-plugins-base-devel

Et voila! This did the trick.

SRC
  • 2,123
  • 3
  • 31
  • 44
  • 1
    Make sure 1) you've a working Gstreamer command by running it in terminal first. 2) your OpenCV has Gstreamer support by searching 'Gstreamer' in the output of `cv2.getBuildInformation()`. 3) the Gstreamer plugins in your command is installed. Check by running `gst-inspect-1.0 plugin_name` in terminal. 4) check [this](https://stackoverflow.com/a/46636126/2286337) and [this](https://stackoverflow.com/a/47045135/2286337). – zindarod Jun 28 '18 at 11:03
  • Try with `docker run -it --net host myworker /bin/bash` and see if it works? – Tarun Lalwani Jun 28 '18 at 11:22
  • Thanks for the directions. I found out that I was compiling OpenCV without gstreamer. I managed to build it with that support. `cv2.getBuildInformation()`Gives me base, video and app as yes. So I believe it is compiled properly. However, the problem now is that when I try to open the capture using `video_capture = cv2.VideoCapture(gst_string)`it hangs there and does not go further in the code (thus not entering in the loop). Again, I am not sure what is wrong. Any ideas? – SRC Jun 28 '18 at 12:44
  • Also, for reference when I use Ctrl-C to stop the hanging script it prints out three error messages (core dupmed) they are identical apart from the reference of the element in them. They look this `(python3.6:279): GStreamer-CRITICAL **: Trying to dispose element vp8dec0, but it is in PLAYING instead of the NULL state. You need to explicitly set elements to the NULL state before dropping the final reference, to allow them to clean up. This problem may also be caused by a refcounting bug in the application or some element.` same error for rtpvp8depay0 and udpsrc0 and core dump after – SRC Jun 28 '18 at 13:02
  • Actually, It is working. I just had to restart the transmission! Thanks a lot for your help :) – SRC Jun 28 '18 at 13:25

0 Answers0