From 51dbd9779c7f7bfea42c062656ed583caaf1fda2 Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Mon, 7 Nov 2022 14:16:29 -0800 Subject: [PATCH] Introduce Stream/SidePacket aliases for Source/SideSource PiperOrigin-RevId: 486761126 --- mediapipe/framework/api2/builder.h | 55 ++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/mediapipe/framework/api2/builder.h b/mediapipe/framework/api2/builder.h index 7dce211c8..bf7f2b399 100644 --- a/mediapipe/framework/api2/builder.h +++ b/mediapipe/framework/api2/builder.h @@ -1,8 +1,10 @@ #ifndef MEDIAPIPE_FRAMEWORK_API2_BUILDER_H_ #define MEDIAPIPE_FRAMEWORK_API2_BUILDER_H_ +#include #include #include +#include #include "absl/container/btree_map.h" #include "absl/strings/string_view.h" @@ -74,6 +76,7 @@ class TagIndexMap { class Graph; class NodeBase; +class PacketGenerator; // These structs are used internally to store information about the endpoints // of a connection. @@ -146,6 +149,7 @@ template class SourceImpl { public: using Base = SourceBase; + using PayloadT = T; // Src is used as the return type of fluent methods below. Since these are // single-port methods, it is desirable to always decay to a reference to the @@ -201,10 +205,61 @@ class SourceImpl { // when building the graph. template using Source = SourceImpl; + +// Represents a stream of packets of a particular type. +// +// The intended use: +// - decouple input/output streams from graph/node during graph construction +// - pass streams around and connect them as needed, extracting reusable parts +// to utility/convenience functions or classes. +// +// For example: +// Stream Resize(Stream image, const Size& size, Graph& graph) { +// auto& scaler_node = graph.AddNode("GlScalerCalculator"); +// auto& opts = scaler_node.GetOptions(); +// opts.set_output_width(size.width); +// opts.set_output_height(size.height); +// a >> scaler_node.In("IMAGE"); +// return scaler_node.Out("IMAGE").Cast(); +// } +// +// Where graph can use it as: +// Graph graph; +// Stream input_image = graph.In("INPUT_IMAGE").Cast(); +// Stream resized_image = Resize(input_image, {64, 64}, graph); +template +using Stream = Source; + template using MultiSource = MultiPort>; + template using SideSource = SourceImpl; + +// Represents a side packet of a particular type. +// +// The intended use: +// - decouple input/output side packets from graph/node during graph +// construction +// - pass side packets around and connect them as needed, extracting reusable +// parts utility/convenience functions or classes. +// +// For example: +// SidePacket GetModel(SidePacket model_blob, +// Graph& graph) { +// auto& model_node = graph.AddNode("TfLiteModelCalculator"); +// model_blob >> model_node.SideIn("MODEL_BLOB"); +// return model_node.SideOut("MODEL").Cast(); +// } +// +// Where graph can use it as: +// Graph graph; +// SidePacket model_blob = +// graph.SideIn("MODEL_BLOB").Cast(); +// SidePacket model = GetModel(model_blob, graph); +template +using SidePacket = SideSource; + template using MultiSideSource = MultiPort>;