Updated formatted

This commit is contained in:
Prianka Liz Kariat 2023-03-23 19:58:03 +05:30
parent da8b60700b
commit 8682a3ffd9
5 changed files with 75 additions and 60 deletions

View File

@ -20,7 +20,8 @@ NS_ASSUME_NONNULL_BEGIN
@interface MPPBaseOptions (Helpers) @interface MPPBaseOptions (Helpers)
- (void)copyToProto:(mediapipe::tasks::core::proto::BaseOptions *)baseOptionsProto; - (void)copyToProto:(mediapipe::tasks::core::proto::BaseOptions *)baseOptionsProto;
- (void)copyToProto:(mediapipe::tasks::core::proto::BaseOptions *)baseOptionsProto withUseStreamMode:(BOOL)useStreamMode; - (void)copyToProto:(mediapipe::tasks::core::proto::BaseOptions *)baseOptionsProto
withUseStreamMode:(BOOL)useStreamMode;
@end @end

View File

@ -23,7 +23,8 @@ static NSString *const kQuantizedModelName = @"mobilenet_v1_0.25_224_quant";
static NSDictionary *const kBurgerImage = @{@"name" : @"burger", @"type" : @"jpg"}; static NSDictionary *const kBurgerImage = @{@"name" : @"burger", @"type" : @"jpg"};
static NSDictionary *const kBurgerRotatedImage = @{@"name" : @"burger_rotated", @"type" : @"jpg"}; static NSDictionary *const kBurgerRotatedImage = @{@"name" : @"burger_rotated", @"type" : @"jpg"};
static NSDictionary *const kMultiObjectsImage = @{@"name" : @"multi_objects", @"type" : @"jpg"}; static NSDictionary *const kMultiObjectsImage = @{@"name" : @"multi_objects", @"type" : @"jpg"};
static NSDictionary *const kMultiObjectsRotatedImage = @{@"name" : @"multi_objects_rotated", @"type" : @"jpg"}; static NSDictionary *const kMultiObjectsRotatedImage =
@{@"name" : @"multi_objects_rotated", @"type" : @"jpg"};
static const int kMobileNetCategoriesCount = 1001; static const int kMobileNetCategoriesCount = 1001;
static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks"; static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
@ -112,7 +113,7 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
#pragma mark Assert Classify Results #pragma mark Assert Classify Results
-(MPPImage *)imageWithFileInfo:(NSDictionary *)fileInfo { - (MPPImage *)imageWithFileInfo:(NSDictionary *)fileInfo {
MPPImage *image = [MPPImage imageFromBundleWithClass:[MPPImageClassifierTests class] MPPImage *image = [MPPImage imageFromBundleWithClass:[MPPImageClassifierTests class]
fileName:fileInfo[@"name"] fileName:fileInfo[@"name"]
ofType:fileInfo[@"type"]]; ofType:fileInfo[@"type"]];
@ -121,11 +122,12 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
return image; return image;
} }
-(MPPImage *)imageWithFileInfo:(NSDictionary *)fileInfo orientation:(UIImageOrientation)orientation { - (MPPImage *)imageWithFileInfo:(NSDictionary *)fileInfo
orientation:(UIImageOrientation)orientation {
MPPImage *image = [MPPImage imageFromBundleWithClass:[MPPImageClassifierTests class] MPPImage *image = [MPPImage imageFromBundleWithClass:[MPPImageClassifierTests class]
fileName:fileInfo[@"name"] fileName:fileInfo[@"name"]
ofType:fileInfo[@"type"] ofType:fileInfo[@"type"]
orientation:orientation]; orientation:orientation];
XCTAssertNotNil(image); XCTAssertNotNil(image);
return image; return image;
@ -173,9 +175,9 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
} }
- (void)assertResultsOfClassifyImageWithFileInfo:(NSDictionary *)fileInfo - (void)assertResultsOfClassifyImageWithFileInfo:(NSDictionary *)fileInfo
usingImageClassifier:(MPPImageClassifier *)imageClassifier usingImageClassifier:(MPPImageClassifier *)imageClassifier
expectedCategoriesCount:(NSInteger)expectedCategoriesCount expectedCategoriesCount:(NSInteger)expectedCategoriesCount
equalsCategories:(NSArray<MPPCategory *> *)expectedCategories { equalsCategories:(NSArray<MPPCategory *> *)expectedCategories {
MPPImage *mppImage = [self imageWithFileInfo:fileInfo]; MPPImage *mppImage = [self imageWithFileInfo:fileInfo];
[self assertResultsOfClassifyImage:mppImage [self assertResultsOfClassifyImage:mppImage
@ -242,11 +244,13 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
- (void)testClassifyWithModelPathAndFloatModelSucceeds { - (void)testClassifyWithModelPathAndFloatModelSucceeds {
MPPImageClassifier *imageClassifier = [self imageClassifierFromModelFileWithName:kFloatModelName]; MPPImageClassifier *imageClassifier = [self imageClassifierFromModelFileWithName:kFloatModelName];
[self assertResultsOfClassifyImageWithFileInfo:kBurgerImage [self
usingImageClassifier:imageClassifier assertResultsOfClassifyImageWithFileInfo:kBurgerImage
expectedCategoriesCount:kMobileNetCategoriesCount usingImageClassifier:imageClassifier
equalsCategories:[MPPImageClassifierTests expectedCategoriesCount:kMobileNetCategoriesCount
expectedResultCategoriesForClassifyBurgerImageWithFloatModel]]; equalsCategories:
[MPPImageClassifierTests
expectedResultCategoriesForClassifyBurgerImageWithFloatModel]];
} }
- (void)testClassifyWithOptionsAndFloatModelSucceeds { - (void)testClassifyWithOptionsAndFloatModelSucceeds {
@ -257,11 +261,13 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
MPPImageClassifier *imageClassifier = [self imageClassifierWithOptionsSucceeds:options]; MPPImageClassifier *imageClassifier = [self imageClassifierWithOptionsSucceeds:options];
[self assertResultsOfClassifyImageWithFileInfo:kBurgerImage [self
usingImageClassifier:imageClassifier assertResultsOfClassifyImageWithFileInfo:kBurgerImage
expectedCategoriesCount:maxResults usingImageClassifier:imageClassifier
equalsCategories:[MPPImageClassifierTests expectedCategoriesCount:maxResults
expectedResultCategoriesForClassifyBurgerImageWithFloatModel]]; equalsCategories:
[MPPImageClassifierTests
expectedResultCategoriesForClassifyBurgerImageWithFloatModel]];
} }
- (void)testClassifyWithQuantizedModelSucceeds { - (void)testClassifyWithQuantizedModelSucceeds {
@ -279,9 +285,9 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
displayName:nil] ]; displayName:nil] ];
[self assertResultsOfClassifyImageWithFileInfo:kBurgerImage [self assertResultsOfClassifyImageWithFileInfo:kBurgerImage
usingImageClassifier:imageClassifier usingImageClassifier:imageClassifier
expectedCategoriesCount:maxResults expectedCategoriesCount:maxResults
equalsCategories:expectedCategories]; equalsCategories:expectedCategories];
} }
- (void)testClassifyWithScoreThresholdSucceeds { - (void)testClassifyWithScoreThresholdSucceeds {
@ -297,9 +303,9 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
displayName:nil] ]; displayName:nil] ];
[self assertResultsOfClassifyImageWithFileInfo:kBurgerImage [self assertResultsOfClassifyImageWithFileInfo:kBurgerImage
usingImageClassifier:imageClassifier usingImageClassifier:imageClassifier
expectedCategoriesCount:expectedCategories.count expectedCategoriesCount:expectedCategories.count
equalsCategories:expectedCategories]; equalsCategories:expectedCategories];
} }
- (void)testClassifyWithAllowListSucceeds { - (void)testClassifyWithAllowListSucceeds {
@ -326,9 +332,9 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
]; ];
[self assertResultsOfClassifyImageWithFileInfo:kBurgerImage [self assertResultsOfClassifyImageWithFileInfo:kBurgerImage
usingImageClassifier:imageClassifier usingImageClassifier:imageClassifier
expectedCategoriesCount:expectedCategories.count expectedCategoriesCount:expectedCategories.count
equalsCategories:expectedCategories]; equalsCategories:expectedCategories];
} }
- (void)testClassifyWithDenyListSucceeds { - (void)testClassifyWithDenyListSucceeds {
@ -358,9 +364,9 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
]; ];
[self assertResultsOfClassifyImageWithFileInfo:kBurgerImage [self assertResultsOfClassifyImageWithFileInfo:kBurgerImage
usingImageClassifier:imageClassifier usingImageClassifier:imageClassifier
expectedCategoriesCount:expectedCategories.count expectedCategoriesCount:expectedCategories.count
equalsCategories:expectedCategories]; equalsCategories:expectedCategories];
} }
- (void)testClassifyWithRegionOfInterestSucceeds { - (void)testClassifyWithRegionOfInterestSucceeds {
@ -377,7 +383,7 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
displayName:nil] ]; displayName:nil] ];
MPPImage *image = [self imageWithFileInfo:kMultiObjectsImage]; MPPImage *image = [self imageWithFileInfo:kMultiObjectsImage];
// roi around soccer ball // roi around soccer ball
MPPImageClassifierResult *imageClassifierResult = MPPImageClassifierResult *imageClassifierResult =
[imageClassifier classifyImage:image [imageClassifier classifyImage:image
@ -412,7 +418,8 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
]; ];
MPPImage *image = [self imageWithFileInfo:kBurgerRotatedImage orientation:UIImageOrientationRight]; MPPImage *image = [self imageWithFileInfo:kBurgerRotatedImage
orientation:UIImageOrientationRight];
[self assertResultsOfClassifyImage:image [self assertResultsOfClassifyImage:image
usingImageClassifier:imageClassifier usingImageClassifier:imageClassifier
@ -434,7 +441,8 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
categoryName:@"folding chair" categoryName:@"folding chair"
displayName:nil] ]; displayName:nil] ];
MPPImage *image = [self imageWithFileInfo:kMultiObjectsRotatedImage orientation:UIImageOrientationRight]; MPPImage *image = [self imageWithFileInfo:kMultiObjectsRotatedImage
orientation:UIImageOrientationRight];
// roi around folding chair // roi around folding chair
MPPImageClassifierResult *imageClassifierResult = MPPImageClassifierResult *imageClassifierResult =
@ -606,8 +614,7 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
MPPImageClassifier *imageClassifier = [self imageClassifierWithOptionsSucceeds:options]; MPPImageClassifier *imageClassifier = [self imageClassifierWithOptionsSucceeds:options];
MPPImage *image = [self imageWithFileInfo:kBurgerImage];
MPPImage *image = [self imageWithFileInfo:kBurgerImage];
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
MPPImageClassifierResult *imageClassifierResult = [imageClassifier classifyVideoFrame:image MPPImageClassifierResult *imageClassifierResult = [imageClassifier classifyVideoFrame:image
@ -615,8 +622,9 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
error:nil]; error:nil];
[self assertImageClassifierResult:imageClassifierResult [self assertImageClassifierResult:imageClassifierResult
hasExpectedCategoriesCount:maxResults hasExpectedCategoriesCount:maxResults
expectedCategories:[MPPImageClassifierTests expectedCategories:
expectedResultCategoriesForClassifyBurgerImageWithFloatModel]]; [MPPImageClassifierTests
expectedResultCategoriesForClassifyBurgerImageWithFloatModel]];
} }
} }
@ -630,13 +638,14 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
options.completion = ^(MPPImageClassifierResult *result, NSError *error) { options.completion = ^(MPPImageClassifierResult *result, NSError *error) {
[self assertImageClassifierResult:result [self assertImageClassifierResult:result
hasExpectedCategoriesCount:maxResults hasExpectedCategoriesCount:maxResults
expectedCategories:[MPPImageClassifierTests expectedCategories:
expectedResultCategoriesForClassifyBurgerImageWithFloatModel]]; [MPPImageClassifierTests
expectedResultCategoriesForClassifyBurgerImageWithFloatModel]];
}; };
MPPImageClassifier *imageClassifier = [self imageClassifierWithOptionsSucceeds:options]; MPPImageClassifier *imageClassifier = [self imageClassifierWithOptionsSucceeds:options];
MPPImage *image = [self imageWithFileInfo:kBurgerImage]; MPPImage *image = [self imageWithFileInfo:kBurgerImage];
XCTAssertTrue([imageClassifier classifyAsyncImage:image timestampMs:1 error:nil]); XCTAssertTrue([imageClassifier classifyAsyncImage:image timestampMs:1 error:nil]);
@ -663,16 +672,17 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
options.completion = ^(MPPImageClassifierResult *result, NSError *error) { options.completion = ^(MPPImageClassifierResult *result, NSError *error) {
[self assertImageClassifierResult:result [self assertImageClassifierResult:result
hasExpectedCategoriesCount:maxResults hasExpectedCategoriesCount:maxResults
expectedCategories:[MPPImageClassifierTests expectedCategories:
expectedResultCategoriesForClassifyBurgerImageWithFloatModel]]; [MPPImageClassifierTests
expectedResultCategoriesForClassifyBurgerImageWithFloatModel]];
}; };
MPPImageClassifier *imageClassifier = [self imageClassifierWithOptionsSucceeds:options]; MPPImageClassifier *imageClassifier = [self imageClassifierWithOptionsSucceeds:options];
// TODO: Mimic initialization from CMSampleBuffer as live stream mode is most likely to be used with the iOS camera.
// AVCaptureVideoDataOutput sample buffer delegates provide frames of type `CMSampleBuffer`.
MPPImage *image = [self imageWithFileInfo:kBurgerImage];
// TODO: Mimic initialization from CMSampleBuffer as live stream mode is most likely to be used
// with the iOS camera. AVCaptureVideoDataOutput sample buffer delegates provide frames of type
// `CMSampleBuffer`.
MPPImage *image = [self imageWithFileInfo:kBurgerImage];
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
XCTAssertTrue([imageClassifier classifyAsyncImage:image timestampMs:i error:nil]); XCTAssertTrue([imageClassifier classifyAsyncImage:image timestampMs:i error:nil]);

