No public description

PiperOrigin-RevId: 562865700
This commit is contained in:
MediaPipe Team 2023-09-05 13:00:48 -07:00 committed by Copybara-Service
parent 2aefa2308b
commit 4e52e96973
5 changed files with 122 additions and 13 deletions

View File

@ -35,6 +35,12 @@ mediapipe_proto_library(
],
)
mediapipe_proto_library(
name = "proto3_options_proto",
srcs = ["proto3_options.proto"],
visibility = ["//visibility:public"],
)
mediapipe_proto_library(
name = "zoo_mutator_proto",
srcs = ["zoo_mutator.proto"],

View File

@ -0,0 +1,25 @@
// Copyright 2023 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.
//
// Forked from mediapipe/framework/tool/source.proto.
// The forked proto must remain identical to the original proto and should be
// ONLY used by mediapipe open source project.
syntax = "proto3";
package mediapipe;
message Proto3Options {
double test_value = 1;
}

View File

@ -192,9 +192,8 @@ cc_test(
"//mediapipe/framework:calculator_framework",
"//mediapipe/framework/port:gtest_main",
"//mediapipe/framework/port:parse_text_proto",
"//mediapipe/framework/port:status",
"//mediapipe/framework/testdata:night_light_calculator_cc_proto",
"//mediapipe/framework/testdata:night_light_calculator_options_lib",
"//mediapipe/framework/testdata:proto3_options_cc_proto",
],
)

View File

