从GStream实时接收Numpy数组

2024-10-02 00:20:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试从GStreamer框架实时接收帧到帧的numpy数组。在

我已经尝试在Python中使用类似这样的管道(来自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)

所有管道也已链接。但是,我不知道如何从流中实时执行numpy数组检索。你有什么建议吗?在


Tags: selfaddmakepipelinedecodergstelementfactoryfilesrc
1条回答
网友
1楼 · 发布于 2024-10-02 00:20:50

原始问题中的管道设计用于显示视频和播放音频,因此它分别使用autovideosink和{}元素。如果希望视频帧转到应用程序而不是屏幕,则需要使用另一个sink元素,即appsink而不是{}。在

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

appsink元素有一个名为“newsample”的信号,当有新的帧可用时,您可以连接到该信号。在

^{pr2}$

然后是将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位整数。在

相关问题 更多 >

    热门问题