Hello World! trong C++

  1. Đảm bảo bạn có phiên bản MediaPipe Framework đang hoạt động. Hãy xem hướng dẫn cài đặt.

  2. Cách chạy ví dụ về 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. Ví dụ về hello world sử dụng một biểu đồ MediaPipe đơn giản trong hàm PrintHelloWorld(), được xác định trong một 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"
        }
      )");
    

    Bạn có thể trực quan hoá biểu đồ này bằng MediaPipe Visualizer bằng cách dán nội dung CalculatorGraphConfig bên dưới vào trình hiển thị. Hãy xem tại đây để được trợ giúp về trình trực quan hoá.

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

    Biểu đồ này bao gồm 1 luồng đầu vào biểu đồ (in) và 1 luồng đầu ra biểu đồ (out) và 2 PassThroughCalculator được kết nối nối tiếp.

    biểu đồ hello_world

  4. Trước khi chạy biểu đồ, đối tượng OutputStreamPoller được kết nối với luồng đầu ra để truy xuất đầu ra biểu đồ sau này, và quá trình chạy biểu đồ sẽ bắt đầu bằng 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. Sau đó, ví dụ sẽ tạo 10 gói (mỗi gói chứa một chuỗi "Hello World!" với giá trị Dấu thời gian nằm trong khoảng từ 0, 1, ... 9) bằng cách sử dụng hàm MakePacket, thêm từng gói vào biểu đồ thông qua luồng đầu vào in và cuối cùng đóng luồng đầu vào để hoàn tất quá trình chạy biểu đồ.

    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. Thông qua đối tượng OutputStreamPoller, ví dụ này sẽ truy xuất tất cả 10 gói từ luồng đầu ra, lấy nội dung chuỗi trong mỗi gói và in vào nhật ký đầu ra.

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