Changed order of methods in MPPImageSegmenter.mm

This commit is contained in:
Prianka Liz Kariat 2023-08-31 18:02:34 +05:30
parent ec87f068c1
commit 9f01540191

View File

@ -52,7 +52,7 @@ using ::mediapipe::Packet;
using ::mediapipe::Timestamp; using ::mediapipe::Timestamp;
using ::mediapipe::tasks::core::PacketMap; using ::mediapipe::tasks::core::PacketMap;
using ::mediapipe::tasks::core::PacketsCallback; using ::mediapipe::tasks::core::PacketsCallback;
} // anonymous namespace } // anonymous namespace
@interface MPPImageSegmenter () { @interface MPPImageSegmenter () {
/** iOS Vision Task Runner */ /** iOS Vision Task Runner */
@ -66,67 +66,18 @@ using ::mediapipe::tasks::core::PacketsCallback;
@implementation MPPImageSegmenter @implementation MPPImageSegmenter
- (nullable MPPImageSegmenterResult *) #pragma mark - Public
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<PacketMap>)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];
});
}
- (instancetype)initWithOptions:(MPPImageSegmenterOptions *)options error:(NSError **)error { - (instancetype)initWithOptions:(MPPImageSegmenterOptions *)options error:(NSError **)error {
self = [super init]; self = [super init];
if (self) { if (self) {
NSMutableArray<NSString *> *outputStreams = [NSMutableArray NSMutableArray<NSString *> *outputStreams = [NSMutableArray
arrayWithObjects:[NSString arrayWithObjects:[NSString stringWithFormat:@"%@:%@", kQualityScoresTag,
stringWithFormat:@"%@:%@", kQualityScoresTag, kQualityScoresStreamName], kQualityScoresStreamName],
[NSString stringWithFormat:@"%@:%@", kImageTag, kImageOutStreamName], [NSString stringWithFormat:@"%@:%@", kImageTag, kImageOutStreamName], nil];
nil];
if (options.shouldOutputConfidenceMasks) { if (options.shouldOutputConfidenceMasks) {
[outputStreams addObject:[NSString [outputStreams addObject:[NSString stringWithFormat:@"%@:%@", kConfidenceMasksTag,
stringWithFormat:@"%@:%@", kConfidenceMasksTag, kConfidenceMasksStreamName]]; kConfidenceMasksStreamName]];
} }
if (options.shouldOutputCategoryMask) { if (options.shouldOutputCategoryMask) {
[outputStreams addObject:[NSString stringWithFormat:@"%@:%@", kCategoryMaskTag, [outputStreams addObject:[NSString stringWithFormat:@"%@:%@", kCategoryMaskTag,
@ -192,22 +143,10 @@ using ::mediapipe::tasks::core::PacketsCallback;
return [self initWithOptions:options error:error]; return [self initWithOptions:options error:error];
} }
- (nullable MPPImageSegmenterResult *)
imageSegmenterResultWithOptionalOutputPacketMap:(std::optional<PacketMap> &)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 { - (nullable MPPImageSegmenterResult *)segmentImage:(MPPImage *)image error:(NSError **)error {
std::optional<PacketMap> outputPacketMap = [_visionTaskRunner processImage:image error:error]; std::optional<PacketMap> outputPacketMap = [_visionTaskRunner processImage:image error:error];
return [self imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap return [MPPImageSegmenter imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap
shouldCopyMaskPacketData:YES]; shouldCopyMaskPacketData:YES];
} }
- (void)segmentImage:(MPPImage *)image - (void)segmentImage:(MPPImage *)image
@ -217,8 +156,8 @@ using ::mediapipe::tasks::core::PacketsCallback;
std::optional<PacketMap> outputPacketMap = [_visionTaskRunner processImage:image error:&error]; std::optional<PacketMap> outputPacketMap = [_visionTaskRunner processImage:image error:&error];
MPPImageSegmenterResult *result = MPPImageSegmenterResult *result =
[self imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap [MPPImageSegmenter imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap
shouldCopyMaskPacketData:NO]; shouldCopyMaskPacketData:NO];
completionHandler(result, error); completionHandler(result, error);
} }
@ -230,8 +169,8 @@ using ::mediapipe::tasks::core::PacketsCallback;
timestampInMilliseconds:timestampInMilliseconds timestampInMilliseconds:timestampInMilliseconds
error:error]; error:error];
return [self imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap return [MPPImageSegmenter imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap
shouldCopyMaskPacketData:YES]; shouldCopyMaskPacketData:YES];
} }
- (void)segmentVideoFrame:(MPPImage *)image - (void)segmentVideoFrame:(MPPImage *)image
@ -245,8 +184,8 @@ using ::mediapipe::tasks::core::PacketsCallback;
error:&error]; error:&error];
MPPImageSegmenterResult *result = MPPImageSegmenterResult *result =
[self imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap [MPPImageSegmenter imageSegmenterResultWithOptionalOutputPacketMap:outputPacketMap
shouldCopyMaskPacketData:NO]; shouldCopyMaskPacketData:NO];
completionHandler(result, error); completionHandler(result, error);
} }
@ -258,4 +197,68 @@ using ::mediapipe::tasks::core::PacketsCallback;
error:error]; error:error];
} }
#pragma mark - Private
+ (nullable MPPImageSegmenterResult *)
imageSegmenterResultWithOptionalOutputPacketMap:(std::optional<PacketMap> &)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<PacketMap>)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 @end