diff --git a/mediapipe/calculators/core/BUILD b/mediapipe/calculators/core/BUILD index 61d402f74..79c1f98c1 100644 --- a/mediapipe/calculators/core/BUILD +++ b/mediapipe/calculators/core/BUILD @@ -465,6 +465,21 @@ cc_test( ], ) +cc_library( + name = "pulsar_calculator", + srcs = ["pulsar_calculator.cc"], + hdrs = ["pulsar_calculator.h"], + visibility = [ + "//visibility:public", + ], + deps = [ + "//mediapipe/framework:calculator_framework", + "//mediapipe/framework/formats:rect_cc_proto", + "//mediapipe/framework/port:ret_check", + ], + alwayslink = 1, +) + cc_library( name = "packet_cloner_calculator", srcs = ["packet_cloner_calculator.cc"], diff --git a/mediapipe/calculators/core/pulsar_calculator.cc b/mediapipe/calculators/core/pulsar_calculator.cc new file mode 100644 index 000000000..68f3acadd --- /dev/null +++ b/mediapipe/calculators/core/pulsar_calculator.cc @@ -0,0 +1,46 @@ +// 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. + +#include "mediapipe/calculators/core/pulsar_calculator.h" + +#include "mediapipe/framework/calculator_framework.h" +#include "mediapipe/framework/formats/rect.pb.h" +#include "mediapipe/framework/port/ret_check.h" + +namespace mediapipe { + +// Example use-case: +// Suppose we are debugging a pipeline where a vector of NormalizedRect has to +// be passed to a ImageCroppingCalculator somehow, which only takes non-vectors. +// +// Example config: +// node { +// calculator: "NormalizedRectsPulsarCalculator" +// input_stream: "head_rects" +// output_stream: "head_rect" +// } +// +// node { +// calculator: "ImageCroppingCalculator" +// input_stream: "IMAGE:throttled_input_video" +// input_stream: "NORM_RECT:head_rect" +// output_stream: "IMAGE:cropped_head" +// } +typedef PulsarCalculator NormalizedRectsPulsarCalculator; +REGISTER_CALCULATOR(NormalizedRectsPulsarCalculator); + +typedef PulsarCalculator RectsPulsarCalculator; +REGISTER_CALCULATOR(RectsPulsarCalculator); + +} // namespace mediapipe diff --git a/mediapipe/calculators/core/pulsar_calculator.h b/mediapipe/calculators/core/pulsar_calculator.h new file mode 100644 index 000000000..e8aa1725d --- /dev/null +++ b/mediapipe/calculators/core/pulsar_calculator.h @@ -0,0 +1,58 @@ +// 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_CALCULATORS_CORE_PULSAR_CALCULATOR_H_ +#define MEDIAPIPE_CALCULATORS_CORE_PULSAR_CALCULATOR_H_ + +#include "mediapipe/framework/calculator_framework.h" +#include "mediapipe/framework/port/ret_check.h" + +namespace mediapipe { + +// This turns a packet of vector into as many NormalizedRect +// items there are in said vector. +template +class PulsarCalculator : public CalculatorBase { + public: + static ::mediapipe::Status GetContract(CalculatorContract* cc) { + RET_CHECK_EQ(cc->Inputs().NumEntries(), 1); + RET_CHECK_EQ(cc->Outputs().NumEntries(), 1); + cc->Inputs().Index(0).Set>(); + cc->Outputs().Index(0).Set(); + return ::mediapipe::OkStatus(); + } + + ::mediapipe::Status Open(CalculatorContext* cc) final { + cc->SetOffset(TimestampDiff(0)); + return ::mediapipe::OkStatus(); + } + + ::mediapipe::Status Process(CalculatorContext* cc) final { + const auto& items = cc->Inputs().Index(0).Get>(); + auto ts = std::max(cc->InputTimestamp(), ts_); + for (const auto& item : items) { + auto just_one = absl::make_unique(item); + cc->Outputs().Index(0).Add(just_one.release(), ts++); + } + ts_ = ts; + return ::mediapipe::OkStatus(); + } + +private: + Timestamp ts_; +}; + +} // namespace mediapipe + +#endif // MEDIAPIPE_CALCULATORS_CORE_PULSAR_CALCULATOR_H_