Ask Your Question
0

How to use Gstreamer pipeline in OpenCV ?

asked 2018-10-29 09:09:32 -0600

Hello all :)

I have OpenCV installed with ffmpeg and gstreamer support.

I have a working Gstreamer pipeline from my raspberry pi 3b to Ubuntu 16.04.

This is my Gstreamer pipeline SEND script line:

gst-launch-1.0 -v v4l2src ! video/x-raw,width=320,height=240 ! videoconvert ! jpegenc ! rtpjpegpay ! udpsink host=192.168.1.101 port=5200

This is my Gstreamer pipeline RECEIVER script line:

gst-launch-1.0 -v udpsrc port=5200 ! application/x-rtp, encoding-name=JPEG,payload=26 ! rtpjpegdepay ! jpegdec ! videoconvert ! autovideosink

Can anyone help me how to use this videostream in Python OpenCV ?

Any help much appreciated ! Greetings, Peter Lunk

edit retag flag offensive close merge delete

Comments

please check first, if your cv2 install has gstreamer capabilities by:

print(cv2.getBuildInformation())

then look at the "Video IO" section there.

(most prebuilt versions from a ppm, like pip or conda won't have any support for this)

berak gravatar imageberak ( 2018-10-29 09:13:58 -0600 )edit
1

As I stated in my Opening-Post: "I have OpenCV installed with ffmpeg and gstreamer support." Everything built from source with all the appropriate libs.

MrLunk gravatar imageMrLunk ( 2018-10-29 12:17:39 -0600 )edit

apologies, just a knee-jerk reflex ;)

berak gravatar imageberak ( 2018-10-29 12:19:51 -0600 )edit

That happens, thanks for the well meant effort though ;) Might you have any tips for me on how to brew the python code to use this video pipeline in openCV ?

MrLunk gravatar imageMrLunk ( 2018-10-29 14:50:43 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-10-30 06:47:30 -0600

supra56 gravatar image

updated 2018-10-30 07:17:55 -0600

import numpy as np import cv2 from multiprocessing import Process

def send():
    cap_send = cv2.VideoCapture('videotestsrc ! video/x-raw,framerate=20/1 ! videoscale ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
    out_send = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000',cv2.CAP_GSTREAMER,0, 20, (320,240), True)

    if not cap_send.isOpened() or not out_send.isOpened():
        print('VideoCapture or VideoWriter not opened')
        exit(0)

    while True:
        ret,frame = cap_send.read()

        if not ret:
            print('empty frame')
            break

        out_send.write(frame)

        cv2.imshow('send', frame)
        if cv2.waitKey(1)&0xFF == ord('q'):
            break

    cap_send.release()
    out_send.release()

def receive():
    cap_receive = cv2.VideoCapture('udpsrc port=5000 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! appsink', cv2.CAP_GSTREAMER)

    if not cap_receive.isOpened():
        print('VideoCapture not opened')
        exit(0)

    while True:
        ret,frame = cap_receive.read()

        if not ret:
            print('empty frame')
            break

        cv2.imshow('receive', frame)
        if cv2.waitKey(1)&0xFF == ord('q'):
            break

    #cap_receive.release()

if __name__ == '__main__':
    s = Process(target=send)
    r = Process(target=receive)
    s.start()
    r.start()
    s.join()
    r.join()

    cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

Thanks or your elaborate reply, I will be testing this as soon as I get home :)

MrLunk gravatar imageMrLunk ( 2018-10-30 07:08:42 -0600 )edit

Tried your code but it gave me the following errors:

(python:2761): GStreamer-CRITICAL **: gst_element_get_static_pad: assertion 'GST_IS_ELEMENT (element)' failed

(python:2761): GStreamer-CRITICAL **: gst_pad_get_current_caps: assertion 'GST_IS_PAD (pad)' failed

(python:2761): GStreamer-CRITICAL **: gst_caps_get_structure: assertion 'GST_IS_CAPS (caps)' failed

(python:2761): GStreamer-CRITICAL **: gst_structure_get_int: assertion 'structure != NULL' failed

(python:2761): GStreamer-CRITICAL **: gst_structure_get_int: assertion 'structure != NULL' failed

(python:2761): GStreamer-CRITICAL **: gst_structure_get_fraction: assertion 'structure != NULL' failed Process Process-1: Traceback (most recent call last): File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _boo

MrLunk gravatar imageMrLunk ( 2018-10-30 08:02:03 -0600 )edit

I really only need a working receiver end OpenCV import in python for the receiving Ubuntu 16.04 machine. The send pipeline comes from commandline on the Raspberry pi.

MrLunk gravatar imageMrLunk ( 2018-10-30 08:03:29 -0600 )edit

If I make a receive only script from that i come up with this wich I think should work:

import numpy as np
import cv2

def receive():
cap = cv2.VideoCapture('udpsrc port=5200 caps=application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H264,payload=(int)96!rtph264depay!decodebin!videoconvert!appsink',cv2.CAP_GSTREAMER)


while True:
    ret,frame = cap.read()
    if not ret:
        print('empty frame')
        continue

    cv2.imshow('receive', frame)
    if cv2.waitKey(1)&0xFF == ord('q'):
        break

cap.release()
receive();

But I get a strange 'indentation' error wich I really don't understand ?

IndentationError: expected an indented block

MrLunk gravatar imageMrLunk ( 2018-10-30 08:30:04 -0600 )edit

@MrLunk. I noticed that you are using Unbuntu. Fortunately, I'm using Debian Stretch

supra56 gravatar imagesupra56 ( 2018-10-30 11:11:28 -0600 )edit

Which lines for indentation?

supra56 gravatar imagesupra56 ( 2018-10-30 11:12:13 -0600 )edit

The error indicator points at '!rtph264depay' in the pipeline code. ??? weird huh.

MrLunk gravatar imageMrLunk ( 2018-10-30 11:13:22 -0600 )edit

Can you use tab to move indentation?

supra56 gravatar imagesupra56 ( 2018-10-30 11:29:01 -0600 )edit

import numpy as np import cv2

def receive():
    cap = cv2.VideoCapture('udpsrc port=5200 caps=application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H264,payload=(int)96!rtph264depay!decodebin!videoconvert!appsink',cv2.CAP_GSTREAMER)

while True:
    ret,frame = cap.read()
    if not ret:
        print('empty frame')
        continue    
    cv2.imshow('receive', frame)
    if cv2.waitKey(1)&0xFF == ord('q'):
        break    
cap.release()
cap.receive()
supra56 gravatar imagesupra56 ( 2018-10-30 11:35:17 -0600 )edit

Btw, I am using python 3.5 and OpenCV 4.0

supra56 gravatar imagesupra56 ( 2018-10-30 11:40:54 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-10-29 09:08:54 -0600

Seen: 69,586 times

Last updated: Oct 30 '18