From b74280e629297badd80de85a6f2175496de885c0 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Wed, 24 May 2023 19:21:49 +0530 Subject: [PATCH] Added a utility to create proto from a pbtxt file --- mediapipe/tasks/ios/test/vision/utils/BUILD | 10 +++++ .../vision/utils/sources/parse_proto_utils.cc | 43 +++++++++++++++++++ .../vision/utils/sources/parse_proto_utils.h | 28 ++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 mediapipe/tasks/ios/test/vision/utils/sources/parse_proto_utils.cc create mode 100644 mediapipe/tasks/ios/test/vision/utils/sources/parse_proto_utils.h diff --git a/mediapipe/tasks/ios/test/vision/utils/BUILD b/mediapipe/tasks/ios/test/vision/utils/BUILD index c5acf7a46..829356c4e 100644 --- a/mediapipe/tasks/ios/test/vision/utils/BUILD +++ b/mediapipe/tasks/ios/test/vision/utils/BUILD @@ -9,3 +9,13 @@ objc_library( module_name = "MPPImageTestUtils", deps = ["//mediapipe/tasks/ios/vision/core:MPPImage"], ) + +cc_library( + name = "parse_proto_utils", + srcs = ["sources/parse_proto_utils.cc"], + hdrs = ["sources/parse_proto_utils.h"], + deps = [ + "@com_google_absl//absl/status", + "@com_google_protobuf//:protobuf", + ], +) diff --git a/mediapipe/tasks/ios/test/vision/utils/sources/parse_proto_utils.cc b/mediapipe/tasks/ios/test/vision/utils/sources/parse_proto_utils.cc new file mode 100644 index 000000000..847f25271 --- /dev/null +++ b/mediapipe/tasks/ios/test/vision/utils/sources/parse_proto_utils.cc @@ -0,0 +1,43 @@ +/* Copyright 2022 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/tasks/ios/test/vision/utils/sources/parse_proto_utils.h" + +#include +#include +#include "google/protobuf/io/zero_copy_stream_impl.h" + +namespace mediapipe::tasks::ios::test::vision::utils { + +namespace { + using ::google::protobuf::TextFormat; + } + + absl::Status get_proto_from_pbtxt(const std::string file_path, google::protobuf::Message& proto) { + std::ifstream fin(file_path); + + if(!fin.is_open()) return absl::InvalidArgumentError( + "Cannot read input file."); + + std::stringstream strings_stream; + + strings_stream << fin.rdbuf(); + + return TextFormat::ParseFromString(strings_stream.str(), &proto) ? absl::OkStatus() : absl::InvalidArgumentError( + "Cannot read a valid proto from the input file."); + + } + +} // namespace mediapipe::tasks::text::utils diff --git a/mediapipe/tasks/ios/test/vision/utils/sources/parse_proto_utils.h b/mediapipe/tasks/ios/test/vision/utils/sources/parse_proto_utils.h new file mode 100644 index 000000000..bafd00946 --- /dev/null +++ b/mediapipe/tasks/ios/test/vision/utils/sources/parse_proto_utils.h @@ -0,0 +1,28 @@ +/* Copyright 2022 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_TASKS_IOS_TEST_VISION_UTILS_H_ +#define MEDIAPIPE_TASKS_IOS_TEST_VISION_UTILS_H_ + +#include + +#include "absl/status/status.h" +#include "google/protobuf/text_format.h" + +namespace mediapipe::tasks::ios::test::vision::utils { + absl::Status get_proto_from_pbtxt(const std::string file_path, google::protobuf::Message& proto); +} // namespace mediapipe::tasks::ios::test::utils + +#endif // MEDIAPIPE_TASKS_IOS_TEST_VISION_UTILS_H_