0

I want to process camera stream in opencv (that works fine), but then output the resulting processed frames over the network so I can see them as it's an embedded system without a monitor. I've spent days looking, but cannot work out how to push the frames to gstreamer in python. It looks like I should create an appsrc->udp pipeline in gstreamer (that's no problem), but I cannot find anywhere how to push the frames to appsrc in opencv using python. I've seen some examples in C++ but I've no idea how to translate them to python. It seems one can either copy buffers like this: Push images into Gstreamer pipeline Or this implies an easier way by opening an appsrc directly in opencv videowriter: How to write opencv mat to gstreamer pipeline?

Does anyone out there have a very simple example of how to output opencv to gstreamer in python?

Community
  • 1
  • 1
Dom
  • 31
  • 1
  • 6

1 Answers1

0

I am no python expert .. but what about this ..

Suppose you have this code for connecting the appsrc need-data signal (like here ):

source = gst.element_factory_make("appsrc", "source")
.. you need to set caps, then connect:
source.connect('need-data', self.needdata)

Then in the needdata function you need to get the data pointer of your opencv frame and create GstBuffer from it and emit it..

Suppose you have two variables self.data is the actual opencv data and self.size is the size of the data:

def needdata(self, src, length):
    buf = Gst.Buffer.new_allocate(self.size) 
    buf.fill(0, self.data)
    src.emit('push-buffer', buf)

Hm not sure whats in that length variable.. the pygst on gstreamer 1.0 seems to be quite undocumented.. or I cannot find the docs..

Community
  • 1
  • 1
nayana
  • 3,787
  • 3
  • 20
  • 51
  • Thanks for the reply and pointers. I've tried this but can't work out the correct caps, image formats or how to set self.data or self.size. The opencv documentation is so poor and there are no working examples in python that I think the only way to work this out is to start with c++ code and then try and translate that into python. There is some code for c++, so I'll start with that. I'll update here if I get anywhere. I can't believe that there is zero sample code to connect opencv to gstreamer in python! – Dom Jun 11 '16 at 07:04
  • you set the self.data with just assigning the allocated data array from opencv (or use some copy memcpy python style function - I am noob in python).. yes the python gstreamer docs are lacking quite .. but there is a outdated 0.10 gstreamer python examples [somewhere](https://cgit.freedesktop.org/gstreamer/gst-python/tree/old_examples?h=1.8) .. good idea with c++ - you can use also plain C as Gstreamer is in C .. gl – nayana Jun 13 '16 at 13:25
  • i there way to write data without signals? – eri Nov 02 '20 at 21:26