-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ResourceProviderCalculator (replacement for LocalFileContentsCalc…
…ulator) PiperOrigin-RevId: 675640351
- Loading branch information
1 parent
7d9d336
commit c0fa667
Showing
5 changed files
with
413 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
mediapipe/calculators/util/resource_provider_calculator.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2024 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/calculators/util/resource_provider_calculator.h" | ||
|
||
#include <memory> | ||
#include <utility> | ||
|
||
#include "absl/status/status.h" | ||
#include "absl/status/statusor.h" | ||
#include "absl/strings/string_view.h" | ||
#include "mediapipe/calculators/util/resource_provider_calculator.pb.h" | ||
#include "mediapipe/framework/api2/node.h" | ||
#include "mediapipe/framework/api2/packet.h" | ||
#include "mediapipe/framework/api2/port.h" | ||
#include "mediapipe/framework/calculator_framework.h" | ||
#include "mediapipe/framework/port/ret_check.h" | ||
#include "mediapipe/framework/port/status_macros.h" | ||
#include "mediapipe/framework/resources.h" | ||
|
||
namespace mediapipe::api2 { | ||
|
||
absl::Status ResourceProviderCalculator::UpdateContract(CalculatorContext* cc) { | ||
RET_CHECK_GT(kResources(cc).Count(), 0) | ||
<< "At least one output resource must be specified."; | ||
const bool uses_side_packets = kIds(cc).Count() > 0; | ||
const auto& opts = cc->Options<ResourceProviderCalculatorOptions>(); | ||
const bool uses_options = opts.resource_id_size() > 0; | ||
RET_CHECK(uses_side_packets ^ uses_options) | ||
<< "Either side packets or options must be used, not both."; | ||
|
||
if (uses_side_packets) { | ||
RET_CHECK_EQ(kIds(cc).Count(), kResources(cc).Count()); | ||
} else if (uses_options) { | ||
RET_CHECK_EQ(opts.resource_id_size(), kResources(cc).Count()); | ||
} | ||
return absl::OkStatus(); | ||
} | ||
|
||
absl::Status ResourceProviderCalculator::Open(CalculatorContext* cc) { | ||
const bool uses_side_packets = kIds(cc).Count() > 0; | ||
const auto& opts = cc->Options<ResourceProviderCalculatorOptions>(); | ||
Resources::Options res_opts = {}; | ||
res_opts.read_as_binary = | ||
opts.read_mode() != ResourceProviderCalculatorOptions::READ_MODE_TEXT; | ||
|
||
auto get_resource_id_fn = [&](int i) -> absl::StatusOr<absl::string_view> { | ||
if (uses_side_packets) { | ||
return kIds(cc)[i].Get(); | ||
} | ||
return opts.resource_id(i); | ||
}; | ||
|
||
for (int i = 0; i < kResources(cc).Count(); ++i) { | ||
MP_ASSIGN_OR_RETURN(absl::string_view res_id, get_resource_id_fn(i)); | ||
MP_ASSIGN_OR_RETURN(std::unique_ptr<Resource> res, | ||
cc->GetResources().Get(res_id, res_opts)); | ||
Packet<Resource> res_packet = api2::PacketAdopting(std::move(res)); | ||
kResources(cc)[i].Set(std::move(res_packet)); | ||
} | ||
return absl::OkStatus(); | ||
} | ||
|
||
MEDIAPIPE_REGISTER_NODE(ResourceProviderCalculator) | ||
|
||
} // namespace mediapipe::api2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#ifndef MEDIAPIPE_CALCULATORS_UTIL_RESOURCE_PROVIDER_CALCULATOR_H_ | ||
#define MEDIAPIPE_CALCULATORS_UTIL_RESOURCE_PROVIDER_CALCULATOR_H_ | ||
|
||
#include <string> | ||
|
||
#include "absl/status/status.h" | ||
#include "mediapipe/framework/api2/node.h" | ||
#include "mediapipe/framework/api2/port.h" | ||
#include "mediapipe/framework/calculator_framework.h" | ||
#include "mediapipe/framework/resources.h" | ||
namespace mediapipe::api2 { | ||
|
||
// The calculator takes resource id (e.g. file path) as input side packet or | ||
// calculator options and provides the corresponding resource. | ||
// | ||
// NOTE: calculator supports loading multiple resources. | ||
// | ||
// Example config: | ||
// | ||
// node { | ||
// calculator: "ResourceProviderCalculator" | ||
// output_side_packet: "RESOURCE:0:resource0" | ||
// output_side_packet: "RESOURCE:1:resource1" | ||
// node_options { | ||
// [type.googleapis.com/mediapipe.ResourceProviderCalculatorOptions]: { | ||
// resource_id: "path/to/resource0" | ||
// resource_id: "path/to/resource1" | ||
// } | ||
// } | ||
// } | ||
// | ||
// node { | ||
// calculator: "ResourceProviderCalculator" | ||
// input_side_packet: "RESOURCE_ID:resource_id" | ||
// output_side_packet: "RESOURCE:resource" | ||
// } | ||
// | ||
// node { | ||
// calculator: "ResourceProviderCalculator" | ||
// input_side_packet: "RESOURCE_ID:0:resource_id0" | ||
// input_side_packet: "RESOURCE_ID:1:resource_id1" | ||
// ... | ||
// output_side_packet: "RESOURCE:0:resource0" | ||
// output_side_packet: "RESOURCE:1:resource1" | ||
// ... | ||
// } | ||
// | ||
class ResourceProviderCalculator : public mediapipe::api2::Node { | ||
public: | ||
static constexpr api2::SideInput<std::string>::Multiple kIds{"RESOURCE_ID"}; | ||
static constexpr api2::SideOutput<Resource>::Multiple kResources{"RESOURCE"}; | ||
|
||
MEDIAPIPE_NODE_INTERFACE(ResourceProviderCalculator, kIds, kResources); | ||
|
||
static absl::Status UpdateContract(CalculatorContext* cc); | ||
|
||
absl::Status Open(CalculatorContext* cc) override; | ||
|
||
absl::Status Process(CalculatorContext* cc) override { | ||
return absl::OkStatus(); | ||
} | ||
}; | ||
|
||
} // namespace mediapipe::api2 | ||
|
||
#endif // MEDIAPIPE_CALCULATORS_UTIL_RESOURCE_PROVIDER_CALCULATOR_H_ |
34 changes: 34 additions & 0 deletions
34
mediapipe/calculators/util/resource_provider_calculator.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2024 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. | ||
|
||
syntax = "proto3"; | ||
|
||
package mediapipe; | ||
|
||
message ResourceProviderCalculatorOptions { | ||
// The calculator will use `resource_id` to load and provide the resource | ||
// through `CalculatorContext::GetResources`. | ||
// | ||
// Resource ID can be a file path, but is not limited to just that. (Clients | ||
// can override resource loading through `kResourcesService`) | ||
repeated string resource_id = 1; | ||
|
||
enum ReadMode { | ||
READ_MODE_UNDEFINED = 0; // Defaults to BINARY | ||
READ_MODE_BINARY = 1; | ||
READ_MODE_TEXT = 2; | ||
} | ||
|
||
ReadMode read_mode = 2; | ||
} |
Oops, something went wrong.