user10525385
user10525385

Reputation:

Using gstreamer with Python OpenCV to capture live stream?

First of all I have Python 3 with the Gstreamer library in it.

print(cv2.getBuildInformation())

It shows Gstreamer with YES next to it.

Here is the transmitter code using gstreamer in RaspberryPi 3:

gst-launch-1.0 v4l2src device="/dev/video0" ! video/x-raw,width=320,height=240 ! videoconvert ! x264enc tune=zerolatency ! rtph264pay ! udpsink host='my ip address' port=10000

and I will be using Python code to determine shapes, recognize objects, etc..

Here is my Python code:

import numpy as np
import cv2




def receive():
cap = cv2.VideoCapture("udpsrc port=10000 ! application/x-rtp,encoding-name=H264 ! rtpjitterbuffer ! rtph264depay ! avdec_h264 ! videoconvert ! ximagesin ", 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 it always shows empty frame.

When I tried this command in the terminal:

gst-launch-1.0 udpsrc port=10000 ! application/x-rtp,encoding-name=H264 ! rtpjitterbuffer ! rtph264depay ! avdec_h264 ! videoconvert ! ximagesink

it works just fine, so the problem is on my Python end.

What do you recommend?

Upvotes: 1

Views: 7111

Answers (2)

Peter  Lunk
Peter Lunk

Reputation: 121

I use different send-receive scripts that give me way better video latency (allso at higher resolutions) when run from commandline, but I have no clue how to import the resulting stream into OpenCV on the Ubuntu end.

(My OpenCV is also installed with ffmpeg and gstreamer ON.)

My GStreamer transmitter code on my Raspberry pi:

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

My Gstreamer Receiver code at the Ubuntu 16.04 end:

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

I hope this will help you improve your stream quality and latency. And I hope somebody may be able to sample me the OpenCV pipeline import code :)

Greetings, Peter Lunk

Upvotes: 0

cap = cv2.VideoCapture('udpsrc port=7000 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)

use this pipeline, it will work just fine. change the port to the one you are using.

Upvotes: 1

Related Questions