From 886a118232c97e7a58231f4660886fb5230f7899 Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Thu, 7 Sep 2023 21:36:04 -0700 Subject: [PATCH] landmarks_to_detection stream utility function. PiperOrigin-RevId: 563633314 --- mediapipe/framework/api2/stream/BUILD | 28 +++++++++++++++ .../api2/stream/landmarks_to_detection.cc | 17 +++++++++ .../api2/stream/landmarks_to_detection.h | 16 +++++++++ .../stream/landmarks_to_detection_test.cc | 35 +++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 mediapipe/framework/api2/stream/landmarks_to_detection.cc create mode 100644 mediapipe/framework/api2/stream/landmarks_to_detection.h create mode 100644 mediapipe/framework/api2/stream/landmarks_to_detection_test.cc diff --git a/mediapipe/framework/api2/stream/BUILD b/mediapipe/framework/api2/stream/BUILD index 086c68a5..f59a65d9 100644 --- a/mediapipe/framework/api2/stream/BUILD +++ b/mediapipe/framework/api2/stream/BUILD @@ -2,6 +2,34 @@ package(default_visibility = ["//visibility:public"]) licenses(["notice"]) +cc_library( + name = "landmarks_to_detection", + srcs = ["landmarks_to_detection.cc"], + hdrs = ["landmarks_to_detection.h"], + deps = [ + "//mediapipe/calculators/util:landmarks_to_detection_calculator", + "//mediapipe/framework/api2:builder", + "//mediapipe/framework/formats:detection_cc_proto", + "//mediapipe/framework/formats:landmark_cc_proto", + ], +) + +cc_test( + name = "landmarks_to_detection_test", + srcs = ["landmarks_to_detection_test.cc"], + deps = [ + ":landmarks_to_detection", + "//mediapipe/framework/api2:builder", + "//mediapipe/framework/formats:detection_cc_proto", + "//mediapipe/framework/formats:landmark_cc_proto", + "//mediapipe/framework/port:gtest", + "//mediapipe/framework/port:gtest_main", + "//mediapipe/framework/port:parse_text_proto", + "//mediapipe/framework/port:status", + "//mediapipe/framework/port:status_matchers", + ], +) + cc_library( name = "landmarks_projection", srcs = ["landmarks_projection.cc"], diff --git a/mediapipe/framework/api2/stream/landmarks_to_detection.cc b/mediapipe/framework/api2/stream/landmarks_to_detection.cc new file mode 100644 index 00000000..99e576ba --- /dev/null +++ b/mediapipe/framework/api2/stream/landmarks_to_detection.cc @@ -0,0 +1,17 @@ +#include "mediapipe/framework/api2/stream/landmarks_to_detection.h" + +#include "mediapipe/framework/api2/builder.h" +#include "mediapipe/framework/formats/detection.pb.h" +#include "mediapipe/framework/formats/landmark.pb.h" + +namespace mediapipe::api2::builder { + +Stream ConvertLandmarksToDetection( + Stream landmarks, Graph& graph) { + auto& landmarks_to_detection = + graph.AddNode("LandmarksToDetectionCalculator"); + landmarks.ConnectTo(landmarks_to_detection.In("NORM_LANDMARKS")); + return landmarks_to_detection.Out("DETECTION").Cast(); +} + +} // namespace mediapipe::api2::builder diff --git a/mediapipe/framework/api2/stream/landmarks_to_detection.h b/mediapipe/framework/api2/stream/landmarks_to_detection.h new file mode 100644 index 00000000..0f0004b1 --- /dev/null +++ b/mediapipe/framework/api2/stream/landmarks_to_detection.h @@ -0,0 +1,16 @@ +#ifndef MEDIAPIPE_FRAMEWORK_API2_STREAM_LANDMARKS_TO_DETECTION_H_ +#define MEDIAPIPE_FRAMEWORK_API2_STREAM_LANDMARKS_TO_DETECTION_H_ + +#include "mediapipe/framework/api2/builder.h" +#include "mediapipe/framework/formats/detection.pb.h" +#include "mediapipe/framework/formats/landmark.pb.h" + +namespace mediapipe::api2::builder { + +// Updates @graph to convert @landmarks to a detection. +Stream ConvertLandmarksToDetection( + Stream landmarks, Graph& graph); + +} // namespace mediapipe::api2::builder + +#endif // MEDIAPIPE_FRAMEWORK_API2_STREAM_LANDMARKS_TO_DETECTION_H_ diff --git a/mediapipe/framework/api2/stream/landmarks_to_detection_test.cc b/mediapipe/framework/api2/stream/landmarks_to_detection_test.cc new file mode 100644 index 00000000..8bd54530 --- /dev/null +++ b/mediapipe/framework/api2/stream/landmarks_to_detection_test.cc @@ -0,0 +1,35 @@ +#include "mediapipe/framework/api2/stream/landmarks_to_detection.h" + +#include "mediapipe/framework/api2/builder.h" +#include "mediapipe/framework/formats/detection.pb.h" +#include "mediapipe/framework/formats/landmark.pb.h" +#include "mediapipe/framework/port/gmock.h" +#include "mediapipe/framework/port/gtest.h" +#include "mediapipe/framework/port/parse_text_proto.h" +#include "mediapipe/framework/port/status_matchers.h" + +namespace mediapipe::api2::builder { +namespace { + +TEST(LandmarksToDetection, VerifyConfig) { + mediapipe::api2::builder::Graph graph; + + Stream landmarks = + graph.In("LANDMARKS").Cast(); + Stream detection = ConvertLandmarksToDetection(landmarks, graph); + detection.SetName("detection"); + + EXPECT_THAT( + graph.GetConfig(), + EqualsProto(mediapipe::ParseTextProtoOrDie(R"pb( + node { + calculator: "LandmarksToDetectionCalculator" + input_stream: "NORM_LANDMARKS:__stream_0" + output_stream: "DETECTION:detection" + } + input_stream: "LANDMARKS:__stream_0" + )pb"))); +} + +} // namespace +} // namespace mediapipe::api2::builder