diff --git a/mediapipe/tasks/cc/vision/face_geometry/calculators/BUILD b/mediapipe/tasks/cc/vision/face_geometry/calculators/BUILD index 7a5041295..b134c81f4 100644 --- a/mediapipe/tasks/cc/vision/face_geometry/calculators/BUILD +++ b/mediapipe/tasks/cc/vision/face_geometry/calculators/BUILD @@ -18,6 +18,28 @@ licenses(["notice"]) package(default_visibility = ["//mediapipe/tasks:internal"]) +mediapipe_proto_library( + name = "env_generator_calculator_proto", + srcs = ["env_generator_calculator.proto"], + deps = [ + "//mediapipe/framework:calculator_options_proto", + "//mediapipe/tasks/cc/vision/face_geometry/proto:environment_proto", + ], +) + +cc_library( + name = "env_generator_calculator", + srcs = ["env_generator_calculator.cc"], + deps = [ + ":env_generator_calculator_cc_proto", + "//mediapipe/framework:calculator_framework", + "//mediapipe/framework/port:status", + "//mediapipe/tasks/cc/vision/face_geometry/libs:validation_utils", + "//mediapipe/tasks/cc/vision/face_geometry/proto:environment_cc_proto", + ], + alwayslink = 1, +) + mediapipe_proto_library( name = "geometry_pipeline_calculator_proto", srcs = ["geometry_pipeline_calculator.proto"], diff --git a/mediapipe/tasks/cc/vision/face_geometry/calculators/env_generator_calculator.cc b/mediapipe/tasks/cc/vision/face_geometry/calculators/env_generator_calculator.cc new file mode 100644 index 000000000..9a7c92fde --- /dev/null +++ b/mediapipe/tasks/cc/vision/face_geometry/calculators/env_generator_calculator.cc @@ -0,0 +1,84 @@ +// 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. + +#include "mediapipe/framework/calculator_framework.h" +#include "mediapipe/framework/port/status.h" +#include "mediapipe/framework/port/status_macros.h" +#include "mediapipe/tasks/cc/vision/face_geometry/calculators/env_generator_calculator.pb.h" +#include "mediapipe/tasks/cc/vision/face_geometry/libs/validation_utils.h" +#include "mediapipe/tasks/cc/vision/face_geometry/proto/environment.pb.h" + +namespace mediapipe::tasks::vision::face_geometry { +namespace { + +static constexpr char kEnvironmentTag[] = "ENVIRONMENT"; + +using ::mediapipe::tasks::vision::face_geometry::proto::Environment; + +// A calculator that generates an environment, which describes a virtual scene. +// +// Output side packets: +// ENVIRONMENT (`Environment`, required) +// Describes an environment; includes the camera frame origin point location +// as well as virtual camera parameters. +// +// Options: +// environment (`Environment`, required): +// Defines an environment to be packed as the output side packet. +// +// Must be valid (for details, please refer to the proto message definition +// comments and/or `face_geometry/libs/validation_utils.h/cc`) +// +class EnvGeneratorCalculator : public CalculatorBase { + public: + static absl::Status GetContract(CalculatorContract* cc) { + cc->OutputSidePackets().Tag(kEnvironmentTag).Set(); + return absl::OkStatus(); + } + + absl::Status Open(CalculatorContext* cc) override { + cc->SetOffset(mediapipe::TimestampDiff(0)); + + const Environment& environment = + cc->Options().environment(); + + MP_RETURN_IF_ERROR(ValidateEnvironment(environment)) + << "Invalid environment!"; + + cc->OutputSidePackets() + .Tag(kEnvironmentTag) + .Set(mediapipe::MakePacket(environment)); + + return absl::OkStatus(); + } + + absl::Status Process(CalculatorContext* cc) override { + return absl::OkStatus(); + } + + absl::Status Close(CalculatorContext* cc) override { + return absl::OkStatus(); + } +}; + +} // namespace + +using FaceGeometryEnvGeneratorCalculator = EnvGeneratorCalculator; + +// clang-format off +REGISTER_CALCULATOR( + ::mediapipe::tasks::vision::face_geometry::FaceGeometryEnvGeneratorCalculator); // NOLINT +// clang-format on + +} // namespace mediapipe::tasks::vision::face_geometry diff --git a/mediapipe/tasks/cc/vision/face_geometry/calculators/env_generator_calculator.proto b/mediapipe/tasks/cc/vision/face_geometry/calculators/env_generator_calculator.proto new file mode 100644 index 000000000..505f32884 --- /dev/null +++ b/mediapipe/tasks/cc/vision/face_geometry/calculators/env_generator_calculator.proto @@ -0,0 +1,32 @@ +// 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. + +syntax = "proto2"; + +package mediapipe.tasks.vision.face_geometry; + +import "mediapipe/framework/calculator_options.proto"; +import "mediapipe/tasks/cc/vision/face_geometry/proto/environment.proto"; + +message FaceGeometryEnvGeneratorCalculatorOptions { + extend mediapipe.CalculatorOptions { + optional FaceGeometryEnvGeneratorCalculatorOptions ext = 512499201; + } + + // Defines an environment to be packed as the output side packet. + // + // Must be valid (for details, please refer to the proto message definition + // comments and/or `face_geometry/libs/validation_utils.h/cc`) + optional proto.Environment environment = 1; +}