简体   繁体   English

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

[英]reading decoded zbar element messages from gst pipeline bus

Using the example CLI (command-line-interface) pipeline from the documentation :使用文档中的示例 CLI(命令行界面)管道:

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

with the option -m which adds Output messages posted on the pipeline's bus my program works as I would expect: Present QR code to webcam, then program prints the decoded QR data.使用选项-m添加Output messages posted on the pipeline's bus我的程序按预期工作:向网络摄像头显示 QR 码,然后程序打印解码的 QR 数据。 If I present a QR code to my webcam I get the following output in a terminal:如果我向网络摄像头出示 QR 码,我会在终端中看到以下 output:

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;

If I take this pipeline and turn into Python code:如果我把这条管道变成 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()

then my output for an identified QR code becomes:然后我的 output 用于识别的 QR 码变为:

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

No matter what I try to print, it seems impossible to get the decoded message out.无论我尝试打印什么,似乎都无法将解码后的消息输出。 Preferably I'd like the full output structure as given from the CLI.最好我想要 CLI 给出的完整的 output 结构。 After a lot of search, I learned that options like -m are only allowed in the CLI interface.经过大量搜索,我了解到像-m这样的选项只允许在 CLI 界面中使用。 I have tried printing the content of all available methods for the available class objects that seemed even remotely relevant to my goal.我已经尝试为可用的 class 对象打印所有可用方法的内容,这些对象似乎与我的目标完全相关。 How can this very simple task be done?如何完成这个非常简单的任务?

After not finding anything, anywhere online, I decided to read the manual again to get a better understanding of the mechanisms behind.在网上找不到任何东西后,我决定再次阅读手册以更好地了解背后的机制。 The answer was quite simple: message.get_structure().to_string()答案很简单: message.get_structure().to_string()

Also we can probe when this is triggered by seeing when t == Gst.MessageType.ELEMENT我们也可以通过查看t == Gst.MessageType.ELEMENT来探测何时触发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM