From 9f015401912d418be0d65bd2bd4505a8ec0c452a Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Thu, 31 Aug 2023 18:02:34 +0530 Subject: [PATCH] Changed order of methods in MPPImageSegmenter.mm --- .../sources/MPPImageSegmenter.mm | 155 +++++++++--------- 1 file changed, 79 insertions(+), 76 deletions(-) diff --git a/mediapipe/tasks/ios/vision/image_segmenter/sources/MPPImageSegmenter.mm b/mediapipe/tasks/ios/vision/image_segmenter/sources/MPPImageSegmenter.mm index a17001fee..fc17a46cd 100644 --- a/mediapipe/tasks/ios/vision/image_segmenter/sources/MPPImageSegmenter.mm +++ b/mediapipe/tasks/ios/vision/image_segmenter/sources/MPPImageSegmenter.mm @@ -52,7 +52,7 @@ using ::mediapipe::Packet; using ::mediapipe::Timestamp; using ::mediapipe::tasks::core::PacketMap; using ::mediapipe::tasks::core::PacketsCallback; -} // anonymous namespace +} // anonymous namespace @interface MPPImageSegmenter () { /** iOS Vision Task Runner */ @@ -66,67 +66,18 @@ using ::mediapipe::tasks::core::PacketsCallback; @implementation MPPImageSegmenter -- (nullable MPPImageSegmenterResult *) - imageSegmenterResultWithOutputPacketMap:(PacketMap &)outputPacketMap - shouldCopyMaskPacketData:(BOOL)shouldCopyMaskPacketData { - return [MPPImageSegmenterResult - imageSegmenterResultWithConfidenceMasksPacket:outputPacketMap[kConfidenceMasksStreamName - .cppString] - categoryMaskPacket:outputPacketMap[kCategoryMaskStreamName - .cppString] - qualityScoresPacket:outputPacketMap[kQualityScoresStreamName - .cppString] - timestampInMilliseconds:outputPacketMap[kImageOutStreamName.cppString] - .Timestamp() - .Value() / - kMicrosecondsPerMillisecond - shouldCopyMaskPacketData:shouldCopyMaskPacketData]; -} - -- (void)processLiveStreamResult:(absl::StatusOr)liveStreamResult { - if (![self.imageSegmenterLiveStreamDelegate - respondsToSelector:@selector(imageSegmenter: - didFinishSegmentationWithResult:timestampInMilliseconds:error:)]) { - return; - } - NSError *callbackError = nil; - if (![MPPCommonUtils checkCppError:liveStreamResult.status() toError:&callbackError]) { - dispatch_async(_callbackQueue, ^{ - [self.imageSegmenterLiveStreamDelegate imageSegmenter:self - didFinishSegmentationWithResult:nil - timestampInMilliseconds:Timestamp::Unset().Value() - error:callbackError]; - }); - return; - } - - PacketMap &outputPacketMap = liveStreamResult.value(); - if (outputPacketMap[kImageOutStreamName.cppString].IsEmpty()) { - return; - } - - MPPImageSegmenterResult *result = [self imageSegmenterResultWithOutputPacketMap:outputPacketMap - shouldCopyMaskPacketData:NO]; - - dispatch_async(_callbackQueue, ^{ - [self.imageSegmenterLiveStreamDelegate imageSegmenter:self - didFinishSegmentationWithResult:result - timestampInMilliseconds:result.timestampInMilliseconds - error:callbackError]; - }); -} +#pragma mark - Public - (instancetype)initWithOptions:(MPPImageSegmenterOptions *)options error:(NSError **)error { self = [super init]; if (self) { NSMutableArray *outputStreams = [NSMutableArray - arrayWithObjects:[NSString - stringWithFormat:@"%@:%@", kQualityScoresTag, kQualityScoresStreamName], - [NSString stringWithFormat:@"%@:%@", kImageTag, kImageOutStreamName], - nil]; + arrayWithObjects:[NSString stringWithFormat:@"%@:%@", kQualityScoresTag, + kQualityScoresStreamName], + [NSString stringWithFormat:@"%@:%@", kImageTag, kImageOutStreamName], nil]; if (options.shouldOutputConfidenceMasks) { - [outputStreams addObject:[NSString - stringWithFormat:@"%@:%@", kConfidenceMasksTag, kConfidenceMasksStreamName]]; + [outputStreams addObject:[NSString stringWithFormat:@"%@:%@", kConfidenceMasksTag, + kConfidenceMasksStreamName]]; } if (options.shouldOutputCategoryMask) { [outputStreams addObject:[NSString stringWithFormat:@"%@:%@", kCategoryMaskTag, @@ -192,22 +143,10 @@ using ::mediapipe::tasks::core::PacketsCallback; return [self initWithOptions:options error:error]; } -- (nullable MPPImageSegmenterResult *) - imageSegmenterResultWithOptionalOutputPacketMap:(std::optional &)outputPacketMap - shouldCopyMaskPacketData:(BOOL)shouldCopyMaskPacketData { - if (!outputPacketMap.has_value()) { - return nil; - } - MPPImageSegmenterResult *result = - [self imageSegmenterResultWithOutputPacketMap:outputPacketMap.value() - shouldCopyMaskPacketData:shouldCopyMaskPacketData]; - return result; -} - - (nullable MPPImageSegmenterResult *)segmentImage:(MPPImage *)image error:(NSError **)error { std::optional outputPacketMap = [_visionTaskRunner processImage:image error:error]; - return [self imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap - shouldCopyMaskPacketData:YES]; + return [MPPImageSegmenter imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap + shouldCopyMaskPacketData:YES]; } - (void)segmentImage:(MPPImage *)image @@ -217,8 +156,8 @@ using ::mediapipe::tasks::core::PacketsCallback; std::optional outputPacketMap = [_visionTaskRunner processImage:image error:&error]; MPPImageSegmenterResult *result = - [self imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap - shouldCopyMaskPacketData:NO]; + [MPPImageSegmenter imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap + shouldCopyMaskPacketData:NO]; completionHandler(result, error); } @@ -230,8 +169,8 @@ using ::mediapipe::tasks::core::PacketsCallback; timestampInMilliseconds:timestampInMilliseconds error:error]; - return [self imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap - shouldCopyMaskPacketData:YES]; + return [MPPImageSegmenter imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap + shouldCopyMaskPacketData:YES]; } - (void)segmentVideoFrame:(MPPImage *)image @@ -245,8 +184,8 @@ using ::mediapipe::tasks::core::PacketsCallback; error:&error]; MPPImageSegmenterResult *result = - [self imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap - shouldCopyMaskPacketData:NO]; + [MPPImageSegmenter imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap + shouldCopyMaskPacketData:NO]; completionHandler(result, error); } @@ -258,4 +197,68 @@ using ::mediapipe::tasks::core::PacketsCallback; error:error]; } +#pragma mark - Private + ++ (nullable MPPImageSegmenterResult *) + imageSegmenterResultWithOptionalOutputPacketMap:(std::optional &)outputPacketMap + shouldCopyMaskPacketData:(BOOL)shouldCopyMaskPacketData { + if (!outputPacketMap.has_value()) { + return nil; + } + MPPImageSegmenterResult *result = + [self imageSegmenterResultWithOutputPacketMap:outputPacketMap.value() + shouldCopyMaskPacketData:shouldCopyMaskPacketData]; + return result; +} + ++ (nullable MPPImageSegmenterResult *) + imageSegmenterResultWithOutputPacketMap:(PacketMap &)outputPacketMap + shouldCopyMaskPacketData:(BOOL)shouldCopyMaskPacketData { + return [MPPImageSegmenterResult + imageSegmenterResultWithConfidenceMasksPacket:outputPacketMap[kConfidenceMasksStreamName + .cppString] + categoryMaskPacket:outputPacketMap[kCategoryMaskStreamName + .cppString] + qualityScoresPacket:outputPacketMap[kQualityScoresStreamName + .cppString] + timestampInMilliseconds:outputPacketMap[kImageOutStreamName.cppString] + .Timestamp() + .Value() / + kMicrosecondsPerMillisecond + shouldCopyMaskPacketData:shouldCopyMaskPacketData]; +} + +- (void)processLiveStreamResult:(absl::StatusOr)liveStreamResult { + if (![self.imageSegmenterLiveStreamDelegate + respondsToSelector:@selector(imageSegmenter: + didFinishSegmentationWithResult:timestampInMilliseconds:error:)]) { + return; + } + NSError *callbackError = nil; + if (![MPPCommonUtils checkCppError:liveStreamResult.status() toError:&callbackError]) { + dispatch_async(_callbackQueue, ^{ + [self.imageSegmenterLiveStreamDelegate imageSegmenter:self + didFinishSegmentationWithResult:nil + timestampInMilliseconds:Timestamp::Unset().Value() + error:callbackError]; + }); + return; + } + + PacketMap &outputPacketMap = liveStreamResult.value(); + if (outputPacketMap[kImageOutStreamName.cppString].IsEmpty()) { + return; + } + + MPPImageSegmenterResult *result = [self imageSegmenterResultWithOutputPacketMap:outputPacketMap + shouldCopyMaskPacketData:NO]; + + dispatch_async(_callbackQueue, ^{ + [self.imageSegmenterLiveStreamDelegate imageSegmenter:self + didFinishSegmentationWithResult:result + timestampInMilliseconds:result.timestampInMilliseconds + error:callbackError]; + }); +} + @end