@ -128,7 +128,8 @@ class OptionsMap {
return *options_.Get<T>();
}
T* result = options_.Get<T>();
if (node_config_->has_options()) {
if (node_config_->has_options() &&
HasExtension<T>(node_config_->options())) {
GetExtension(node_config_->options(), result);
} else {
GetNodeOptions(*node_config_, result);
@ -141,8 +142,9 @@ class OptionsMap {
if (options_.Has<T>()) {
return true;
}
if (node_config_->has_options()) {
return HasExtension<T>(node_config_->options());
if (node_config_->has_options() &&
HasExtension<T>(node_config_->options())) {
return true;
}
#if defined(MEDIAPIPE_PROTO_LITE) && defined(MEDIAPIPE_PROTO_THIRD_PARTY)
// protobuf::Any is unavailable with third_party/protobuf:protobuf-lite.
@ -170,7 +172,8 @@ class MutableOptionsMap : public OptionsMap {
template <class T>
void Set(const T& value) const {
*options_.Get<T>() = value;
if (node_config_->has_options()) {
if (node_config_->has_options() &&
HasExtension<T>(node_config_->options())) {
*GetExtension<T>(*node_config_->mutable_options()) = value;
} else {
SetNodeOptions(*node_config_, value);
@ -182,7 +185,8 @@ class MutableOptionsMap : public OptionsMap {
if (options_.Has<T>()) {
return options_.Get<T>();
}
if (node_config_->has_options()) {
if (node_config_->has_options() &&
HasExtension<T>(node_config_->options())) {
return GetExtension<T>(*node_config_->mutable_options());
}
T* result = options_.Get<T>();

View File

@ -17,14 +17,11 @@
#include <unistd.h>
#include <memory>
#include "mediapipe/framework/calculator_framework.h"
#include "mediapipe/framework/port/gtest.h"
#include "mediapipe/framework/port/parse_text_proto.h"
#include "mediapipe/framework/port/status.h"
#include "mediapipe/framework/port/status_macros.h"
#include "mediapipe/framework/testdata/night_light_calculator.pb.h"
#include "mediapipe/framework/testdata/proto3_options.pb.h"
namespace mediapipe {
namespace tool {
@ -40,9 +37,10 @@ TEST(OptionsMapTest, QueryNotFound) {
OptionsMap options;
options.Initialize(node);
EXPECT_FALSE(options.Has<mediapipe::NightLightCalculatorOptions>());
EXPECT_FALSE(options.Has<mediapipe::Proto3Options>());
}
TEST(OptionsMapTest, QueryFound) {
TEST(OptionsMapTest, Proto2QueryFound) {
CalculatorGraphConfig::Node node =
ParseTextProtoOrDie<CalculatorGraphConfig::Node>(R"pb(
calculator: "NightLightCalculator"
@ -64,7 +62,7 @@ TEST(OptionsMapTest, QueryFound) {
123);
}
TEST(MutableOptionsMapTest, InsertAndQueryFound) {
TEST(MutableOptionsMapTest, InsertProto2AndQueryFound) {
CalculatorGraphConfig::Node node =
ParseTextProtoOrDie<CalculatorGraphConfig::Node>(R"pb(
calculator: "NightLightCalculator"
@ -83,6 +81,83 @@ TEST(MutableOptionsMapTest, InsertAndQueryFound) {
123);
}
TEST(OptionsMapTest, Proto3QueryFound) {
CalculatorGraphConfig::Node node =
ParseTextProtoOrDie<CalculatorGraphConfig::Node>(R"pb(
calculator: "NightLightCalculator"
input_side_packet: "input_value"
output_stream: "values"
node_options {
[type.googleapis.com/mediapipe.Proto3Options] { test_value: 123 }
}
)pb");
OptionsMap options;
options.Initialize(node);
EXPECT_TRUE(options.Has<mediapipe::Proto3Options>());
EXPECT_EQ(options.Get<mediapipe::Proto3Options>().test_value(), 123);
}
TEST(MutableOptionsMapTest, InsertProto3AndQueryFound) {
CalculatorGraphConfig::Node node =
ParseTextProtoOrDie<CalculatorGraphConfig::Node>(R"pb(
calculator: "NightLightCalculator"
input_side_packet: "input_value"
output_stream: "values"
)pb");
MutableOptionsMap options;
options.Initialize(node);
EXPECT_FALSE(options.Has<mediapipe::Proto3Options>());
mediapipe::Proto3Options proto3_options;
proto3_options.set_test_value(123);
options.Set(proto3_options);
EXPECT_TRUE(options.Has<mediapipe::Proto3Options>());
EXPECT_EQ(options.Get<mediapipe::Proto3Options>().test_value(), 123);
}
TEST(OptionsMapTest, BothProto2AndProto3QueriesFound) {
CalculatorGraphConfig::Node node =
ParseTextProtoOrDie<CalculatorGraphConfig::Node>(R"pb(
calculator: "NightLightCalculator"
input_side_packet: "input_value"
output_stream: "values"
options {
[mediapipe.NightLightCalculatorOptions.ext] { jitter: 321 }
}
node_options {
[type.googleapis.com/mediapipe.Proto3Options] { test_value: 123 }
}
)pb");
OptionsMap options;
options.Initialize(node);
EXPECT_TRUE(options.Has<mediapipe::Proto3Options>());
EXPECT_EQ(options.Get<mediapipe::Proto3Options>().test_value(), 123);
EXPECT_TRUE(options.Has<mediapipe::NightLightCalculatorOptions>());
EXPECT_EQ(options.Get<mediapipe::NightLightCalculatorOptions>().jitter(),
321);
}
TEST(OptionsMapTest, PrefersOptionsOverNodeOptions) {
CalculatorGraphConfig::Node node =
ParseTextProtoOrDie<CalculatorGraphConfig::Node>(R"pb(
calculator: "NightLightCalculator"
input_side_packet: "input_value"
output_stream: "values"
options {
[mediapipe.NightLightCalculatorOptions.ext] { jitter: 111 }
}
node_options {
[type.googleapis.com/mediapipe.NightLightCalculatorOptions] {
jitter: 222
}
}
)pb");
OptionsMap options;
options.Initialize(node);
EXPECT_TRUE(options.Has<mediapipe::NightLightCalculatorOptions>());
EXPECT_EQ(options.Get<mediapipe::NightLightCalculatorOptions>().jitter(),
111);
}
} // namespace
} // namespace tool
} // namespace mediapipe