Hello World! (C++)

  1. 确保您拥有有效的 MediaPipe 框架版本。请参阅安装说明

  2. 如需运行 hello world 示例,请执行以下操作:

    $ git clone https://github.com/google/mediapipe.git
    $ cd mediapipe
    
    $ export GLOG_logtostderr=1
    # Need bazel flag 'MEDIAPIPE_DISABLE_GPU=1' as desktop GPU is not supported currently.
    $ bazel run --define MEDIAPIPE_DISABLE_GPU=1 \
        mediapipe/examples/desktop/hello_world:hello_world
    
    # It should print 10 rows of Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    # Hello World!
    
  3. hello world 示例在 CalculatorGraphConfig proto 中定义的 PrintHelloWorld() 函数中使用一个简单的 MediaPipe 图。

    absl::Status PrintHelloWorld() {
      // Configures a simple graph, which concatenates 2 PassThroughCalculators.
      CalculatorGraphConfig config = ParseTextProtoOrDie<CalculatorGraphConfig>(R"(
        input_stream: "in"
        output_stream: "out"
        node {
          calculator: "PassThroughCalculator"
          input_stream: "in"
          output_stream: "out1"
        }
        node {
          calculator: "PassThroughCalculator"
          input_stream: "out1"
          output_stream: "out"
        }
      )");
    

    您可以通过将以下 CalculatorGraphConfig 内容粘贴到可视化工具中,使用 MediaPipe Visualizer 直观呈现此图。如需关于可视化工具的帮助,请点击此处

        input_stream: "in"
        output_stream: "out"
        node {
          calculator: "PassThroughCalculator"
          input_stream: "in"
          output_stream: "out1"
        }
        node {
          calculator: "PassThroughCalculator"
          input_stream: "out1"
          output_stream: "out"
        }
    

    该图由 1 个图表输入流 (in) 和 1 个图表输出流 (out) 以及 2 个串行连接的 PassThroughCalculator 组成。

    hello_world 图

  4. 在运行图之前,OutputStreamPoller 对象会连接到输出流以便稍后检索图输出,并且会使用 StartRun 启动图形运行。

    CalculatorGraph graph;
    MP_RETURN_IF_ERROR(graph.Initialize(config));
    MP_ASSIGN_OR_RETURN(OutputStreamPoller poller,
                        graph.AddOutputStreamPoller("out"));
    MP_RETURN_IF_ERROR(graph.StartRun({}));
    
  5. 然后,该示例使用 MakePacket 函数创建 10 个数据包(每个数据包包含一个时间戳值范围为 0、1、...9 的字符串),并通过 in 输入流将每个数据包添加到图表中,最后关闭该输入流以完成图表运行。

    for (int i = 0; i < 10; ++i) {
      MP_RETURN_IF_ERROR(graph.AddPacketToInputStream("in",
                         MakePacket<std::string>("Hello World!").At(Timestamp(i))));
    }
    MP_RETURN_IF_ERROR(graph.CloseInputStream("in"));
    
  6. 然后,此示例会通过 OutputStreamPoller 对象从输出流中检索全部 10 个数据包,从每个数据包中获取字符串内容并将其输出到输出日志。

    mediapipe::Packet packet;
    while (poller.Next(&packet)) {
      LOG(INFO) << packet.Get<string>();
    }