View File

@ -40,7 +40,8 @@ NS_ASSUME_NONNULL_BEGIN
NS_SWIFT_NAME(imageFromBundle(class:filename:type:)); NS_SWIFT_NAME(imageFromBundle(class:filename:type:));
/** /**
* Loads an image from a file in an app bundle into a `MPPImage` object with the specified orientation. * Loads an image from a file in an app bundle into a `MPPImage` object with the specified
* orientation.
* *
* @param classObject The specified class associated with the bundle containing * @param classObject The specified class associated with the bundle containing
* the file to be loaded. * the file to be loaded.
@ -52,10 +53,10 @@ NS_ASSUME_NONNULL_BEGIN
* nil if it cannot load the image. * nil if it cannot load the image.
*/ */
+ (nullable MPPImage *)imageFromBundleWithClass:(Class)classObject + (nullable MPPImage *)imageFromBundleWithClass:(Class)classObject
fileName:(NSString *)name fileName:(NSString *)name
ofType:(NSString *)type ofType:(NSString *)type
orientation:(UIImageOrientation)imageOrientation orientation:(UIImageOrientation)imageOrientation
NS_SWIFT_NAME(imageFromBundle(class:filename:type:orientation:)); NS_SWIFT_NAME(imageFromBundle(class:filename:type:orientation:));
@end @end

