Hallo Welt! in C++

  1. Sie benötigen eine funktionierende Version des MediaPipe-Frameworks. Siehe Installationsanleitung.

  2. So führen Sie das Beispiel hello world aus:

    $ 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. Im Beispiel hello world wird ein einfaches MediaPipe-Diagramm in der Funktion PrintHelloWorld() verwendet, das in einem CalculatorGraphConfig-Proto definiert ist.

    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"
        }
      )");
    

    Sie können dieses Diagramm mit MediaPipe Visualizer visualisieren, indem Sie den CalculatorGraphConfig-Inhalt unten in den Visualizer einfügen. Hilfe zum Visualizer finden Sie hier.

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

    Diese Grafik besteht aus 1 Grafikeingabestream (in) und 1 Grafikausgabestream (out) sowie zwei nacheinander verbundenen PassThroughCalculator.

    Grafik „hello_world“

  4. Bevor die Grafik ausgeführt wird, wird ein OutputStreamPoller-Objekt mit dem Ausgabestream verbunden, um später die Grafikausgabe abzurufen. Außerdem wird die Diagrammausführung mit StartRun gestartet.

    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. Im Beispiel werden dann mit der Funktion MakePacket 10 Pakete erstellt (jedes Paket enthält den String „Hello World!“ mit Zeitstempelwerten zwischen 0, 1, ... 9) und fügt jedes Paket über den Eingabestream in in die Grafik ein. Anschließend wird der Eingabestream geschlossen, um die Ausführung des Diagramms zu beenden.

    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. Über das Objekt OutputStreamPoller ruft das Beispiel dann alle zehn Pakete aus dem Ausgabestream ab, ruft den Stringinhalt aus jedem Paket ab und gibt ihn im Ausgabelog aus.

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