Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Project import generated by Copybara.
GitOrigin-RevId: 852dfb05d450167899c0dd5ef7c45622a12e865b
- Loading branch information
MediaPipe Team
authored and
Hadon Nash
committed
Feb 10, 2020
1 parent
d144e56
commit de4fbc1
Showing
100 changed files
with
1,664 additions
and
628 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
mediapipe/calculators/core/constant_side_packet_calculator.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright 2020 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. | ||
|
||
#include <string> | ||
|
||
#include "mediapipe/calculators/core/constant_side_packet_calculator.pb.h" | ||
#include "mediapipe/framework/calculator_framework.h" | ||
#include "mediapipe/framework/collection_item_id.h" | ||
#include "mediapipe/framework/port/canonical_errors.h" | ||
#include "mediapipe/framework/port/ret_check.h" | ||
#include "mediapipe/framework/port/status.h" | ||
|
||
namespace mediapipe { | ||
|
||
// Generates an output side packet or multiple output side packets according to | ||
// the specified options. | ||
// | ||
// Example configs: | ||
// node { | ||
// calculator: "ConstantSidePacketCalculator" | ||
// output_side_packet: "PACKET:packet" | ||
// options: { | ||
// [mediapipe.ConstantSidePacketCalculatorOptions.ext]: { | ||
// packet { int_value: 2 } | ||
// } | ||
// } | ||
// } | ||
// | ||
// node { | ||
// calculator: "ConstantSidePacketCalculator" | ||
// output_side_packet: "PACKET:0:int_packet" | ||
// output_side_packet: "PACKET:1:bool_packet" | ||
// options: { | ||
// [mediapipe.ConstantSidePacketCalculatorOptions.ext]: { | ||
// packet { int_value: 2 } | ||
// packet { bool_value: true } | ||
// } | ||
// } | ||
// } | ||
class ConstantSidePacketCalculator : public CalculatorBase { | ||
public: | ||
static ::mediapipe::Status GetContract(CalculatorContract* cc) { | ||
const auto& options = cc->Options().GetExtension( | ||
::mediapipe::ConstantSidePacketCalculatorOptions::ext); | ||
RET_CHECK_EQ(cc->OutputSidePackets().NumEntries(kPacketTag), | ||
options.packet_size()) | ||
<< "Number of output side packets has to be same as number of packets " | ||
"configured in options."; | ||
|
||
int index = 0; | ||
for (CollectionItemId id = cc->OutputSidePackets().BeginId(kPacketTag); | ||
id != cc->OutputSidePackets().EndId(kPacketTag); ++id, ++index) { | ||
const auto& packet_options = options.packet(index); | ||
auto& packet = cc->OutputSidePackets().Get(id); | ||
if (packet_options.has_int_value()) { | ||
packet.Set<int>(); | ||
} else if (packet_options.has_float_value()) { | ||
packet.Set<float>(); | ||
} else if (packet_options.has_bool_value()) { | ||
packet.Set<bool>(); | ||
} else if (packet_options.has_string_value()) { | ||
packet.Set<std::string>(); | ||
} else { | ||
return ::mediapipe::InvalidArgumentError( | ||
"None of supported values were specified in options."); | ||
} | ||
} | ||
return ::mediapipe::OkStatus(); | ||
} | ||
|
||
::mediapipe::Status Open(CalculatorContext* cc) override { | ||
const auto& options = cc->Options().GetExtension( | ||
::mediapipe::ConstantSidePacketCalculatorOptions::ext); | ||
int index = 0; | ||
for (CollectionItemId id = cc->OutputSidePackets().BeginId(kPacketTag); | ||
id != cc->OutputSidePackets().EndId(kPacketTag); ++id, ++index) { | ||
auto& packet = cc->OutputSidePackets().Get(id); | ||
const auto& packet_options = options.packet(index); | ||
if (packet_options.has_int_value()) { | ||
packet.Set(MakePacket<int>(packet_options.int_value())); | ||
} else if (packet_options.has_float_value()) { | ||
packet.Set(MakePacket<float>(packet_options.float_value())); | ||
} else if (packet_options.has_bool_value()) { | ||
packet.Set(MakePacket<bool>(packet_options.bool_value())); | ||
} else if (packet_options.has_string_value()) { | ||
packet.Set(MakePacket<std::string>(packet_options.string_value())); | ||
} else { | ||
return ::mediapipe::InvalidArgumentError( | ||
"None of supported values were specified in options."); | ||
} | ||
} | ||
return ::mediapipe::OkStatus(); | ||
} | ||
|
||
::mediapipe::Status Process(CalculatorContext* cc) override { | ||
return ::mediapipe::OkStatus(); | ||
} | ||
|
||
private: | ||
static constexpr const char* kPacketTag = "PACKET"; | ||
}; | ||
|
||
REGISTER_CALCULATOR(ConstantSidePacketCalculator); | ||
|
||
} // namespace mediapipe |
36 changes: 36 additions & 0 deletions
36
mediapipe/calculators/core/constant_side_packet_calculator.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2020 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. | ||
|
||
syntax = "proto2"; | ||
|
||
package mediapipe; | ||
|
||
import "mediapipe/framework/calculator.proto"; | ||
|
||
message ConstantSidePacketCalculatorOptions { | ||
extend CalculatorOptions { | ||
optional ConstantSidePacketCalculatorOptions ext = 291214597; | ||
} | ||
|
||
message ConstantSidePacket { | ||
oneof value { | ||
int32 int_value = 1; | ||
float float_value = 2; | ||
bool bool_value = 3; | ||
string string_value = 4; | ||
} | ||
} | ||
|
||
repeated ConstantSidePacket packet = 1; | ||
} |
Oops, something went wrong.