2019-08-17 03:49:25 +02:00
|
|
|
// 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.
|
|
|
|
|
2021-05-10 21:19:00 +02:00
|
|
|
#include "mediapipe/framework/api2/node.h"
|
2019-08-17 03:49:25 +02:00
|
|
|
#include "mediapipe/framework/calculator_framework.h"
|
2021-05-10 21:19:00 +02:00
|
|
|
#include "mediapipe/framework/formats/image.h"
|
2019-08-17 03:49:25 +02:00
|
|
|
#include "mediapipe/framework/formats/image_frame.h"
|
|
|
|
|
2021-02-27 09:21:16 +01:00
|
|
|
#if !MEDIAPIPE_DISABLE_GPU
|
2019-08-17 03:49:25 +02:00
|
|
|
#include "mediapipe/gpu/gpu_buffer.h"
|
2021-02-27 09:21:16 +01:00
|
|
|
#endif // !MEDIAPIPE_DISABLE_GPU
|
2019-08-17 03:49:25 +02:00
|
|
|
|
|
|
|
namespace mediapipe {
|
2021-05-10 21:19:00 +02:00
|
|
|
namespace api2 {
|
|
|
|
|
|
|
|
#if MEDIAPIPE_DISABLE_GPU
|
|
|
|
// Just a placeholder to not have to depend on mediapipe::GpuBuffer.
|
|
|
|
using GpuBuffer = AnyType;
|
|
|
|
#else
|
|
|
|
using GpuBuffer = mediapipe::GpuBuffer;
|
|
|
|
#endif // MEDIAPIPE_DISABLE_GPU
|
2019-08-17 03:49:25 +02:00
|
|
|
|
|
|
|
// Extracts image properties from the input image and outputs the properties.
|
|
|
|
// Currently only supports image size.
|
|
|
|
// Input:
|
|
|
|
// One of the following:
|
2021-05-10 21:19:00 +02:00
|
|
|
// IMAGE: An Image or ImageFrame (for backward compatibility with existing
|
|
|
|
// graphs that use IMAGE for ImageFrame input)
|
|
|
|
// IMAGE_CPU: An ImageFrame
|
2019-08-17 03:49:25 +02:00
|
|
|
// IMAGE_GPU: A GpuBuffer
|
|
|
|
//
|
|
|
|
// Output:
|
|
|
|
// SIZE: Size (as a std::pair<int, int>) of the input image.
|
|
|
|
//
|
|
|
|
// Example usage:
|
|
|
|
// node {
|
|
|
|
// calculator: "ImagePropertiesCalculator"
|
|
|
|
// input_stream: "IMAGE:image"
|
|
|
|
// output_stream: "SIZE:size"
|
|
|
|
// }
|
2021-05-10 21:19:00 +02:00
|
|
|
class ImagePropertiesCalculator : public Node {
|
2019-08-17 03:49:25 +02:00
|
|
|
public:
|
2021-05-10 21:19:00 +02:00
|
|
|
static constexpr Input<
|
|
|
|
OneOf<mediapipe::Image, mediapipe::ImageFrame>>::Optional kIn{"IMAGE"};
|
|
|
|
// IMAGE_CPU, dedicated to ImageFrame input, is only needed in some top-level
|
|
|
|
// graphs for the Python Solution APIs to figure out the type of input stream
|
|
|
|
// without running into ambiguities from IMAGE.
|
|
|
|
// TODO: Remove IMAGE_CPU once Python Solution APIs adopt Image.
|
|
|
|
static constexpr Input<mediapipe::ImageFrame>::Optional kInCpu{"IMAGE_CPU"};
|
|
|
|
static constexpr Input<GpuBuffer>::Optional kInGpu{"IMAGE_GPU"};
|
|
|
|
static constexpr Output<std::pair<int, int>> kOut{"SIZE"};
|
2019-08-17 03:49:25 +02:00
|
|
|
|
2021-05-10 21:19:00 +02:00
|
|
|
MEDIAPIPE_NODE_CONTRACT(kIn, kInCpu, kInGpu, kOut);
|
2019-08-17 03:49:25 +02:00
|
|
|
|
2021-05-10 21:19:00 +02:00
|
|
|
static absl::Status UpdateContract(CalculatorContract* cc) {
|
|
|
|
RET_CHECK_EQ(kIn(cc).IsConnected() + kInCpu(cc).IsConnected() +
|
|
|
|
kInGpu(cc).IsConnected(),
|
|
|
|
1)
|
|
|
|
<< "One and only one of IMAGE, IMAGE_CPU and IMAGE_GPU input is "
|
|
|
|
"expected.";
|
2019-08-17 03:49:25 +02:00
|
|
|
|
2021-02-27 09:21:16 +01:00
|
|
|
return absl::OkStatus();
|
2019-08-17 03:49:25 +02:00
|
|
|
}
|
|
|
|
|
2021-02-27 09:21:16 +01:00
|
|
|
absl::Status Process(CalculatorContext* cc) override {
|
2021-05-10 21:19:00 +02:00
|
|
|
std::pair<int, int> size;
|
2019-08-17 03:49:25 +02:00
|
|
|
|
2021-05-10 21:19:00 +02:00
|
|
|
if (kIn(cc).IsConnected()) {
|
|
|
|
kIn(cc).Visit(
|
|
|
|
[&size](const mediapipe::Image& value) {
|
|
|
|
size.first = value.width();
|
|
|
|
size.second = value.height();
|
|
|
|
},
|
|
|
|
[&size](const mediapipe::ImageFrame& value) {
|
|
|
|
size.first = value.Width();
|
|
|
|
size.second = value.Height();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (kInCpu(cc).IsConnected()) {
|
|
|
|
const auto& image = *kInCpu(cc);
|
|
|
|
size.first = image.Width();
|
|
|
|
size.second = image.Height();
|
2019-08-17 03:49:25 +02:00
|
|
|
}
|
2021-02-27 09:21:16 +01:00
|
|
|
#if !MEDIAPIPE_DISABLE_GPU
|
2021-05-10 21:19:00 +02:00
|
|
|
if (kInGpu(cc).IsConnected()) {
|
|
|
|
const auto& image = *kInGpu(cc);
|
|
|
|
size.first = image.width();
|
|
|
|
size.second = image.height();
|
2019-08-17 03:49:25 +02:00
|
|
|
}
|
2021-02-27 09:21:16 +01:00
|
|
|
#endif // !MEDIAPIPE_DISABLE_GPU
|
2019-08-17 03:49:25 +02:00
|
|
|
|
2021-05-10 21:19:00 +02:00
|
|
|
kOut(cc).Send(size);
|
2019-08-17 03:49:25 +02:00
|
|
|
|
2021-02-27 09:21:16 +01:00
|
|
|
return absl::OkStatus();
|
2019-08-17 03:49:25 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-10 21:19:00 +02:00
|
|
|
MEDIAPIPE_REGISTER_NODE(ImagePropertiesCalculator);
|
|
|
|
|
|
|
|
} // namespace api2
|
2019-08-17 03:49:25 +02:00
|
|
|
} // namespace mediapipe
|