Changed order of methods in MPPImageSegmenter.mm
This commit is contained in:
		
							parent
							
								
									ec87f068c1
								
							
						
					
					
						commit
						9f01540191
					
				| 
						 | 
					@ -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,21 +143,9 @@ 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];
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -217,7 +156,7 @@ 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,7 +169,7 @@ 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];
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -245,7 +184,7 @@ 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
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user