从gst管道总线读取解码的zbar元素消息

2024-09-28 17:17:21 发布

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

使用来自documentation的示例CLI(命令行界面)管道:

gst-launch-1.0 -m v4l2src ! videoconvert ! zbar ! videoconvert ! autovideosink

通过添加-m选项Output messages posted on the pipeline's bus我的程序可以像我预期的那样工作:向网络摄像头显示二维码,然后程序打印解码的二维数据。如果我向网络摄像头显示二维码,我会在终端中获得以下输出:

Got message #103 from element "zbar0" (element): barcode,
timestamp=(guint64)5069092054, stream-time=(guint64)5069092054,
running-time=(guint64)5069092054, type=(string)QR-Code,
symbol=(string)http://www.stackoverflow.com, quality=(int)1,
duration=(guint64)100000000;

如果我将此管道转换为Python代码:

import sys
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst, GLib

def bus_call(bus, message, loop):

    print("message:",message)
    print("message.src:",message.src)
    print("message.src.get_name():",message.src.get_name())

    t = message.type
    if t == Gst.MessageType.EOS:
        sys.stdout.write("End-of-stream\n")
        loop.quit()
    elif t == Gst.MessageType.WARNING:
        err, debug = message.parse_warning()
        sys.stderr.write("Warning: %s: %s\n" % (err, debug))
    elif t == Gst.MessageType.ERROR:
        err, debug = message.parse_error()
        sys.stderr.write("Error: %s: %s\n" % (err, debug))
        loop.quit()
    return True

def my_pipeline():

    Gst.init(None)

    # Create Pipeline
    pipeline = Gst.parse_launch("v4l2src device=/dev/video0 ! \
                                 videoconvert ! video/x-raw,width=640,height=480,framerate=30/1 ! \
                                 videoconvert ! zbar ! videoconvert ! autovideosink")

    # Create stream loop
    loop = GLib.MainLoop()

    # Setup pipeline bus
    bus = pipeline.get_bus()
    bus.add_signal_watch()
    bus.enable_sync_message_emission()
    bus.connect("message", bus_call, loop)

    print("Starting pipeline...\n")
    
    pipeline.set_state(Gst.State.PLAYING)
    try:
        loop.run()
    except:
        pass
    pipeline.set_state(Gst.State.NULL)

if __name__ == '__main__':
    my_pipeline()

然后,我对已识别二维码的输出变为:

message:                  <Gst.Message object at 0x7fe13a4d1880 (GstMessage at 0x11a75a0)>
message.src:              <__gi__.GstZBar object at 0x7fe13a4ebdc0 (GstZBar at 0x11888d0)>
message.src.get_name():   zbar0

无论我试图打印什么,似乎都不可能将解码后的信息打印出来。最好是我想要CLI中给出的完整输出结构。经过大量搜索,我了解到像-m这样的选项只允许在CLI界面中使用。我已经尝试为可用的类对象打印所有可用方法的内容,这些内容似乎与我的目标有着千丝万缕的联系。如何完成这项非常简单的任务


Tags: namedebugsrcloopmessagegetpipelinesys