diff --git a/mediapipe/tasks/ios/test/vision/utils/BUILD b/mediapipe/tasks/ios/test/vision/utils/BUILD index c5acf7a46..d117ad73d 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..d6a95116a --- /dev/null +++ b/mediapipe/tasks/ios/test/vision/utils/sources/parse_proto_utils.cc @@ -0,0 +1,44 @@ +/* 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 +#include + +#include "absl/status/status.h" +#include "google/protobuf/text_format.h" + +namespace mediapipe::tasks::ios::test::vision::utils { + +namespace { +using ::google::protobuf::Message; +using ::google::protobuf::TextFormat; +} // anonymous namespace + +::absl::Status get_proto_from_pbtxt(const std::string file_path, + Message& proto) { + std::ifstream file_input_stream(file_path); + if (!file_input_stream.is_open()) + return absl::InvalidArgumentError("Cannot read input file."); + + std::stringstream strings_stream; + strings_stream << file_input_stream.rdbuf(); + + return TextFormat::ParseFromString(strings_stream.str(), &proto) + ? ::absl::OkStatus() + : ::absl::InvalidArgumentError( + "Cannot read a valid proto from the input file."); +} + +} // namespace mediapipe::tasks::ios::test::vision::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..129d0f897 --- /dev/null +++ b/mediapipe/tasks/ios/test/vision/utils/sources/parse_proto_utils.h @@ -0,0 +1,29 @@ +/* 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. +==============================================================================*/ + +#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/message.h" + +namespace mediapipe::tasks::ios::test::vision::utils { +absl::Status get_proto_from_pbtxt(std::string file_path, + ::google::protobuf::Message& proto); +} // namespace mediapipe::tasks::ios::test::vision::utils + +#endif // MEDIAPIPE_TASKS_IOS_TEST_VISION_UTILS_H_