Added packets callback provision and built in op resolver in MPPTaskRunner

This commit is contained in:
Prianka Liz Kariat 2023-01-05 18:31:56 +05:30
parent dfe7c83ad5
commit 8ba81de20b
3 changed files with 53 additions and 5 deletions

View File

@ -90,6 +90,8 @@ objc_library(
deps = [ deps = [
"//mediapipe/framework:calculator_cc_proto", "//mediapipe/framework:calculator_cc_proto",
"//mediapipe/tasks/cc/core:task_runner", "//mediapipe/tasks/cc/core:task_runner",
"//mediapipe/tasks/cc/core:mediapipe_builtin_op_resolver",
"//mediapipe/tasks/ios/common/utils:MPPCommonUtils", "//mediapipe/tasks/ios/common/utils:MPPCommonUtils",
"@org_tensorflow//tensorflow/lite/core/api:op_resolver",
], ],
) )

View File

@ -20,23 +20,63 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
/** /**
* This class is used to create and call appropriate methods on the C++ Task Runner. * This class is used to create and call appropriate methods on the C++ Task Runner to initialize,
* execute and terminate any Mediapipe task.
*
* An instance of the newly created C++ task runner will
* be stored until this class is destroyed. When methods are called for processing (performing
* inference), closing etc., on this class, internally the appropriate methods will be called on the
* C++ task runner instance to execute the appropriate actions. For each type of task, a subclass of
* this class must be defined to add any additional functionality. For eg:, vision tasks must create
* an `MPPVisionTaskRunner` and provide additional functionality. An instance of
* `MPPVisionTaskRunner` can in turn be used by the each vision task for creation and execution of
* the task. Please see the documentation for the C++ Task Runner for more details on how the taks
* runner operates.
*/ */
@interface MPPTaskRunner : NSObject @interface MPPTaskRunner : NSObject
/** /**
* Initializes a new `MPPTaskRunner` with the mediapipe task graph config proto. * Initializes a new `MPPTaskRunner` with the mediapipe task graph config proto and an optional C++
* packets callback.
*
* You can pass `nullptr` for `packetsCallback` in case the mode of operation
* requested by the user is synchronous.
*
* If the task is operating in asynchronous mode, any iOS Mediapipe task that uses the `MPPTaskRunner`
* must define a C++ callback function to obtain the results of inference asynchronously and deliver
* the results to the user. To accomplish this, callback function will in turn invoke the block
* provided by the user in the task options supplied to create the task.
* Please see the documentation of the C++ Task Runner for more information on the synchronous and
* asynchronous modes of operation.
* *
* @param graphConfig A mediapipe task graph config proto. * @param graphConfig A mediapipe task graph config proto.
* *
* @return An instance of `MPPTaskRunner` initialized to the given graph config proto. * @param packetsCallback An optional C++ callback function that takes a list of output packets as
* the input argument. If provided, the callback must in turn call the block provided by the user in
* the appropriate task options.
*
* @return An instance of `MPPTaskRunner` initialized to the given graph config proto and optional
* packetsCallback.
*/ */
- (instancetype)initWithCalculatorGraphConfig:(mediapipe::CalculatorGraphConfig)graphConfig - (instancetype)initWithCalculatorGraphConfig:(mediapipe::CalculatorGraphConfig)graphConfig
packetsCallback:
(mediapipe::tasks::core::PacketsCallback)packetsCallback
error:(NSError **)error NS_DESIGNATED_INITIALIZER; error:(NSError **)error NS_DESIGNATED_INITIALIZER;
/** A synchronous method for processing batch data or offline streaming data. This method is
designed for processing either batch data such as unrelated images and texts or offline streaming
data such as the decoded frames from a video file and an audio file. The call blocks the current
thread until a failure status or a successful result is returned. If the input packets have no
timestamp, an internal timestamp will be assigend per invocation. Otherwise, when the timestamp is
set in the input packets, the caller must ensure that the input packet timestamps are greater than
the timestamps of the previous invocation. This method is thread-unsafe and it is the caller's
responsibility to synchronize access to this method across multiple threads and to ensure that the
input packet timestamps are in order.*/
- (absl::StatusOr<mediapipe::tasks::core::PacketMap>)process: - (absl::StatusOr<mediapipe::tasks::core::PacketMap>)process:
(const mediapipe::tasks::core::PacketMap &)packetMap; (const mediapipe::tasks::core::PacketMap &)packetMap;
/** Shuts down the C++ task runner. After the runner is closed, any calls that send input data to
* the runner are illegal and will receive errors. */
- (absl::Status)close; - (absl::Status)close;
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;

View File

@ -13,11 +13,15 @@
// limitations under the License. // limitations under the License.
#import "mediapipe/tasks/ios/core/sources/MPPTaskRunner.h" #import "mediapipe/tasks/ios/core/sources/MPPTaskRunner.h"
#include "mediapipe/tasks/cc/core/mediapipe_builtin_op_resolver.h"
#import "mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h" #import "mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h"
#include "tensorflow/lite/core/api/op_resolver.h"
namespace { namespace {
using ::mediapipe::CalculatorGraphConfig; using ::mediapipe::CalculatorGraphConfig;
using ::mediapipe::tasks::core::MediaPipeBuiltinOpResolver;
using ::mediapipe::tasks::core::PacketMap; using ::mediapipe::tasks::core::PacketMap;
using ::mediapipe::tasks::core::PacketsCallback;
using TaskRunnerCpp = ::mediapipe::tasks::core::TaskRunner; using TaskRunnerCpp = ::mediapipe::tasks::core::TaskRunner;
} // namespace } // namespace
@ -30,15 +34,17 @@ using TaskRunnerCpp = ::mediapipe::tasks::core::TaskRunner;
@implementation MPPTaskRunner @implementation MPPTaskRunner
- (instancetype)initWithCalculatorGraphConfig:(CalculatorGraphConfig)graphConfig - (instancetype)initWithCalculatorGraphConfig:(CalculatorGraphConfig)graphConfig
packetsCallback:(PacketsCallback)packetsCallback
error:(NSError **)error { error:(NSError **)error {
self = [super init]; self = [super init];
if (self) { if (self) {
auto taskRunnerResult = TaskRunnerCpp::Create(std::move(graphConfig)); auto taskRunnerResult = TaskRunnerCpp::Create(std::move(graphConfig),
absl::make_unique<MediaPipeBuiltinOpResolver>(),
std::move(packetsCallback));
if (![MPPCommonUtils checkCppError:taskRunnerResult.status() toError:error]) { if (![MPPCommonUtils checkCppError:taskRunnerResult.status() toError:error]) {
return nil; return nil;
} }
_cppTaskRunner = std::move(taskRunnerResult.value()); _cppTaskRunner = std::move(taskRunnerResult.value());
} }
return self; return self;