สวัสดีทุกคน! ใน 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 ใช้กราฟ MediaPipe อย่างง่ายในฟังก์ชัน PrintHelloWorld() ที่กำหนดไว้ในโปรโตคอล 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"
        }
      )");
    

    คุณแสดงภาพกราฟนี้ได้โดยใช้ MediaPipe Visualizer โดยวางเนื้อหา CalculatorGraphConfig ด้านล่างลงใน 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) และสตรีม PassThroughCalculator 2 รายการเชื่อมต่อกันตามลำดับ

    กราฟ 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. จากนั้นตัวอย่างจะสร้างแพ็กเก็ต 10 แพ็ก (แต่ละแพ็กเก็ตมีสตริง " Hello World!" ที่มีค่าการประทับเวลาตั้งแต่ 0, 1, ... 9) โดยใช้ฟังก์ชัน MakePacket เพิ่มแต่ละแพ็กเก็ตลงในกราฟผ่านสตรีมอินพุต 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>();
    }