Made live stream mode delegate truly optional and delegate method required in iOS image classifier
This commit is contained in:
		
							parent
							
								
									d32e63297a
								
							
						
					
					
						commit
						62df48eefd
					
				| 
						 | 
				
			
			@ -30,10 +30,10 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
 | 
			
		|||
static NSString *const kLiveStreamTestsDictImageClassifierKey = @"image_classifier";
 | 
			
		||||
static NSString *const kLiveStreamTestsDictExpectationKey = @"expectation";
 | 
			
		||||
 | 
			
		||||
#define AssertEqualErrors(error, expectedError)                                               \
 | 
			
		||||
  XCTAssertNotNil(error);                                                                     \
 | 
			
		||||
  XCTAssertEqualObjects(error.domain, expectedError.domain);                                  \
 | 
			
		||||
  XCTAssertEqual(error.code, expectedError.code);                                             \
 | 
			
		||||
#define AssertEqualErrors(error, expectedError)              \
 | 
			
		||||
  XCTAssertNotNil(error);                                    \
 | 
			
		||||
  XCTAssertEqualObjects(error.domain, expectedError.domain); \
 | 
			
		||||
  XCTAssertEqual(error.code, expectedError.code);            \
 | 
			
		||||
  XCTAssertEqualObjects(error.localizedDescription, expectedError.localizedDescription)
 | 
			
		||||
 | 
			
		||||
#define AssertEqualCategoryArrays(categories, expectedCategories)                         \
 | 
			
		||||
| 
						 | 
				
			
			@ -439,7 +439,7 @@ static NSString *const kLiveStreamTestsDictExpectationKey = @"expectation";
 | 
			
		|||
 | 
			
		||||
#pragma mark Running Mode Tests
 | 
			
		||||
 | 
			
		||||
