Hello World! en C++

  1. Assurez-vous de disposer d'une version fonctionnelle du framework MediaPipe. Consultez les instructions d'installation.

  2. Pour exécuter l'exemple hello world, procédez comme suit:

    $ 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. L'exemple hello world utilise un graphique MediaPipe simple dans la fonction PrintHelloWorld(), défini dans un fichier proto CalculatorGraphConfig.

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

    Vous pouvez visualiser ce graphique à l'aide du visualiseur MediaPipe en collant le contenu CalculatorGraphConfig ci-dessous dans le visualiseur. Cliquez ici pour obtenir de l'aide sur le visualiseur.

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

    Ce graphe se compose d'un flux d'entrée de graphe (in) et d'un flux de sortie de graphe (out), et de deux PassThroughCalculator connectés en série.

    Graphique hello_world

  4. Avant d'exécuter le graphe, un objet OutputStreamPoller est connecté au flux de sortie afin de récupérer ultérieurement la sortie du graphe, et l'exécution du graphe est lancée avec 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. L'exemple crée ensuite 10 paquets (chacun contenant une chaîne "Hello World!" avec des valeurs d'horodatage allant de 0, 1, ... 9) à l'aide de la fonction MakePacket, ajoute chaque paquet dans le graphe via le flux d'entrée in, puis ferme le flux d'entrée pour terminer l'exécution du graphe.

    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. Via l'objet OutputStreamPoller, l'exemple récupère ensuite les 10 paquets du flux de sortie, récupère le contenu de la chaîne de chaque paquet et l'imprime dans le journal de sortie.

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