프로그래밍/MediaPipe

MediaPipe Framework Concepts

Jay_rock 2022. 8. 16. 15:02

1. The Basics

1) Packet

기본 데이터 흐름 단위로 timestamp에서 각 입력 스트림을 따라 단일 packet이 전송된다. Packet에는 모든 종류의 데이터가 포함될 수 있다. 

자세히 알아보기

 

Packets

Cross-platform, customizable ML solutions for live and streaming media.

google.github.io

 

2) Graph

MediaPipe processing은 노드 간의 패킷 이동을 graph에서 결정한다. Graph에서 node의 유형 입력과 출력 그리고 데이터 흐름의 병합과 분할을 정의한다. 

자세히 알아보기

 

Graphs

Cross-platform, customizable ML solutions for live and streaming media.

google.github.io

 

3) Node

Node는 패킷을 생성 및 사용하고 그래프 작업이 수행되는 곳으로 "Calculator" 라고 부른다. 또한 각 node의 입출력을 Tag 및 Index로 식별하여 입출력의 포트 수를 정의한다.

 

- Calculator

각 Calculator는 그래프의 노드로 사용되며, "CalculatorBase" 클래스의 하위 클래스를 정의하여 사용한다. Calculator를 구현할 때 사용하는 방법 중엔 4가지가 있다.

 

Getcontract() : Calculator의 예상 입출력 유형을 지정하고, 프레임워크가 연결된 입출력이 패킷 유형이 일치한지 확인하기 위해 호출된다.

Open() : Calculator가 시작된 후에 호출되어 이 시점부터 입력 패킷을 사용할 수 있다. 또한 여기서 오류가 발생할 경우 그래프 실행이 종료된다.

Process() : 입력이 있는 경우 사용 가능할 패킷이 있을 때마다 반복적으로 호출된다. 또 입력이 동일한 타임스탬프를 갖도록 보장하고, 호출할 때마다 타임스탬프가 증가한다. 여기서도 오류가 발생할 경우 그래프가 종료된다. 패킷이 누락된 경우 빈 패킷을 생성한다.

Close() : 모든 호출이 완료된 후 프레임워크는 이 함수를 호출한다. 반대로 오류로 종료된 경우에도 이 함수를 호출하게 된다. 여기서도 Input Stream에 접근이 가능하여 Output을 쓸 수 있다. 이 함수가 반환된 후 Graph는 파괴된다.

// code snippets from CalculatorBase.h
class CalculatorBase {
 public:
  ...

  // The subclasses of CalculatorBase must implement GetContract.
  // ...
  static absl::Status GetContract(CalculatorContract* cc);

  // Open is called before any Process() calls, on a freshly constructed
  // calculator.  Subclasses may override this method to perform necessary
  // setup, and possibly output Packets and/or set output streams' headers.
  // ...
  virtual absl::Status Open(CalculatorContext* cc) {
    return absl::OkStatus();
  }

  // Processes the incoming inputs. May call the methods on cc to access
  // inputs and produce outputs.
  // ...
  virtual absl::Status Process(CalculatorContext* cc) = 0;

  // Is called if Open() was called and succeeded.  Is called either
  // immediately after processing is complete or after a graph run has ended
  // (if an error occurred in the graph).  ...
  virtual absl::Status Close(CalculatorContext* cc) {
    return absl::OkStatus();
  }

  ...
};

자세히 알아보기

 

Calculators

Cross-platform, customizable ML solutions for live and streaming media.

google.github.io

 

4) Streams

Streams는 두 노드에 타임스탬프가 증가함에 따라 패킷을 운반하는 connection을 의미한다.

 

5) Side packets

노드들 중 Side packets connection은 Single packet을 운반한다. Side packets은 constant한 일부 데이터를 제공하는 데 사용할 수 있는 반면 Streams는 시간에 따라 변경되는 데이터 흐름을 나타낸다.

MediaPipe Pose Tracking graph

 

 

2. Input and Output

Input은 Input Stream이 있거나 패킷을 생성하는 source nodes에서 시작될 수도 있다. (eg. 파일 불러오기) 마찬가지로 sink node가 있어 callbacks을 사용하여 그래프에서 출력을 수신할 수도 있다.

 

 

3. Runtime behavior

그래프는 각 스트림이 닫히거나 그래프가 취소될 때까지 진행된다. 또한 노드는 Open, Process, Close 세 가지 방법으로 프레임워크가 호출한다. 연속적인 Input은 타임스탬프 순서대로 처리되어, 앞서 수신된 Input이 처리될 때까지 일부 패킷의 처리가 지연될 수 있다. 또한 MediaPipe는 비디오나 오디오 스트림을 실시간으로 처리하도록 체계적으로 설계되어 실시간 추론에 유용하다.

'프로그래밍 > MediaPipe' 카테고리의 다른 글

MediaPipe란 무엇인가?  (0) 2022.07.19