Skip to content

Commit

Permalink
Migrate modules to use ResourceProviderCalculator.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 675741716
  • Loading branch information
MediaPipe Team authored and copybara-github committed Sep 17, 2024
1 parent 313c72d commit a5ac9b1
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 70 deletions.
5 changes: 3 additions & 2 deletions mediapipe/framework/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -667,11 +667,12 @@ cc_library(
hdrs = ["resources.h"],
visibility = ["//visibility:public"],
deps = [
"//mediapipe/framework/port:status",
"//mediapipe/framework/tool:status_util",
"//mediapipe/util:resource_util",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
],
)
Expand All @@ -687,7 +688,7 @@ cc_test(
"//mediapipe/framework/port:gtest_main",
"//mediapipe/framework/port:status_matchers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings:string_view",
],
Expand Down
34 changes: 29 additions & 5 deletions mediapipe/framework/resources.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
#include <utility>

#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "mediapipe/framework/port/status_macros.h"
#include "mediapipe/framework/tool/status_util.h"
#include "mediapipe/util/resource_util.h"

namespace mediapipe {
Expand Down Expand Up @@ -37,10 +39,32 @@ class DefaultResources : public Resources {
public:
absl::StatusOr<std::unique_ptr<Resource>> Get(
absl::string_view resource_id, const Options& options) const final {
std::string contents;
MP_RETURN_IF_ERROR(GetResourceContents(std::string(resource_id), &contents,
options.read_as_binary));
return MakeStringResource(std::move(contents));
// First try to load resource as is.
std::string path(resource_id);
std::string output;
absl::Status status =
GetResourceContents(path, &output, options.read_as_binary);
if (status.ok()) {
return MakeStringResource(std::move(output));
}

// Try to resolve resource_id.
absl::StatusOr<std::string> resolved_path = PathToResourceAsFile(path);
if (!resolved_path.ok() || resolved_path.value() == path) {
return tool::CombinedStatus(
absl::StrCat("Failed to load resource: ", resource_id),
{status, resolved_path.status()});
}

// Try to load by resolved path.
absl::Status status_for_resolved = GetResourceContents(
resolved_path.value(), &output, options.read_as_binary);
if (status_for_resolved.ok()) {
return MakeStringResource(std::move(output));
}
return tool::CombinedStatus(
absl::StrCat("Failed to load resource: ", resource_id),
{status, status_for_resolved});
}
};

Expand Down
26 changes: 25 additions & 1 deletion mediapipe/framework/resources_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
#include <utility>

#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/flags/declare.h"
#include "absl/flags/flag.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "mediapipe/framework/port/gmock.h"
#include "mediapipe/framework/port/gtest.h"
#include "mediapipe/framework/port/status_matchers.h"

ABSL_DECLARE_FLAG(std::string, resource_root_dir);

namespace mediapipe {
namespace {

Expand All @@ -36,6 +39,27 @@ TEST(Resources, CanCreateDefaultResourcesAndReadFileContents) {
EXPECT_EQ(resource->ToStringView(), "File system calculator contents\n");
}

TEST(Resources, CanReadFileContentsByUnresolvedId) {
absl::SetFlag(&FLAGS_resource_root_dir, "mediapipe/framework/testdata");
std::unique_ptr<Resources> resources = CreateDefaultResources();

MP_ASSERT_OK_AND_ASSIGN(std::unique_ptr<Resource> resource,
resources->Get("resource_calculator.data"));
EXPECT_EQ(resource->ToStringView(), "File system calculator contents\n");
}

// PathToResourceFile is called in many in places and Resource object may
// receive an already resolved id.
TEST(Resources, CanReadFileContentsByResolvedIdWhenRootDirSpecified) {
absl::SetFlag(&FLAGS_resource_root_dir, "mediapipe/framework/testdata");
std::unique_ptr<Resources> resources = CreateDefaultResources();

MP_ASSERT_OK_AND_ASSIGN(
std::unique_ptr<Resource> resource,
resources->Get("mediapipe/framework/testdata/resource_calculator.data"));
EXPECT_EQ(resource->ToStringView(), "File system calculator contents\n");
}

TEST(Resources, CanCreateDefaultResourcesWithMappingAndReadFileContents) {
absl::flat_hash_map<std::string, std::string> mapping = {
{"$CUSTOM_ID", "mediapipe/framework/testdata/resource_calculator.data"}};
Expand Down
2 changes: 1 addition & 1 deletion mediapipe/modules/face_landmark/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ mediapipe_simple_subgraph(
deps = [
"//mediapipe/calculators/core:constant_side_packet_calculator",
"//mediapipe/calculators/tflite:tflite_model_calculator",
"//mediapipe/calculators/util:local_file_contents_calculator",
"//mediapipe/calculators/util:resource_provider_calculator",
"//mediapipe/framework/tool:switch_container",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ node {
}
}

# Loads the file in the specified path into a blob.
# Loads the file in the specified path as a resource.
node {
calculator: "LocalFileContentsCalculator"
input_side_packet: "FILE_PATH:model_path"
output_side_packet: "CONTENTS:model_blob"
calculator: "ResourceProviderCalculator"
input_side_packet: "RESOURCE_ID:model_path"
output_side_packet: "RESOURCE:model_resource"
}

# Converts the input blob into a TF Lite model.
# Converts the provided resource into a TF Lite model.
node {
calculator: "TfLiteModelCalculator"
input_side_packet: "MODEL_BLOB:model_blob"
input_side_packet: "MODEL_RESOURCE:model_resource"
output_side_packet: "MODEL:model"
}
2 changes: 1 addition & 1 deletion mediapipe/modules/hand_landmark/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mediapipe_simple_subgraph(
deps = [
"//mediapipe/calculators/core:constant_side_packet_calculator",
"//mediapipe/calculators/tflite:tflite_model_calculator",
"//mediapipe/calculators/util:local_file_contents_calculator",
"//mediapipe/calculators/util:resource_provider_calculator",
"//mediapipe/framework/tool:switch_container",
],
)
Expand Down
17 changes: 6 additions & 11 deletions mediapipe/modules/hand_landmark/hand_landmark_model_loader.pbtxt
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,16 @@ node {
}
}

# Loads the file in the specified path into a blob.
# Loads the file in the specified path as a resource.
node {
calculator: "LocalFileContentsCalculator"
input_side_packet: "FILE_PATH:model_path"
output_side_packet: "CONTENTS:model_blob"
options: {
[mediapipe.LocalFileContentsCalculatorOptions.ext]: {
text_mode: false
}
}
calculator: "ResourceProviderCalculator"
input_side_packet: "RESOURCE_ID:model_path"
output_side_packet: "RESOURCE:model_resource"
}

# Converts the input blob into a TF Lite model.
# Converts the provided resource into a TF Lite model.
node {
calculator: "TfLiteModelCalculator"
input_side_packet: "MODEL_BLOB:model_blob"
input_side_packet: "MODEL_RESOURCE:model_resource"
output_side_packet: "MODEL:model"
}
2 changes: 1 addition & 1 deletion mediapipe/modules/objectron/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ mediapipe_simple_subgraph(
"//mediapipe/calculators/util:association_norm_rect_calculator",
"//mediapipe/calculators/util:collection_has_min_size_calculator",
"//mediapipe/calculators/util:detections_to_rects_calculator",
"//mediapipe/calculators/util:local_file_contents_calculator",
"//mediapipe/calculators/util:resource_provider_calculator",
"//mediapipe/modules/objectron/calculators:frame_annotation_to_rect_calculator",
"//mediapipe/modules/objectron/calculators:landmarks_to_frame_annotation_calculator",
"//mediapipe/modules/objectron/calculators:lift_2d_frame_annotation_to_3d_calculator",
Expand Down
12 changes: 6 additions & 6 deletions mediapipe/modules/objectron/objectron_cpu.pbtxt
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ output_stream: "MULTI_LANDMARKS:multi_box_landmarks"
# Crop rectangles derived from bounding box landmarks.
output_stream: "NORM_RECTS:multi_box_rects"

# Loads the file in the specified path into a blob.
# Loads the file in the specified path as a resource.
node {
calculator: "LocalFileContentsCalculator"
input_side_packet: "FILE_PATH:0:box_landmark_model_path"
output_side_packet: "CONTENTS:0:box_landmark_model_blob"
calculator: "ResourceProviderCalculator"
input_side_packet: "RESOURCE_ID:0:box_landmark_model_path"
output_side_packet: "RESOURCE:0:box_landmark_model_resource"
}

# Converts the input blob into a TF Lite model.
# Converts the resource into a TF Lite model.
node {
calculator: "TfLiteModelCalculator"
input_side_packet: "MODEL_BLOB:box_landmark_model_blob"
input_side_packet: "MODEL_RESOURCE:box_landmark_model_resource"
output_side_packet: "MODEL:box_landmark_model"
}

Expand Down
2 changes: 1 addition & 1 deletion mediapipe/modules/palm_detection/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mediapipe_simple_subgraph(
deps = [
"//mediapipe/calculators/core:constant_side_packet_calculator",
"//mediapipe/calculators/tflite:tflite_model_calculator",
"//mediapipe/calculators/util:local_file_contents_calculator",
"//mediapipe/calculators/util:resource_provider_calculator",
"//mediapipe/framework/tool:switch_container",
],
)
Expand Down
17 changes: 6 additions & 11 deletions mediapipe/modules/palm_detection/palm_detection_model_loader.pbtxt
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,16 @@ node {
}
}

# Loads the file in the specified path into a blob.
# Loads the file in the specified path as a resource.
node {
calculator: "LocalFileContentsCalculator"
input_side_packet: "FILE_PATH:model_path"
output_side_packet: "CONTENTS:model_blob"
options: {
[mediapipe.LocalFileContentsCalculatorOptions.ext]: {
text_mode: false
}
}
calculator: "ResourceProviderCalculator"
input_side_packet: "RESOURCE_ID:model_path"
output_side_packet: "RESOURCE:model_resource"
}

# Converts the input blob into a TF Lite model.
# Converts the provided resource into a TF Lite model.
node {
calculator: "TfLiteModelCalculator"
input_side_packet: "MODEL_BLOB:model_blob"
input_side_packet: "MODEL_RESOURCE:model_resource"
output_side_packet: "MODEL:model"
}
2 changes: 1 addition & 1 deletion mediapipe/modules/pose_landmark/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mediapipe_simple_subgraph(
deps = [
"//mediapipe/calculators/core:constant_side_packet_calculator",
"//mediapipe/calculators/tflite:tflite_model_calculator",
"//mediapipe/calculators/util:local_file_contents_calculator",
"//mediapipe/calculators/util:resource_provider_calculator",
"//mediapipe/framework/tool:switch_container",
],
)
Expand Down
17 changes: 6 additions & 11 deletions mediapipe/modules/pose_landmark/pose_landmark_model_loader.pbtxt
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,16 @@ node {
}
}

# Loads the file in the specified path into a blob.
# Loads the file in the specified path as a resource.
node {
calculator: "LocalFileContentsCalculator"
input_side_packet: "FILE_PATH:model_path"
output_side_packet: "CONTENTS:model_blob"
options: {
[mediapipe.LocalFileContentsCalculatorOptions.ext]: {
text_mode: false
}
}
calculator: "ResourceProviderCalculator"
input_side_packet: "RESOURCE_ID:model_path"
output_side_packet: "RESOURCE:model_resource"
}

# Converts the input blob into a TF Lite model.
# Converts the provided resource into a TF Lite model.
node {
calculator: "TfLiteModelCalculator"
input_side_packet: "MODEL_BLOB:model_blob"
input_side_packet: "MODEL_RESOURCE:model_resource"
output_side_packet: "MODEL:model"
}
2 changes: 1 addition & 1 deletion mediapipe/modules/selfie_segmentation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mediapipe_simple_subgraph(
deps = [
"//mediapipe/calculators/core:constant_side_packet_calculator",
"//mediapipe/calculators/tflite:tflite_model_calculator",
"//mediapipe/calculators/util:local_file_contents_calculator",
"//mediapipe/calculators/util:resource_provider_calculator",
"//mediapipe/framework/tool:switch_container",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,16 @@ node {
}
}

# Loads the file in the specified path into a blob.
# Loads the file in the specified path as a resource.
node {
calculator: "LocalFileContentsCalculator"
input_side_packet: "FILE_PATH:model_path"
output_side_packet: "CONTENTS:model_blob"
options: {
[mediapipe.LocalFileContentsCalculatorOptions.ext]: {
text_mode: false
}
}
calculator: "ResourceProviderCalculator"
input_side_packet: "RESOURCE_ID:model_path"
output_side_packet: "RESOURCE:model_resource"
}

# Converts the input blob into a TF Lite model.
# Converts the provided resource into a TF Lite model.
node {
calculator: "TfLiteModelCalculator"
input_side_packet: "MODEL_BLOB:model_blob"
input_side_packet: "MODEL_RESOURCE:model_resource"
output_side_packet: "MODEL:model"
}

0 comments on commit a5ac9b1

Please sign in to comment.