簡體   English   中英

從 GStreamer 接收 Numpy 陣列實時

[英]Receive Numpy Array Realtime from GStreamer

我試圖從 GStreamer 框架中實時逐幀接收 numpy arrays。

我已經嘗試在 ZA7F5F35426B927411FC9231B56382173 中使用這樣的管道(來自http://stackoverflow.com/questions/8187257/play-audio-and-video-with-a-pipeline-in-gstreamer-python/8197837和修改)

self.filesrc = Gst.ElementFactory.make('filesrc')
self.filesrc.set_property('location', self.source_file)
self.pipeline.add(self.filesrc)

# Demuxer
self.decoder = Gst.ElementFactory.make('decodebin')
self.decoder.connect('pad-added', self.__on_decoded_pad)
self.pipeline.add(self.decoder)

# Video elements
self.videoqueue = Gst.ElementFactory.make('queue', 'videoqueue')
self.pipeline.add(self.videoqueue)

self.autovideoconvert = Gst.ElementFactory.make('autovideoconvert')
self.pipeline.add(self.autovideoconvert)

self.autovideosink = Gst.ElementFactory.make('autovideosink')
self.pipeline.add(self.autovideosink)

# Audio elements
self.audioqueue = Gst.ElementFactory.make('queue', 'audioqueue')
self.pipeline.add(self.audioqueue)

self.audioconvert = Gst.ElementFactory.make('audioconvert')
self.pipeline.add(self.audioconvert)

self.autoaudiosink = Gst.ElementFactory.make('autoaudiosink')
self.pipeline.add(self.autoaudiosink)

self.progressreport = Gst.ElementFactory.make('progressreport')
self.progressreport.set_property('update-freq', 1)
self.pipeline.add(self.progressreport)

所有管道也已經鏈接。 但是,我不知道如何從 stream 實時檢索 numpy 數組。 你有什么建議嗎?

原始問題中的管道旨在顯示視頻和播放音頻,因此它分別使用了autovideosinkautoaudiosink元素。 如果您希望您的視頻幀 go 到您的應用程序而不是屏幕,您需要使用不同的接收器元素,即appsink而不是autovideosink

self.appsink = Gst.ElementFactory.make('appsink')
self.pipeline.add(self.appsink)

如果您啟用了appsink 的“emit-signals”屬性, appsink元素有一個名為“new-sample”的信號,您可以連接到該信號,當新幀可用時觸發。

serf.appsink.set_property("emit-signals", True)
handler_id = self.appsink.connect("new-sample", self.__on_new_sample)

然后就是將 GStreamer 的緩沖區格式轉換為 Numpy 數組的問題。

def __on_new_sample(self, app_sink):
    sample = app_sink.pull_sample()
    caps = sample.get_caps()

    # Extract the width and height info from the sample's caps
    height = caps.get_structure(0).get_value("height")
    width = caps.get_structure(0).get_value("width")

    # Get the actual data
    buffer = sample.get_buffer()
    # Get read access to the buffer data
    success, map_info = buffer.map(Gst.MapFlags.READ)
    if not success:
        raise RuntimeError("Could not map buffer data!")

    numpy_frame = np.ndarray(
        shape=(height, width, 3),
        dtype=np.uint8,
        buffer=map_info.data)

    # Clean up the buffer mapping
    buffer.unmap(map_info)

請注意,此代碼對幀數據進行了某些假設,即它是一種類似於 RGB 的 3 色格式,並且顏色數據將是無符號的 8 位整數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM