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 함수를 사용하여 각 패킷에 0, 1, ... 9 사이의 타임스탬프 값을 가진 'Hello World!' 문자열을 포함한 패킷 10개를 만들고 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>();
    }