Updated MPPObjectDetector live stream delegate
This commit is contained in:
parent
d6db8c7268
commit
afe2a08def
|
@ -60,7 +60,7 @@ static NSString *const kLiveStreamTestsDictExpectationKey = @"expectation";
|
||||||
XCTAssertEqualWithAccuracy(boundingBox.size.height, expectedBoundingBox.size.height, \
|
XCTAssertEqualWithAccuracy(boundingBox.size.height, expectedBoundingBox.size.height, \
|
||||||
pixelDifferenceTolerance, @"index i = %d", idx);
|
pixelDifferenceTolerance, @"index i = %d", idx);
|
||||||
|
|
||||||
@interface MPPObjectDetectorTests : XCTestCase <MPPObjectDetectorDelegate> {
|
@interface MPPObjectDetectorTests : XCTestCase <MPPObjectDetectorLiveStreamDelegate> {
|
||||||
NSDictionary *liveStreamSucceedsTestDict;
|
NSDictionary *liveStreamSucceedsTestDict;
|
||||||
NSDictionary *outOfOrderTimestampTestDict;
|
NSDictionary *outOfOrderTimestampTestDict;
|
||||||
}
|
}
|
||||||
|
@ -457,7 +457,7 @@ static NSString *const kLiveStreamTestsDictExpectationKey = @"expectation";
|
||||||
MPPObjectDetectorOptions *options = [self objectDetectorOptionsWithModelName:kModelName];
|
MPPObjectDetectorOptions *options = [self objectDetectorOptionsWithModelName:kModelName];
|
||||||
|
|
||||||
options.runningMode = runningModesToTest[i];
|
options.runningMode = runningModesToTest[i];
|
||||||
options.objectDetectorDelegate = self;
|
options.objectDetectorLiveStreamDelegate = self;
|
||||||
|
|
||||||
[self
|
[self
|
||||||
assertCreateObjectDetectorWithOptions:options
|
assertCreateObjectDetectorWithOptions:options
|
||||||
|
@ -568,7 +568,7 @@ static NSString *const kLiveStreamTestsDictExpectationKey = @"expectation";
|
||||||
MPPObjectDetectorOptions *options = [self objectDetectorOptionsWithModelName:kModelName];
|
MPPObjectDetectorOptions *options = [self objectDetectorOptionsWithModelName:kModelName];
|
||||||
|
|
||||||
options.runningMode = MPPRunningModeLiveStream;
|
options.runningMode = MPPRunningModeLiveStream;
|
||||||
options.objectDetectorDelegate = self;
|
options.objectDetectorLiveStreamDelegate = self;
|
||||||
|
|
||||||
MPPObjectDetector *objectDetector = [self objectDetectorWithOptionsSucceeds:options];
|
MPPObjectDetector *objectDetector = [self objectDetectorWithOptionsSucceeds:options];
|
||||||
|
|
||||||
|
@ -633,7 +633,7 @@ static NSString *const kLiveStreamTestsDictExpectationKey = @"expectation";
|
||||||
options.maxResults = maxResults;
|
options.maxResults = maxResults;
|
||||||
|
|
||||||
options.runningMode = MPPRunningModeLiveStream;
|
options.runningMode = MPPRunningModeLiveStream;
|
||||||
options.objectDetectorDelegate = self;
|
options.objectDetectorLiveStreamDelegate = self;
|
||||||
|
|
||||||
XCTestExpectation *expectation = [[XCTestExpectation alloc]
|
XCTestExpectation *expectation = [[XCTestExpectation alloc]
|
||||||
initWithDescription:@"detectWithOutOfOrderTimestampsAndLiveStream"];
|
initWithDescription:@"detectWithOutOfOrderTimestampsAndLiveStream"];
|
||||||
|
@ -689,7 +689,7 @@ static NSString *const kLiveStreamTestsDictExpectationKey = @"expectation";
|
||||||
expectation.expectedFulfillmentCount = iterationCount + 1;
|
expectation.expectedFulfillmentCount = iterationCount + 1;
|
||||||
expectation.inverted = YES;
|
expectation.inverted = YES;
|
||||||
|
|
||||||
options.objectDetectorDelegate = self;
|
options.objectDetectorLiveStreamDelegate = self;
|
||||||
|
|
||||||
MPPObjectDetector *objectDetector = [self objectDetectorWithOptionsSucceeds:options];
|
MPPObjectDetector *objectDetector = [self objectDetectorWithOptionsSucceeds:options];
|
||||||
|
|
||||||
|
@ -710,7 +710,7 @@ static NSString *const kLiveStreamTestsDictExpectationKey = @"expectation";
|
||||||
[self waitForExpectations:@[ expectation ] timeout:0.5];
|
[self waitForExpectations:@[ expectation ] timeout:0.5];
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark MPPObjectDetectorDelegate Methods
|
#pragma mark MPPObjectDetectorLiveStreamDelegate Methods
|
||||||
- (void)objectDetector:(MPPObjectDetector *)objectDetector
|
- (void)objectDetector:(MPPObjectDetector *)objectDetector
|
||||||
didFinishDetectionWithResult:(MPPObjectDetectionResult *)objectDetectionResult
|
didFinishDetectionWithResult:(MPPObjectDetectionResult *)objectDetectionResult
|
||||||
timestampInMilliseconds:(NSInteger)timestampInMilliseconds
|
timestampInMilliseconds:(NSInteger)timestampInMilliseconds
|
||||||
|
|
|
@ -138,7 +138,7 @@ NS_SWIFT_NAME(ObjectDetector)
|
||||||
* `MPPRunningModeLiveStream`. Results are provided asynchronously via the `completion` callback
|
* `MPPRunningModeLiveStream`. Results are provided asynchronously via the `completion` callback
|
||||||
* provided in the `MPPObjectDetectorOptions`.
|
* provided in the `MPPObjectDetectorOptions`.
|
||||||
* The object which needs to be continuously notified of the available results of object
|
* The object which needs to be continuously notified of the available results of object
|
||||||
* detection must confirm to `MPPObjectDetectorDelegate` protocol and implement the
|
* detection must confirm to `MPPObjectDetectorLiveStreamDelegate` protocol and implement the
|
||||||
* `objectDetector:didFinishDetectionWithResult:timestampInMilliseconds:error:` delegate method.
|
* `objectDetector:didFinishDetectionWithResult:timestampInMilliseconds:error:` delegate method.
|
||||||
*
|
*
|
||||||
* It's required to provide a timestamp (in milliseconds) to indicate when the input image is sent
|
* It's required to provide a timestamp (in milliseconds) to indicate when the input image is sent
|
||||||
|
|
|
@ -51,14 +51,13 @@ static NSString *const kTaskName = @"objectDetector";
|
||||||
/** iOS Vision Task Runner */
|
/** iOS Vision Task Runner */
|
||||||
MPPVisionTaskRunner *_visionTaskRunner;
|
MPPVisionTaskRunner *_visionTaskRunner;
|
||||||
}
|
}
|
||||||
@property(nonatomic, weak) id<MPPObjectDetectorDelegate> objectDetectorDelegate;
|
@property(nonatomic, weak) id<MPPObjectDetectorLiveStreamDelegate> objectDetectorLiveStreamDelegate;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation MPPObjectDetector
|
@implementation MPPObjectDetector
|
||||||
|
|
||||||
- (instancetype)initWithOptions:(MPPObjectDetectorOptions *)options error:(NSError **)error {
|
- (instancetype)initWithOptions:(MPPObjectDetectorOptions *)options error:(NSError **)error {
|
||||||
self = [super init];
|
self = [super init];
|
||||||
NSLog(@"Object Detector Initializing with dispatch queu and weak self");
|
|
||||||
if (self) {
|
if (self) {
|
||||||
MPPTaskInfo *taskInfo = [[MPPTaskInfo alloc]
|
MPPTaskInfo *taskInfo = [[MPPTaskInfo alloc]
|
||||||
initWithTaskGraphName:kTaskGraphName
|
initWithTaskGraphName:kTaskGraphName
|
||||||
|
@ -80,19 +79,19 @@ static NSString *const kTaskName = @"objectDetector";
|
||||||
|
|
||||||
PacketsCallback packetsCallback = nullptr;
|
PacketsCallback packetsCallback = nullptr;
|
||||||
|
|
||||||
if (options.objectDetectorDelegate) {
|
if (options.objectDetectorLiveStreamDelegate) {
|
||||||
_objectDetectorDelegate = options.objectDetectorDelegate;
|
_objectDetectorLiveStreamDelegate = options.objectDetectorLiveStreamDelegate;
|
||||||
|
|
||||||
// Capturing `self` as weak in order to avoid `self` being kept in memory
|
// Capturing `self` as weak in order to avoid `self` being kept in memory
|
||||||
// and cause a retain cycle, after self is set to `nil`.
|
// and cause a retain cycle, after self is set to `nil`.
|
||||||
MPPObjectDetector *__weak weakSelf = self;
|
MPPObjectDetector *__weak weakSelf = self;
|
||||||
dispatch_queue_t callbackQueue =
|
dispatch_queue_t callbackQueue =
|
||||||
dispatch_queue_create([MPPVisionTaskRunner uniqueQueueNameWithTaskName:kTaskName], NULL);
|
dispatch_queue_create([MPPVisionTaskRunner uniqueDispatchQueueNameWithSuffix:kTaskName], NULL);
|
||||||
packetsCallback = [=](absl::StatusOr<PacketMap> statusOrPackets) {
|
packetsCallback = [=](absl::StatusOr<PacketMap> statusOrPackets) {
|
||||||
if (!weakSelf) {
|
if (!weakSelf) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (![weakSelf.objectDetectorDelegate
|
if (![weakSelf.objectDetectorLiveStreamDelegate
|
||||||
respondsToSelector:@selector
|
respondsToSelector:@selector
|
||||||
(objectDetector:didFinishDetectionWithResult:timestampInMilliseconds:error:)]) {
|
(objectDetector:didFinishDetectionWithResult:timestampInMilliseconds:error:)]) {
|
||||||
return;
|
return;
|
||||||
|
@ -101,10 +100,10 @@ static NSString *const kTaskName = @"objectDetector";
|
||||||
NSError *callbackError = nil;
|
NSError *callbackError = nil;
|
||||||
if (![MPPCommonUtils checkCppError:statusOrPackets.status() toError:&callbackError]) {
|
if (![MPPCommonUtils checkCppError:statusOrPackets.status() toError:&callbackError]) {
|
||||||
dispatch_async(callbackQueue, ^{
|
dispatch_async(callbackQueue, ^{
|
||||||
[weakSelf.objectDetectorDelegate objectDetector:weakSelf
|
[weakSelf.objectDetectorLiveStreamDelegate objectDetector:weakSelf
|
||||||
didFinishDetectionWithResult:nil
|
didFinishDetectionWithResult:nil
|
||||||
timestampInMilliseconds:Timestamp::Unset().Value()
|
timestampInMilliseconds:Timestamp::Unset().Value()
|
||||||
error:callbackError];
|
error:callbackError];
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -122,10 +121,10 @@ static NSString *const kTaskName = @"objectDetector";
|
||||||
outputPacketMap[kImageOutStreamName.cppString].Timestamp().Value() /
|
outputPacketMap[kImageOutStreamName.cppString].Timestamp().Value() /
|
||||||
kMicroSecondsPerMilliSecond;
|
kMicroSecondsPerMilliSecond;
|
||||||
dispatch_async(callbackQueue, ^{
|
dispatch_async(callbackQueue, ^{
|
||||||
[weakSelf.objectDetectorDelegate objectDetector:weakSelf
|
[weakSelf.objectDetectorLiveStreamDelegate objectDetector:weakSelf
|
||||||
didFinishDetectionWithResult:result
|
didFinishDetectionWithResult:result
|
||||||
timestampInMilliseconds:timeStampInMilliseconds
|
timestampInMilliseconds:timeStampInMilliseconds
|
||||||
error:callbackError];
|
error:callbackError];
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,15 +23,15 @@ NS_ASSUME_NONNULL_BEGIN
|
||||||
@class MPPObjectDetector;
|
@class MPPObjectDetector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This protocol defines an interface for the delegates of `MPPImageClassifier` object to receive
|
* This protocol defines an interface for the delegates of `MPPObjectDetector` object to receive
|
||||||
* results of performing asynchronous object detection on images
|
* results of performing asynchronous object detection on images
|
||||||
* (i.e, when `runningMode` = `MPPRunningModeLiveStream`).
|
* (i.e, when `runningMode` = `MPPRunningModeLiveStream`).
|
||||||
*
|
*
|
||||||
* The delegate of `MPPImageClassifier` must adopt `MPPImageClassifierDelegate` protocol.
|
* The delegate of `MPPObjectDetector` must adopt `MPPObjectDetectorLiveStreamDelegate` protocol.
|
||||||
* The methods in this protocol are optional.
|
* The methods in this protocol are optional.
|
||||||
*/
|
*/
|
||||||
NS_SWIFT_NAME(ObjectDetectorDelegate)
|
NS_SWIFT_NAME(ObjectDetectorLiveStreamDelegate)
|
||||||
@protocol MPPObjectDetectorDelegate <NSObject>
|
@protocol MPPObjectDetectorLiveStreamDelegate <NSObject>
|
||||||
|
|
||||||
@optional
|
@optional
|
||||||
|
|
||||||
|
@ -77,12 +77,13 @@ NS_SWIFT_NAME(ObjectDetectorOptions)
|
||||||
@property(nonatomic) MPPRunningMode runningMode;
|
@property(nonatomic) MPPRunningMode runningMode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An object that confirms to `MPPObjectDetectorDelegate` protocol. This object must implement
|
* An object that confirms to `MPPObjectDetectorLiveStreamDelegate` protocol. This object must
|
||||||
* `objectDetector:didFinishDetectionWithResult:timestampInMilliseconds:error:`
|
* implement `objectDetector:didFinishDetectionWithResult:timestampInMilliseconds:error:` to receive
|
||||||
* to receive the results of performing asynchronous object detection on images (i.e, when
|
* the results of performing asynchronous object detection on images (i.e, when `runningMode` =
|
||||||
* `runningMode` = `MPPRunningModeLiveStream`).
|
* `MPPRunningModeLiveStream`).
|
||||||
*/
|
*/
|
||||||
@property(nonatomic, weak, nullable) id<MPPObjectDetectorDelegate> objectDetectorDelegate;
|
@property(nonatomic, weak, nullable) id<MPPObjectDetectorLiveStreamDelegate>
|
||||||
|
objectDetectorLiveStreamDelegate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The locale to use for display names specified through the TFLite Model Metadata, if any. Defaults
|
* The locale to use for display names specified through the TFLite Model Metadata, if any. Defaults
|
||||||
|
|
|
@ -33,7 +33,7 @@
|
||||||
objectDetectorOptions.categoryDenylist = self.categoryDenylist;
|
objectDetectorOptions.categoryDenylist = self.categoryDenylist;
|
||||||
objectDetectorOptions.categoryAllowlist = self.categoryAllowlist;
|
objectDetectorOptions.categoryAllowlist = self.categoryAllowlist;
|
||||||
objectDetectorOptions.displayNamesLocale = self.displayNamesLocale;
|
objectDetectorOptions.displayNamesLocale = self.displayNamesLocale;
|
||||||
objectDetectorOptions.objectDetectorDelegate = self.objectDetectorDelegate;
|
objectDetectorOptions.objectDetectorLiveStreamDelegate = self.objectDetectorLiveStreamDelegate;
|
||||||
|
|
||||||
return objectDetectorOptions;
|
return objectDetectorOptions;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user