View File

@ -16,14 +16,17 @@
@interface UIImage (FileUtils) @interface UIImage (FileUtils)
+(nullable UIImage *)imageFromBundleWithClass:(Class)classObject fileName:(NSString *)name ofType:(NSString *)type; + (nullable UIImage *)imageFromBundleWithClass:(Class)classObject
fileName:(NSString *)name
ofType:(NSString *)type;
@end @end
@implementation UIImage (FileUtils) @implementation UIImage (FileUtils)
+(nullable UIImage *)imageFromBundleWithClass:(Class)classObject fileName:(NSString *)name ofType:(NSString *)type { + (nullable UIImage *)imageFromBundleWithClass:(Class)classObject
fileName:(NSString *)name
ofType:(NSString *)type {
NSString *imagePath = [[NSBundle bundleForClass:classObject] pathForResource:name ofType:type]; NSString *imagePath = [[NSBundle bundleForClass:classObject] pathForResource:name ofType:type];
if (!imagePath) return nil; if (!imagePath) return nil;
@ -35,17 +38,17 @@
@implementation MPPImage (TestUtils) @implementation MPPImage (TestUtils)
+ (nullable MPPImage *)imageFromBundleWithClass:(Class)classObject + (nullable MPPImage *)imageFromBundleWithClass:(Class)classObject
fileName:(NSString *)name fileName:(NSString *)name
ofType:(NSString *)type { ofType:(NSString *)type {
UIImage *image = [UIImage imageFromBundleWithClass:classObject fileName:name ofType:type]; UIImage *image = [UIImage imageFromBundleWithClass:classObject fileName:name ofType:type];
return [[MPPImage alloc] initWithUIImage:image error:nil]; return [[MPPImage alloc] initWithUIImage:image error:nil];
} }
+ (nullable MPPImage *)imageFromBundleWithClass:(Class)classObject + (nullable MPPImage *)imageFromBundleWithClass:(Class)classObject
fileName:(NSString *)name fileName:(NSString *)name
ofType:(NSString *)type ofType:(NSString *)type
orientation:(UIImageOrientation)imageOrientation { orientation:(UIImageOrientation)imageOrientation {
UIImage *image = [UIImage imageFromBundleWithClass:classObject fileName:name ofType:type]; UIImage *image = [UIImage imageFromBundleWithClass:classObject fileName:name ofType:type];
return [[MPPImage alloc] initWithUIImage:image orientation:imageOrientation error:nil]; return [[MPPImage alloc] initWithUIImage:image orientation:imageOrientation error:nil];

View File

@ -138,7 +138,7 @@ static const NSInteger kMPPOrientationDegreesLeft = -270;
} }
- (std::optional<PacketMap>)processImagePacketMap:(const PacketMap &)packetMap - (std::optional<PacketMap>)processImagePacketMap:(const PacketMap &)packetMap
error:(NSError **)error { error:(NSError **)error {
if (_runningMode != MPPRunningModeImage) { if (_runningMode != MPPRunningModeImage) {
[MPPCommonUtils [MPPCommonUtils
createCustomError:error createCustomError:error