This commit is contained in:
Loïc Touraine 2023-12-22 22:40:26 +03:00 committed by GitHub
commit 7f0b7003da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 116 additions and 1 deletions

View File

@ -1 +1 @@
6.1.1
6.3.2

View File

@ -195,6 +195,8 @@ cc_library(
srcs = [
"resource_util.cc",
"resource_util_internal.h",
"resource_path_manager.cc",
"resource_path_manager.h",
] + select({
"//conditions:default": ["resource_util_default.cc"],
"//mediapipe:android": ["resource_util_android.cc"],

View File

@ -0,0 +1,47 @@
// Copyright 2019 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/util/resource_path_manager.h"
#include <iostream>
#include "absl/strings/str_split.h"
#include "mediapipe/framework/deps/file_path.h"
#include "mediapipe/framework/port/file_helpers.h"
#include "mediapipe/framework/port/ret_check.h"
namespace mediapipe {
std::vector<std::string> resource_search_paths_;
void ResourcePathManager::AddSearchPath(const std::string& path)
{
resource_search_paths_.push_back(path);
}
absl::StatusOr<std::string> ResourcePathManager::ResolveFilePath(const std::string& path)
{
if (absl::StartsWith(path, "/")) {
return path;
}
for (auto & resource_path: resource_search_paths_) {
auto file_path = file::JoinPath(resource_path, path);
if (file::Exists(file_path).ok()) {
return file_path;
}
}
return absl::NotFoundError("No file " + path + " found in declared search paths");
}
} // namespace mediapipe

View File

@ -0,0 +1,36 @@
// Copyright 2019 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_UTIL_RESOURCE_PATH_MANAGER_H_
#define MEDIAPIPE_UTIL_RESOURCE_PATH_MANAGER_H_
#include <string>
#include "mediapipe/framework/port/statusor.h"
namespace mediapipe {
// The ResourcePathManager provides additionnal search paths handling for resources (tflite...) locations.
class ResourcePathManager {
public:
// Adds a path to search resources for.
static void AddSearchPath(const std::string& path);
// Tries to resolve a filepath from path and previously added search path.
// Either return the filepath when it exists, or absl::NotFoundError when the path doesn't exists.
static absl::StatusOr<std::string> ResolveFilePath(const std::string& path);
};
} // namespace mediapipe
#endif // MEDIAPIPE_UTIL_RESOURCE_UTIL_H_

View File

@ -22,6 +22,7 @@
#include "mediapipe/framework/port/statusor.h"
#include "mediapipe/util/android/asset_manager_util.h"
#include "mediapipe/util/android/file/base/helpers.h"
#include "mediapipe/util/resource_path_manager.h"
namespace mediapipe {
@ -80,6 +81,15 @@ absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path) {
}
}
// try to load file in potentially declared resource paths
{
auto status_or_path = ResourcePathManager::ResolveFilePath(path);
if (status_or_path.ok()) {
LOG(INFO) << "Successfully loaded: " << path;
return status_or_path;
}
}
// If that fails, assume it was a relative path, and try just the base name.
{
const size_t last_slash_idx = path.find_last_of("\\/");

View File

@ -23,6 +23,7 @@
#include "mediapipe/framework/port/ret_check.h"
#include "mediapipe/framework/port/statusor.h"
#include "mediapipe/util/resource_util.h"
#include "mediapipe/util/resource_path_manager.h"
namespace mediapipe {
@ -61,6 +62,15 @@ absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path) {
return path;
}
// try to load file in potentially declared resource paths
{
auto status_or_path = ResourcePathManager::ResolveFilePath(path);
if (status_or_path.ok()) {
LOG(INFO) << "Successfully loaded: " << path;
return status_or_path;
}
}
// Try to load a relative path or a base filename as is.
{
auto status_or_path = PathToResourceAsFileInternal(path);

View File

@ -18,6 +18,7 @@
#include "mediapipe/framework/deps/file_path.h"
#include "mediapipe/framework/port/file_helpers.h"
#include "mediapipe/framework/port/statusor.h"
#include "mediapipe/util/resource_path_manager.h"
ABSL_FLAG(
std::string, resource_root_dir, "",
@ -43,6 +44,15 @@ absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path) {
return path;
}
// try to load file in potentially declared resource paths
{
auto status_or_path = ResourcePathManager::ResolveFilePath(path);
if (status_or_path.ok()) {
LOG(INFO) << "Successfully loaded: " << path;
return status_or_path;
}
}
// Try to load the file from bazel-bin. If it does not exist, fall back to the
// resource folder.
auto bazel_path = JoinPath("bazel-bin", path);