Added MPPImage Utils for tests

This commit is contained in:
Prianka Liz Kariat 2023-03-23 13:13:03 +05:30
parent eac6348fd3
commit 2e4e17d837
3 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,13 @@
package(default_visibility = ["//mediapipe/tasks:internal"])
licenses(["notice"])
objc_library(
name = "MPPImageTestUtils",
srcs = ["sources/MPPImage+TestUtils.m"],
hdrs = ["sources/MPPImage+TestUtils.h"],
module_name = "MPPImageTestUtils",
deps = [
"//mediapipe/tasks/ios/vision/core:MPPImage",
],
)

View File

@ -0,0 +1,51 @@
// Copyright 2023 The MediaPipe Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import <Foundation/Foundation.h>
#import "mediapipe/tasks/ios/vision/core/sources/MPPImage.h"
NS_ASSUME_NONNULL_BEGIN
/**
* Helper utility for initializing `MPPImage` for MediaPipe iOS vision library tests.
*/
@interface MPPImage (TestUtils)
/**
* Loads an image from a file in an app bundle into a `MPPImage` object.
*
* @param classObject The specified class associated with the bundle containing
* the file to be loaded.
* @param name Name of the image file.
* @param type Extenstion of the image file.
*
* @return The `MPPImage` object contains the loaded image. This method returns
* nil if it cannot load the image.
*/
+ (nullable MPPImage *)imageFromBundleWithClass:(Class)classObject
fileName:(NSString *)name
ofType:(NSString *)type
error:(NSError **)error
NS_SWIFT_NAME(imageFromBundle(class:filename:type:));
+ (nullable MPPImage *)imageFromBundleWithClass:(Class)classObject
fileName:(NSString *)name
ofType:(NSString *)type
orientation:(UIImageOrientation)imageOrientation
error:(NSError **)error
NS_SWIFT_NAME(imageFromBundle(class:filename:type:orientation:));
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,46 @@
// Copyright 2023 The MediaPipe Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "mediapipe/tasks/ios/test/vision/utils/sources/MPPImage+TestUtils.h"
@implementation MPPImage (TestUtils)
+ (nullable MPPImage *)imageFromBundleWithClass:(Class)classObject
fileName:(NSString *)name
ofType:(NSString *)type
error:(NSError **)error {
NSString *imagePath = [[NSBundle bundleForClass:classObject] pathForResource:name ofType:type];
if (!imagePath) return nil;
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];
if (!image) return nil;
return [[MPPImage alloc] initWithUIImage:image error:error];
}
+ (nullable MPPImage *)imageFromBundleWithClass:(Class)classObject
fileName:(NSString *)name
ofType:(NSString *)type
orientation:(UIImageOrientation)imageOrientation
error:(NSError **)error {
NSString *imagePath = [[NSBundle bundleForClass:classObject] pathForResource:name ofType:type];
if (!imagePath) return nil;
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];
if (!image) return nil;
return [[MPPImage alloc] initWithUIImage:image orientation:imageOrientation error:error];
}
@end