- (void)testCreateImageClassifierFailsWithDelegateInNonLiveStreamMode {
 | 
			
		||||
- (void)testCreateImageClassifierSucceedsWithDelegateInNonLiveStreamMode {
 | 
			
		||||
  MPPRunningMode runningModesToTest[] = {MPPRunningModeImage, MPPRunningModeVideo};
 | 
			
		||||
  for (int i = 0; i < sizeof(runningModesToTest) / sizeof(runningModesToTest[0]); i++) {
 | 
			
		||||
    MPPImageClassifierOptions *options = [self imageClassifierOptionsWithModelName:kFloatModelName];
 | 
			
		||||
| 
						 | 
				
			
			@ -447,36 +447,20 @@ static NSString *const kLiveStreamTestsDictExpectationKey = @"expectation";
 | 
			
		|||
    options.runningMode = runningModesToTest[i];
 | 
			
		||||
    options.imageClassifierLiveStreamDelegate = self;
 | 
			
		||||
 | 
			
		||||
    [self
 | 
			
		||||
        assertCreateImageClassifierWithOptions:options
 | 
			
		||||
                        failsWithExpectedError:
 | 
			
		||||
                            [NSError
 | 
			
		||||
                                errorWithDomain:kExpectedErrorDomain
 | 
			
		||||
                                           code:MPPTasksErrorCodeInvalidArgumentError
 | 
			
		||||
                                       userInfo:@{
 | 
			
		||||
                                         NSLocalizedDescriptionKey :
 | 
			
		||||
                                             @"The vision task is in image or video mode. The "
 | 
			
		||||
                                             @"delegate must not be set in the task's options."
 | 
			
		||||
                                       }]];
 | 
			
		||||
    MPPImageClassifier *imageClassifier = [[MPPImageClassifier alloc] initWithOptions:options
 | 
			
		||||
                                                                                error:nil];
 | 
			
		||||
    XCTAssertNotNil(imageClassifier);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
- (void)testCreateImageClassifierFailsWithMissingDelegateInLiveStreamMode {
 | 
			
		||||
- (void)testCreateImageClassifieSucceedsWithMissingDelegateInLiveStreamMode {
 | 
			
		||||
  MPPImageClassifierOptions *options = [self imageClassifierOptionsWithModelName:kFloatModelName];
 | 
			
		||||
 | 
			
		||||
  options.runningMode = MPPRunningModeLiveStream;
 | 
			
		||||
 | 
			
		||||
  [self assertCreateImageClassifierWithOptions:options
 | 
			
		||||
                        failsWithExpectedError:
 | 
			
		||||
                            [NSError errorWithDomain:kExpectedErrorDomain
 | 
			
		||||
                                                code:MPPTasksErrorCodeInvalidArgumentError
 | 
			
		||||
                                            userInfo:@{
 | 
			
		||||
                                              NSLocalizedDescriptionKey :
 | 
			
		||||
                                                  @"The vision task is in live stream mode. An "
 | 
			
		||||
                                                  @"object must be set as the delegate of the task "
 | 
			
		||||
                                                  @"in its options to ensure asynchronous delivery "
 | 
			
		||||
                                                  @"of results."
 | 
			
		||||
                                            }]];
 | 
			
		||||
  MPPImageClassifier *imageClassifier = [[MPPImageClassifier alloc] initWithOptions:options
 | 
			
		||||
                                                                              error:nil];
 | 
			
		||||
  XCTAssertNotNil(imageClassifier);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
- (void)testClassifyFailsWithCallingWrongApiInImageMode {
 | 
			
		||||
| 
						 | 
				
			
			@ -614,6 +598,30 @@ static NSString *const kLiveStreamTestsDictExpectationKey = @"expectation";
 | 
			
		|||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
- (void)testClassifyFailsWithMissingDelegateInLiveStreamMode {
 | 
			
		||||
  MPPImageClassifierOptions *options = [self imageClassifierOptionsWithModelName:kFloatModelName];
 | 
			
		||||
  options.runningMode = MPPRunningModeLiveStream;
 | 
			
		||||
 | 
			
		||||
  MPPImageClassifier *imageClassifier = [self imageClassifierWithOptionsSucceeds:options];
 | 
			
		||||
 | 
			
		||||
  NSError *expectedError =
 | 
			
		||||
      [NSError errorWithDomain:kExpectedErrorDomain
 | 
			
		||||
                          code:MPPTasksErrorCodeFailedPreconditionError
 | 
			
		||||
                      userInfo:@{
 | 
			
		||||
                        NSLocalizedDescriptionKey :
 | 
			
		||||
                            @"This method can only be called if the task is running in a stream "
 | 
			
		||||
                            @"mode and an object of a class is provided as the delegate in the "
 | 
			
		||||
                            @"task options to receive the results asynchronously."
 | 
			
		||||
                      }];
 | 
			
		||||
 | 
			
		||||
  MPPImage *image = [self imageWithFileInfo:kBurgerImage];
 | 
			
		||||
 | 
			
		||||
  NSError *error = nil;
 | 
			
		||||
  XCTAssertFalse([imageClassifier classifyAsyncImage:image timestampInMilliseconds:1 error:&error]);
 | 
			
		||||
 | 
			
		||||
  AssertEqualErrors(error, expectedError);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
- (void)testClassifyWithOutOfOrderTimestampsAndLiveStreamModeFails {
 | 
			
		||||
  MPPImageClassifierOptions *options = [self imageClassifierOptionsWithModelName:kFloatModelName];
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -64,12 +64,7 @@ static const int kMicroSecondsPerMilliSecond = 1000;
 | 
			
		|||
@implementation MPPImageClassifier
 | 
			
		||||
 | 
			
		||||
- (void)processLiveStreamResult:(absl::StatusOr<PacketMap>)liveStreamResult {
 | 
			
		||||
  if (![self.imageClassifierLiveStreamDelegate
 | 
			
		||||
          respondsToSelector:@selector
 | 
			
		||||
          (imageClassifier:didFinishClassificationWithResult:timestampInMilliseconds:error:)]) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
  NSError *callbackError = nil;
 | 
			
		||||
  if (![MPPCommonUtils checkCppError:liveStreamResult.status() toError:&callbackError]) {
 | 
			
		||||
    dispatch_async(_callbackQueue, ^{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -33,7 +33,7 @@ NS_ASSUME_NONNULL_BEGIN
 | 
			
		|||
NS_SWIFT_NAME(ImageClassifierLiveStreamDelegate)
 | 
			
		||||
@protocol MPPImageClassifierLiveStreamDelegate <NSObject>
 | 
			
		||||
 | 
			
		||||
@optional
 | 
			
		||||
@required
 | 
			
		||||
/**
 | 
			
		||||
 * This method notifies a delegate that the results of asynchronous classification of
 | 
			
		||||
 * an image submitted to the `MPPImageClassifier` is available.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user