Added a utility to create proto from a pbtxt file

This commit is contained in:
Prianka Liz Kariat 2023-05-24 19:21:49 +05:30
parent 117bb507e6
commit b74280e629
3 changed files with 81 additions and 0 deletions

View File

@ -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",
],
)

View File

@ -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 <fstream>
#include <sstream>
#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

View File

@ -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 <string>
#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_