From 58772a73fc360fbd8a050471073ba2e8a047d31a Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Thu, 25 May 2023 19:56:29 +0530 Subject: [PATCH] Added a method for processing live stream results in callback for iOS gesture recognizer --- .../utils/sources/MPPCategory+Helpers.mm | 2 +- .../sources/MPPGestureRecognizer.mm | 84 +++++++++---------- 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/mediapipe/tasks/ios/components/containers/utils/sources/MPPCategory+Helpers.mm b/mediapipe/tasks/ios/components/containers/utils/sources/MPPCategory+Helpers.mm index 84d45eb3f..542b8c41d 100644 --- a/mediapipe/tasks/ios/components/containers/utils/sources/MPPCategory+Helpers.mm +++ b/mediapipe/tasks/ios/components/containers/utils/sources/MPPCategory+Helpers.mm @@ -41,7 +41,7 @@ using ClassificationProto = ::mediapipe::Classification; } + (MPPCategory *)categoryWithProto:(const ClassificationProto &)classificationProto { - return [MPPCategory categoryWithProto:classificationProto andIndex:classificationProto.index()]; + return [MPPCategory categoryWithProto:classificationProto index:classificationProto.index()]; } @end diff --git a/mediapipe/tasks/ios/vision/gesture_recognizer/sources/MPPGestureRecognizer.mm b/mediapipe/tasks/ios/vision/gesture_recognizer/sources/MPPGestureRecognizer.mm index ddb49a45f..b6acc05e8 100644 --- a/mediapipe/tasks/ios/vision/gesture_recognizer/sources/MPPGestureRecognizer.mm +++ b/mediapipe/tasks/ios/vision/gesture_recognizer/sources/MPPGestureRecognizer.mm @@ -57,6 +57,7 @@ static NSString *const kTaskName = @"gestureRecognizer"; @interface MPPGestureRecognizer () { /** iOS Vision Task Runner */ MPPVisionTaskRunner *_visionTaskRunner; + dispatch_queue_t _callbackQueue; } @property(nonatomic, weak) id gestureRecognizerLiveStreamDelegate; @@ -77,6 +78,44 @@ static NSString *const kTaskName = @"gestureRecognizer"; .cppString]]; } +- (void)processLiveStreamResult:(absl::StatusOr)liveStreamResult { + MPPGestureRecognizer *__weak weakSelf = self; + if (![weakSelf.gestureRecognizerLiveStreamDelegate + respondsToSelector:@selector(gestureRecognizer: + didFinishRecognitionWithResult:timestampInMilliseconds:error:)]) { + return; + } + + NSError *callbackError = nil; + if (![MPPCommonUtils checkCppError:liveStreamResult.status() toError:&callbackError]) { + dispatch_async(_callbackQueue, ^{ + [weakSelf.gestureRecognizerLiveStreamDelegate gestureRecognizer:weakSelf + didFinishRecognitionWithResult:nil + timestampInMilliseconds:Timestamp::Unset().Value() + error:callbackError]; + }); + return; + } + + PacketMap &outputPacketMap = liveStreamResult.value(); + if (outputPacketMap[kImageOutStreamName.cppString].IsEmpty()) { + return; + } + + MPPGestureRecognizerResult *result = + [weakSelf gestureRecognizerResultWithOutputPacketMap:outputPacketMap]; + + NSInteger timeStampInMilliseconds = + outputPacketMap[kImageOutStreamName.cppString].Timestamp().Value() / + kMicroSecondsPerMilliSecond; + dispatch_async(_callbackQueue, ^{ + [weakSelf.gestureRecognizerLiveStreamDelegate gestureRecognizer:weakSelf + didFinishRecognitionWithResult:result + timestampInMilliseconds:timeStampInMilliseconds + error:callbackError]; + }); +} + - (instancetype)initWithOptions:(MPPGestureRecognizerOptions *)options error:(NSError **)error { self = [super init]; if (self) { @@ -116,47 +155,9 @@ static NSString *const kTaskName = @"gestureRecognizer"; // the delegate method, the queue on which the C++ callbacks is invoked is not blocked and is // freed up to continue with its operations. const char *queueName = [MPPVisionTaskRunner uniqueDispatchQueueNameWithSuffix:kTaskName]; - dispatch_queue_t callbackQueue = dispatch_queue_create(queueName, NULL); - packetsCallback = [=](absl::StatusOr status_or_packets) { - if (!weakSelf) { - return; - } - if (![weakSelf.gestureRecognizerLiveStreamDelegate - respondsToSelector:@selector - (gestureRecognizer: - didFinishRecognitionWithResult:timestampInMilliseconds:error:)]) { - return; - } - - NSError *callbackError = nil; - if (![MPPCommonUtils checkCppError:status_or_packets.status() toError:&callbackError]) { - dispatch_async(callbackQueue, ^{ - [weakSelf.gestureRecognizerLiveStreamDelegate - gestureRecognizer:weakSelf - didFinishRecognitionWithResult:nil - timestampInMilliseconds:Timestamp::Unset().Value() - error:callbackError]; - }); - return; - } - - PacketMap &outputPacketMap = status_or_packets.value(); - if (outputPacketMap[kImageOutStreamName.cppString].IsEmpty()) { - return; - } - - MPPGestureRecognizerResult *result = - [weakSelf gestureRecognizerResultWithOutputPacketMap:outputPacketMap]; - - NSInteger timeStampInMilliseconds = - outputPacketMap[kImageOutStreamName.cppString].Timestamp().Value() / - kMicroSecondsPerMilliSecond; - dispatch_async(callbackQueue, ^{ - [weakSelf.gestureRecognizerLiveStreamDelegate gestureRecognizer:weakSelf - didFinishRecognitionWithResult:result - timestampInMilliseconds:timeStampInMilliseconds - error:callbackError]; - }); + _callbackQueue = dispatch_queue_create(queueName, NULL); + packetsCallback = [=](absl::StatusOr liveStreamResult) { + [weakSelf processLiveStreamResult:liveStreamResult]; }; } @@ -165,7 +166,6 @@ static NSString *const kTaskName = @"gestureRecognizer"; runningMode:options.runningMode packetsCallback:std::move(packetsCallback) error:error]; - if (!_visionTaskRunner) { return nil; }