Project import generated by Copybara.
GitOrigin-RevId: 08c2016a4df5aef571b464a4d4491f38c6b2af10
This commit is contained in:
@@ -225,7 +225,7 @@ cc_library(
|
||||
"//mediapipe/framework:stream_handler_cc_proto",
|
||||
"//mediapipe/framework/port:any_proto",
|
||||
"//mediapipe/framework/port:status",
|
||||
"//mediapipe/framework/tool:options_util",
|
||||
"//mediapipe/framework/tool:options_map",
|
||||
"//mediapipe/framework/tool:tag_map",
|
||||
"@com_google_absl//absl/memory",
|
||||
],
|
||||
@@ -473,7 +473,7 @@ cc_library(
|
||||
"//mediapipe/framework:calculator_cc_proto",
|
||||
"//mediapipe/framework/port:any_proto",
|
||||
"//mediapipe/framework/port:logging",
|
||||
"//mediapipe/framework/tool:options_util",
|
||||
"//mediapipe/framework/tool:options_map",
|
||||
"@com_google_absl//absl/base:core_headers",
|
||||
"@com_google_absl//absl/strings",
|
||||
],
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Experimental new APIs
|
||||
# New MediaPipe APIs
|
||||
|
||||
This directory defines new APIs for MediaPipe:
|
||||
|
||||
@@ -6,13 +6,12 @@ This directory defines new APIs for MediaPipe:
|
||||
- Builder API, for assembling CalculatorGraphConfigs with C++, as an alternative
|
||||
to using the proto API directly.
|
||||
|
||||
The code is working, and the new APIs interoperate fully with the existing
|
||||
framework code. They are considered a work in progress, but are being released
|
||||
now so we can begin adopting them in our calculators.
|
||||
The new APIs interoperate fully with the existing framework code, and we are
|
||||
adopting them in our calculators. We are still making improvements, and the
|
||||
placement of this code under the `mediapipe::api2` namespace is not final.
|
||||
|
||||
Developers are welcome to try out these APIs as early adopters, but should
|
||||
expect breaking changes. The placement of this code under the `mediapipe::api2`
|
||||
namespace is not final.
|
||||
Developers are welcome to try out these APIs as early adopters, but there may be
|
||||
breaking changes.
|
||||
|
||||
## Node API
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
#include "mediapipe/framework/port.h"
|
||||
#include "mediapipe/framework/port/any_proto.h"
|
||||
#include "mediapipe/framework/status_handler.pb.h"
|
||||
#include "mediapipe/framework/tool/options_util.h"
|
||||
#include "mediapipe/framework/tool/options_map.h"
|
||||
|
||||
namespace mediapipe {
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
#include "mediapipe/framework/packet_set.h"
|
||||
#include "mediapipe/framework/port.h"
|
||||
#include "mediapipe/framework/port/any_proto.h"
|
||||
#include "mediapipe/framework/tool/options_util.h"
|
||||
#include "mediapipe/framework/tool/options_map.h"
|
||||
|
||||
namespace mediapipe {
|
||||
|
||||
|
||||
@@ -154,12 +154,25 @@ cc_test(
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "options_map",
|
||||
hdrs = ["options_map.h"],
|
||||
visibility = ["//mediapipe/framework:mediapipe_internal"],
|
||||
deps = [
|
||||
"//mediapipe/framework:calculator_cc_proto",
|
||||
"//mediapipe/framework/port:any_proto",
|
||||
"//mediapipe/framework/port:status",
|
||||
"//mediapipe/framework/tool:type_util",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "options_util",
|
||||
srcs = ["options_util.cc"],
|
||||
hdrs = ["options_util.h"],
|
||||
visibility = ["//mediapipe/framework:mediapipe_internal"],
|
||||
deps = [
|
||||
":options_map",
|
||||
":proto_util_lite",
|
||||
"//mediapipe/framework:calculator_cc_proto",
|
||||
"//mediapipe/framework:collection",
|
||||
@@ -199,17 +212,6 @@ mediapipe_cc_test(
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "packet_util",
|
||||
hdrs = ["packet_util.h"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//mediapipe/framework:packet",
|
||||
"//mediapipe/framework/port:statusor",
|
||||
"@org_tensorflow//tensorflow/core:protos_all_cc",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "proto_util_lite",
|
||||
srcs = ["proto_util_lite.cc"],
|
||||
@@ -681,6 +683,7 @@ cc_library(
|
||||
"//mediapipe/framework/port:logging",
|
||||
"//mediapipe/framework/port:ret_check",
|
||||
"//mediapipe/framework/port:status",
|
||||
"//mediapipe/framework/stream_handler:immediate_input_stream_handler",
|
||||
"//mediapipe/framework/tool:switch_container_cc_proto",
|
||||
"@com_google_absl//absl/strings",
|
||||
],
|
||||
@@ -706,6 +709,7 @@ cc_library(
|
||||
"//mediapipe/framework/port:logging",
|
||||
"//mediapipe/framework/port:ret_check",
|
||||
"//mediapipe/framework/port:status",
|
||||
"//mediapipe/framework/stream_handler:immediate_input_stream_handler",
|
||||
"//mediapipe/framework/tool:switch_container_cc_proto",
|
||||
"@com_google_absl//absl/strings",
|
||||
],
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
#ifndef MEDIAPIPE_FRAMEWORK_TOOL_OPTIONS_MAP_H_
|
||||
#define MEDIAPIPE_FRAMEWORK_TOOL_OPTIONS_MAP_H_
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
#include "mediapipe/framework/calculator.pb.h"
|
||||
#include "mediapipe/framework/port/any_proto.h"
|
||||
#include "mediapipe/framework/port/status.h"
|
||||
#include "mediapipe/framework/tool/type_util.h"
|
||||
|
||||
namespace mediapipe {
|
||||
|
||||
namespace tool {
|
||||
|
||||
// A compile-time detector for the constant |T::ext|.
|
||||
template <typename T>
|
||||
struct IsExtension {
|
||||
private:
|
||||
template <typename U>
|
||||
static char test(decltype(&U::ext));
|
||||
|
||||
template <typename>
|
||||
static int test(...);
|
||||
|
||||
public:
|
||||
static constexpr bool value = (sizeof(test<T>(0)) == sizeof(char));
|
||||
};
|
||||
|
||||
template <class T,
|
||||
typename std::enable_if<IsExtension<T>::value, int>::type = 0>
|
||||
void GetExtension(const CalculatorOptions& options, T* result) {
|
||||
if (options.HasExtension(T::ext)) {
|
||||
*result = options.GetExtension(T::ext);
|
||||
}
|
||||
}
|
||||
|
||||
template <class T,
|
||||
typename std::enable_if<!IsExtension<T>::value, int>::type = 0>
|
||||
void GetExtension(const CalculatorOptions& options, T* result) {}
|
||||
|
||||
template <class T>
|
||||
void GetNodeOptions(const CalculatorGraphConfig::Node& node_config, T* result) {
|
||||
#if defined(MEDIAPIPE_PROTO_LITE) && defined(MEDIAPIPE_PROTO_THIRD_PARTY)
|
||||
// protobuf::Any is unavailable with third_party/protobuf:protobuf-lite.
|
||||
#else
|
||||
for (const mediapipe::protobuf::Any& options : node_config.node_options()) {
|
||||
if (options.Is<T>()) {
|
||||
options.UnpackTo(result);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// A map from object type to object.
|
||||
class TypeMap {
|
||||
public:
|
||||
template <class T>
|
||||
bool Has() const {
|
||||
return content_.count(TypeId<T>()) > 0;
|
||||
}
|
||||
template <class T>
|
||||
T* Get() const {
|
||||
if (!Has<T>()) {
|
||||
content_[TypeId<T>()] = std::make_shared<T>();
|
||||
}
|
||||
return static_cast<T*>(content_[TypeId<T>()].get());
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::map<TypeIndex, std::shared_ptr<void>> content_;
|
||||
};
|
||||
|
||||
// Extracts the options message of a specified type from a
|
||||
// CalculatorGraphConfig::Node.
|
||||
class OptionsMap {
|
||||
public:
|
||||
OptionsMap& Initialize(const CalculatorGraphConfig::Node& node_config) {
|
||||
node_config_ = &node_config;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Returns the options data for a CalculatorGraphConfig::Node, from
|
||||
// either "options" or "node_options" using either GetExtension or UnpackTo.
|
||||
template <class T>
|
||||
const T& Get() const {
|
||||
if (options_.Has<T>()) {
|
||||
return *options_.Get<T>();
|
||||
}
|
||||
T* result = options_.Get<T>();
|
||||
if (node_config_->has_options()) {
|
||||
GetExtension(node_config_->options(), result);
|
||||
} else {
|
||||
GetNodeOptions(*node_config_, result);
|
||||
}
|
||||
return *result;
|
||||
}
|
||||
|
||||
const CalculatorGraphConfig::Node* node_config_;
|
||||
TypeMap options_;
|
||||
};
|
||||
|
||||
} // namespace tool
|
||||
} // namespace mediapipe
|
||||
|
||||
#endif // MEDIAPIPE_FRAMEWORK_TOOL_OPTIONS_MAP_H_
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "mediapipe/framework/packet.h"
|
||||
#include "mediapipe/framework/packet_set.h"
|
||||
#include "mediapipe/framework/port/any_proto.h"
|
||||
#include "mediapipe/framework/tool/options_map.h"
|
||||
#include "mediapipe/framework/tool/type_util.h"
|
||||
|
||||
namespace mediapipe {
|
||||
@@ -34,64 +35,6 @@ inline T MergeOptions(const T& base, const T& options) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// A compile-time detector for the constant |T::ext|.
|
||||
template <typename T>
|
||||
struct IsExtension {
|
||||
private:
|
||||
template <typename U>
|
||||
static char test(decltype(&U::ext));
|
||||
|
||||
template <typename>
|
||||
static int test(...);
|
||||
|
||||
public:
|
||||
static constexpr bool value = (sizeof(test<T>(0)) == sizeof(char));
|
||||
};
|
||||
|
||||
// A map from object type to object.
|
||||
class TypeMap {
|
||||
public:
|
||||
template <class T>
|
||||
bool Has() const {
|
||||
return content_.count(TypeId<T>()) > 0;
|
||||
}
|
||||
template <class T>
|
||||
T* Get() const {
|
||||
if (!Has<T>()) {
|
||||
content_[TypeId<T>()] = std::make_shared<T>();
|
||||
}
|
||||
return static_cast<T*>(content_[TypeId<T>()].get());
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::map<TypeIndex, std::shared_ptr<void>> content_;
|
||||
};
|
||||
|
||||
template <class T,
|
||||
typename std::enable_if<IsExtension<T>::value, int>::type = 0>
|
||||
void GetExtension(const CalculatorOptions& options, T* result) {
|
||||
if (options.HasExtension(T::ext)) {
|
||||
*result = options.GetExtension(T::ext);
|
||||
}
|
||||
}
|
||||
|
||||
template <class T,
|
||||
typename std::enable_if<!IsExtension<T>::value, int>::type = 0>
|
||||
void GetExtension(const CalculatorOptions& options, T* result) {}
|
||||
|
||||
template <class T>
|
||||
void GetNodeOptions(const CalculatorGraphConfig::Node& node_config, T* result) {
|
||||
#if defined(MEDIAPIPE_PROTO_LITE) && defined(MEDIAPIPE_PROTO_THIRD_PARTY)
|
||||
// protobuf::Any is unavailable with third_party/protobuf:protobuf-lite.
|
||||
#else
|
||||
for (const mediapipe::protobuf::Any& options : node_config.node_options()) {
|
||||
if (options.Is<T>()) {
|
||||
options.UnpackTo(result);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Combine a base options message with an optional side packet. The specified
|
||||
// packet can hold either the specified options type T or CalculatorOptions.
|
||||
// Fields are either replaced or merged depending on field merge_fields.
|
||||
@@ -132,35 +75,6 @@ inline T RetrieveOptions(const T& base, const InputStreamShardSet& stream_set,
|
||||
return base;
|
||||
}
|
||||
|
||||
// Extracts the options message of a specified type from a
|
||||
// CalculatorGraphConfig::Node.
|
||||
class OptionsMap {
|
||||
public:
|
||||
OptionsMap& Initialize(const CalculatorGraphConfig::Node& node_config) {
|
||||
node_config_ = &node_config;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Returns the options data for a CalculatorGraphConfig::Node, from
|
||||
// either "options" or "node_options" using either GetExtension or UnpackTo.
|
||||
template <class T>
|
||||
const T& Get() const {
|
||||
if (options_.Has<T>()) {
|
||||
return *options_.Get<T>();
|
||||
}
|
||||
T* result = options_.Get<T>();
|
||||
if (node_config_->has_options()) {
|
||||
GetExtension(node_config_->options(), result);
|
||||
} else {
|
||||
GetNodeOptions(*node_config_, result);
|
||||
}
|
||||
return *result;
|
||||
}
|
||||
|
||||
const CalculatorGraphConfig::Node* node_config_;
|
||||
TypeMap options_;
|
||||
};
|
||||
|
||||
// Finds the descriptor for a protobuf.
|
||||
const proto_ns::Descriptor* GetProtobufDescriptor(const std::string& type_name);
|
||||
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
// Copyright 2019 The MediaPipe Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef MEDIAPIPE_FRAMEWORK_TOOL_PACKET_UTIL_H_
|
||||
#define MEDIAPIPE_FRAMEWORK_TOOL_PACKET_UTIL_H_
|
||||
|
||||
#include "mediapipe/framework/packet.h"
|
||||
#include "tensorflow/core/example/example.pb.h"
|
||||
|
||||
namespace mediapipe {
|
||||
namespace tool {
|
||||
// The CLIF-friendly util functions to create and access a typed MediaPipe
|
||||
// Packet from MediaPipe Python interface.
|
||||
|
||||
// Functions for SequenceExample Packets.
|
||||
|
||||
// Make a SequenceExample packet from a serialized SequenceExample.
|
||||
// The SequenceExample in the Packet is owned by the C++ packet.
|
||||
Packet CreateSequenceExamplePacketFromString(std::string* serialized_content) {
|
||||
tensorflow::SequenceExample sequence_example;
|
||||
sequence_example.ParseFromString(*serialized_content);
|
||||
return MakePacket<tensorflow::SequenceExample>(sequence_example);
|
||||
}
|
||||
|
||||
// Get a serialized SequenceExample std::string from a Packet.
|
||||
// The ownership of the returned std::string will be transferred to the Python
|
||||
// object.
|
||||
std::unique_ptr<std::string> GetSerializedSequenceExample(Packet* packet) {
|
||||
return absl::make_unique<std::string>(
|
||||
packet->Get<tensorflow::SequenceExample>().SerializeAsString());
|
||||
}
|
||||
|
||||
// Make a String packet
|
||||
Packet CreateStringPacket(std::string* input_string) {
|
||||
return MakePacket<std::string>(*input_string);
|
||||
}
|
||||
|
||||
// Get the std::string from a Packet<std::string>
|
||||
std::unique_ptr<std::string> GetString(Packet* packet) {
|
||||
return absl::make_unique<std::string>(packet->Get<std::string>());
|
||||
}
|
||||
|
||||
} // namespace tool
|
||||
} // namespace mediapipe
|
||||
|
||||
#endif // MEDIAPIPE_FRAMEWORK_TOOL_PACKET_UTIL_H_
|
||||
@@ -16,8 +16,6 @@
|
||||
#define MEDIAPIPE_FRAMEWORK_TOOL_TYPE_UTIL_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <typeindex>
|
||||
#include <typeinfo>
|
||||
|
||||
#include "mediapipe/framework/port.h"
|
||||
|
||||
Reference in New Issue
Block a user