From cbccd472ab14dc8c3ccd61021f6170f47d5b3b86 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Mon, 6 Feb 2023 16:29:53 +0530 Subject: [PATCH 1/6] Added MPPTaskImage for iOS vision tasks --- mediapipe/tasks/ios/vision/core/BUILD | 15 ++ .../tasks/ios/vision/core/sources/MPPImage.h | 178 ++++++++++++++++++ .../tasks/ios/vision/core/sources/MPPImage.m | 123 ++++++++++++ 3 files changed, 316 insertions(+) create mode 100644 mediapipe/tasks/ios/vision/core/BUILD create mode 100644 mediapipe/tasks/ios/vision/core/sources/MPPImage.h create mode 100644 mediapipe/tasks/ios/vision/core/sources/MPPImage.m diff --git a/mediapipe/tasks/ios/vision/core/BUILD b/mediapipe/tasks/ios/vision/core/BUILD new file mode 100644 index 000000000..f4c1aa848 --- /dev/null +++ b/mediapipe/tasks/ios/vision/core/BUILD @@ -0,0 +1,15 @@ +package(default_visibility = ["//mediapipe/tasks:internal"]) + +licenses(["notice"]) + +objc_library( + name = "MPPTaskImage", + srcs = ["sources/MPPTaskImage.h"], + hdrs = ["sources/MPPTaskImage.m"], + module_name = "MPPTaskImage", + sdk_frameworks = [ + "CoreMedia", + "CoreVideo", + "UIKit", + ] +) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPImage.h b/mediapipe/tasks/ios/vision/core/sources/MPPImage.h new file mode 100644 index 000000000..5c805870f --- /dev/null +++ b/mediapipe/tasks/ios/vision/core/sources/MPPImage.h @@ -0,0 +1,178 @@ +// 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 +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +/** Types of image sources. */ +typedef NSInteger GMLImageSourceType NS_TYPED_ENUM NS_SWIFT_NAME(MLImageSourceType); +/** Image source is a `UIImage`. */ +static const GMLImageSourceType MPPTaskImageSourceTypeImage = 0; +/** Image source is a `CVPixelBuffer`. */ +static const GMLImageSourceType MPPTaskImageSourceTypePixelBuffer = 1; +/** Image source is a `CMSampleBuffer`. */ +static const GMLImageSourceType MPPTaskImageSourceTypeSampleBuffer = 2; + +/** An image used in on-device machine learning using MediaPipe Task library. */ +NS_SWIFT_NAME(MPPTaskImage) +@interface MPPTaskImage : NSObject + +/** Width of the image in pixels. */ +@property(nonatomic, readonly) CGFloat width; + +/** Height of the image in pixels. */ +@property(nonatomic, readonly) CGFloat height; + +/** + * The display orientation of the image. If `imageSourceType` is `MPPTaskImageSourceTypeImage`, the + * default value is `image.imageOrientation`; otherwise the default value is `UIImageOrientationUp`. + * If the `MPPTaskImage` is being used as input for any MediaPipe vision tasks and is set to any + * orientation other than `UIImageOrientationUp`, inference will be performed on a rotated copy of + * the image according to the orientation. + */ +@property(nonatomic, readonly) UIImageOrientation orientation; + +/** The type of the image source. */ +@property(nonatomic, readonly) GMLImageSourceType imageSourceType; + +/** The source image. `nil` if `imageSourceType` is not `.image`. */ +@property(nonatomic, readonly, nullable) UIImage *image; + +/** The source pixel buffer. `nil` if `imageSourceType` is not `.pixelBuffer`. */ +@property(nonatomic, readonly, nullable) CVPixelBufferRef pixelBuffer; + +/** The source sample buffer. `nil` if `imageSourceType` is not `.sampleBuffer`. */ +@property(nonatomic, readonly, nullable) CMSampleBufferRef sampleBuffer; + +/** + * Initializes an `MPPTaskImage` object with the given `UIImage`. + * The orientation of the newly created `MPPTaskImage` will be `UIImageOrientationUp`. + * Hence, if this image is used as input for any MediaPipe vision tasks, inference will be performed + * on the it without any rotation. To create an `MPPTaskImage` with a different orientation, please + * use `[MPPTaskImage initWithImage:orientation:error:]`. + * + * @param image The image to use as the source. Its `CGImage` property must not be `NULL`. + * @param error An optional error parameter populated when there is an error in initializing the + * `MPPTaskImage`. + * + * @return A new `MPPTaskImage` instance with the given image as the source. `nil` if the given + * `image` is `nil` or invalid. + */ +- (nullable instancetype)initWithUIImage:(UIImage *)image + error:(NSError **)error NS_DESIGNATED_INITIALIZER; + +/** + * Initializes an `MPPTaskImage` object with the given `UIImabe` and orientation. + * + * If the newly created `MPPTaskImage` is used as input for any MediaPipe vision tasks, inference + * will be performed on a copy of the image rotated according to the orientation. + * + * @param image The image to use as the source. Its `CGImage` property must not be `NULL`. + * @param orientation The display orientation of the image. This will be stored in the property + * `orientation`. `MPPTaskImage`. + * @param error An optional error parameter populated when there is an error in initializing the + * `MPPTaskImage`. + * + * @return A new `MPPTaskImage` instance with the given image as the source. `nil` if the given + * `image` is `nil` or invalid. + */ +- (nullable instancetype)initWithUIImage:(UIImage *)image + orientation:(UIImageOrientation)orientation + error:(NSError **)error NS_DESIGNATED_INITIALIZER; + +/** + * Initializes an `MPPTaskImage` object with the given pixel buffer. + * + * The orientation of the newly created `MPPTaskImage` will be `UIImageOrientationUp`. + * Hence, if this image is used as input for any MediaPipe vision tasks, inference will be performed + * on the it without any rotation. To create an `MPPTaskImage` with a different orientation, please + * use `[MPPTaskImage initWithPixelBuffer:orientation:error:]`. + * + * @param pixelBuffer The pixel buffer to use as the source. It will be retained by the new + * `MPPTaskImage` instance for the duration of its lifecycle. + * @param error An optional error parameter populated when there is an error in initializing the + * `MPPTaskImage`. + * + * @return A new `MPPTaskImage` instance with the given pixel buffer as the source. `nil` if the + * given pixel buffer is `nil` or invalid. + */ +- (nullable instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer + error:(NSError **)error NS_DESIGNATED_INITIALIZER; + +/** + * Initializes an `MPPTaskImage` object with the given pixel buffer and orientation. + * + * If the newly created `MPPTaskImage` is used as input for any MediaPipe vision tasks, inference + * will be performed on a copy of the image rotated according to the orientation. + * + * @param pixelBuffer The pixel buffer to use as the source. It will be retained by the new + * `MPPTaskImage` instance for the duration of its lifecycle. + * @param orientation The display orientation of the image. + * @param error An optional error parameter populated when there is an error in initializing the + * `MPPTaskImage`. + * + * @return A new `MPPTaskImage` instance with the given orientation and pixel buffer as the source. + * `nil` if the given pixel buffer is `nil` or invalid. + */ +- (nullable instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer + orientation:(UIImageOrientation)orientation + NS_DESIGNATED_INITIALIZER; + +/** + * Initializes an `MPPTaskImage` object with the given sample buffer. + * + * The orientation of the newly created `MPPTaskImage` will be `UIImageOrientationUp`. + * Hence, if this image is used as input for any MediaPipe vision tasks, inference will be performed + * on the it without any rotation. To create an `MPPTaskImage` with a different orientation, please + * use `[MPPTaskImage initWithSampleBuffer:orientation:error:]`. + * + * @param sampleBuffer The sample buffer to use as the source. It will be retained by the new + * `MPPTaskImage` instance for the duration of its lifecycle. The sample buffer must be based on + * a pixel buffer (not compressed data). In practice, it should be the video output of the camera on + * an iOS device, not other arbitrary types of `CMSampleBuffer`s. + * @return A new `MPPTaskImage` instance with the given sample buffer as the source. `nil` if the + * given sample buffer is `nil` or invalid. + */ +- (nullable instancetype)initWithSampleBuffer:(CMSampleBufferRef)sampleBuffer + error:(NSError **)error NS_DESIGNATED_INITIALIZER; + +/** + * Initializes an `MPPTaskImage` object with the given sample buffer and orientation. + * + * If the newly created `MPPTaskImage` is used as input for any MediaPipe vision tasks, inference + * will be performed on a copy of the image rotated according to the orientation. + * + * @param sampleBuffer The sample buffer to use as the source. It will be retained by the new + * `MPPTaskImage` instance for the duration of its lifecycle. The sample buffer must be based on + * a pixel buffer (not compressed data). In practice, it should be the video output of the camera on + * an iOS device, not other arbitrary types of `CMSampleBuffer`s. + * @param orientation The display orientation of the image. + * @return A new `MPPTaskImage` instance with the given orientation and sample buffer as the source. + * `nil` if the given sample buffer is `nil` or invalid. + */ +- (nullable instancetype)initWithSampleBuffer:(CMSampleBufferRef)sampleBuffer + orientation:(UIImageOrientation)orientation + NS_DESIGNATED_INITIALIZER; + +/** Unavailable. */ +- (instancetype)init NS_UNAVAILABLE; + ++ (instancetype)new NS_UNAVAILABLE; + +@end + +NS_ASSUME_NONNULL_END diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPImage.m b/mediapipe/tasks/ios/vision/core/sources/MPPImage.m new file mode 100644 index 000000000..4ef5020fc --- /dev/null +++ b/mediapipe/tasks/ios/vision/core/sources/MPPImage.m @@ -0,0 +1,123 @@ +// 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/vision/core/sources/MPPTaskImage.h" +#import "mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h" + +NS_ASSUME_NONNULL_BEGIN + +@implementation MPPTaskImage + +- (nullable instancetype)initWithUIImage:(UIImage *)image error:(NSError **)error { + return [self initWithUIImage:image orientation:image.orientation error:error]; +} + +- (nullable instancetype)initWithUIImage:(UIImage *)image + orientation:(UIImageOrientation)orientation + error:(NSError **)error { + if (image == nil) { + [MPPCommonUtils createCustomError:error + withCode:MPPTasksErrorCodeInvalidArgumentError + description:@"Image cannot be nil."]; + } + if (image.CGImage == NULL) { + [MPPCommonUtils createCustomError:error + withCode:MPPTasksErrorCodeInvalidArgumentError + description:@"Image does not have a valid underlying CGImage. " + @"image.CGImage must be non nil."]; + return nil; + } + + self = [super init]; + if (self) { + _imageSourceType = MPPTaskImageSourceTypeImage; + _orientation = orientation; + _image = image; + _width = image.size.width * image.scale; + _height = image.size.height * image.scale; + } + return self; +} + +- (nullable instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer error:(NSError **)error { + return [self initWithPixelBuffer:pixelBuffer orientation:UIImageOrientationUp error:error]; +} + +- (nullable instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer + orientation:(UIImageOrientation)orientation + error:(NSError **)error { + if (pixelBuffer == NULL) { + [MPPCommonUtils createCustomError:error + withCode:MPPTasksErrorCodeInvalidArgumentError + description:@"Pixel Buffer cannot be nil."]; + return nil; + } + + self = [super init]; + if (self != nil) { + _imageSourceType = MPPTaskImageSourceTypePixelBuffer; + _orientation = orientation; + CVPixelBufferRetain(pixelBuffer); + _pixelBuffer = pixelBuffer; + _width = CVPixelBufferGetWidth(pixelBuffer); + _height = CVPixelBufferGetHeight(pixelBuffer); + } + return self; +} + +- (nullable instancetype)initWithSampleBuffer:(CMSampleBufferRef)sampleBuffer + error:(NSError **)error { + return [self initWithSampleBuffer:sampleBuffer orientation:UIImageOrientation error:error]; +} + +- (nullable instancetype)initWithSampleBuffer:(CMSampleBufferRef)sampleBuffer + orientation:(UIImageOrientation)orientation + error:(NSError **)error { + if (!CMSampleBufferIsValid(sampleBuffer)) { + [MPPCommonUtils createCustomError:error + withCode:MPPTasksErrorCodeInvalidArgumentError + description:@"Sample buffer is not valid. Invoking " + @"CMSampleBufferIsValid(sampleBuffer) must return true."]; + return nil; + } + + CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); + if (imageBuffer == NULL) { + return nil; + } + + self = [super init]; + if (self != nil) { + _imageSourceType = MPPTaskImageSourceTypeSampleBuffer; + _orientation = orientation; + CFRetain(sampleBuffer); + _sampleBuffer = sampleBuffer; + _width = CVPixelBufferGetWidth(imageBuffer); + _height = CVPixelBufferGetHeight(imageBuffer); + } + return self; +} + +- (void)dealloc { + if (_sampleBuffer != NULL) { + CFRelease(_sampleBuffer); + } + if (_pixelBuffer != NULL) { + CVPixelBufferRelease(_pixelBuffer); + } +} + +@end + +NS_ASSUME_NONNULL_END From a5ac5b9eec956edab7df38103c5c8ad277c85e46 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Mon, 6 Feb 2023 16:35:27 +0530 Subject: [PATCH 2/6] Updated type name of image source --- mediapipe/tasks/ios/vision/core/sources/MPPImage.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPImage.h b/mediapipe/tasks/ios/vision/core/sources/MPPImage.h index 5c805870f..f796c76c9 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPImage.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPImage.h @@ -19,13 +19,13 @@ NS_ASSUME_NONNULL_BEGIN /** Types of image sources. */ -typedef NSInteger GMLImageSourceType NS_TYPED_ENUM NS_SWIFT_NAME(MLImageSourceType); +typedef NSInteger MPPTaskImageSourceType NS_TYPED_ENUM NS_SWIFT_NAME(MLImageSourceType); /** Image source is a `UIImage`. */ -static const GMLImageSourceType MPPTaskImageSourceTypeImage = 0; +static const MPPTaskImageSourceType MPPTaskImageSourceTypeImage = 0; /** Image source is a `CVPixelBuffer`. */ -static const GMLImageSourceType MPPTaskImageSourceTypePixelBuffer = 1; +static const MPPTaskImageSourceType MPPTaskImageSourceTypePixelBuffer = 1; /** Image source is a `CMSampleBuffer`. */ -static const GMLImageSourceType MPPTaskImageSourceTypeSampleBuffer = 2; +static const MPPTaskImageSourceType MPPTaskImageSourceTypeSampleBuffer = 2; /** An image used in on-device machine learning using MediaPipe Task library. */ NS_SWIFT_NAME(MPPTaskImage) @@ -47,7 +47,7 @@ NS_SWIFT_NAME(MPPTaskImage) @property(nonatomic, readonly) UIImageOrientation orientation; /** The type of the image source. */ -@property(nonatomic, readonly) GMLImageSourceType imageSourceType; +@property(nonatomic, readonly) MPPTaskImageSourceType imageSourceType; /** The source image. `nil` if `imageSourceType` is not `.image`. */ @property(nonatomic, readonly, nullable) UIImage *image; From 4da91896af8d018f07ebae953cbca3bc0ec58f55 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Mon, 6 Feb 2023 16:36:33 +0530 Subject: [PATCH 3/6] Updated swift name for ImageSource Type --- mediapipe/tasks/ios/vision/core/sources/MPPImage.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPImage.h b/mediapipe/tasks/ios/vision/core/sources/MPPImage.h index f796c76c9..44f5f9ba9 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPImage.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPImage.h @@ -19,7 +19,7 @@ NS_ASSUME_NONNULL_BEGIN /** Types of image sources. */ -typedef NSInteger MPPTaskImageSourceType NS_TYPED_ENUM NS_SWIFT_NAME(MLImageSourceType); +typedef NSInteger MPPTaskImageSourceType NS_TYPED_ENUM NS_SWIFT_NAME(TaskImageSourceType); /** Image source is a `UIImage`. */ static const MPPTaskImageSourceType MPPTaskImageSourceTypeImage = 0; /** Image source is a `CVPixelBuffer`. */ From 0e4f0fbf9bdaad8323fbddc94f52548411c08029 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Mon, 6 Feb 2023 16:39:17 +0530 Subject: [PATCH 4/6] Updated file name for MPPTaskImage --- .../tasks/ios/vision/core/sources/{MPPImage.h => MPPTaskImage.h} | 0 .../tasks/ios/vision/core/sources/{MPPImage.m => MPPTaskImage.m} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename mediapipe/tasks/ios/vision/core/sources/{MPPImage.h => MPPTaskImage.h} (100%) rename mediapipe/tasks/ios/vision/core/sources/{MPPImage.m => MPPTaskImage.m} (100%) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPImage.h b/mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.h similarity index 100% rename from mediapipe/tasks/ios/vision/core/sources/MPPImage.h rename to mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.h diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPImage.m b/mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.m similarity index 100% rename from mediapipe/tasks/ios/vision/core/sources/MPPImage.m rename to mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.m From a3889c946d449f9c809b9a8ae45ec14bef78a37d Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Mon, 6 Feb 2023 16:40:36 +0530 Subject: [PATCH 5/6] Updated list of designated initializers --- mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.h b/mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.h index 44f5f9ba9..f757a796c 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.h @@ -73,7 +73,7 @@ NS_SWIFT_NAME(MPPTaskImage) * `image` is `nil` or invalid. */ - (nullable instancetype)initWithUIImage:(UIImage *)image - error:(NSError **)error NS_DESIGNATED_INITIALIZER; + error:(NSError **)error; /** * Initializes an `MPPTaskImage` object with the given `UIImabe` and orientation. @@ -111,7 +111,7 @@ NS_SWIFT_NAME(MPPTaskImage) * given pixel buffer is `nil` or invalid. */ - (nullable instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer - error:(NSError **)error NS_DESIGNATED_INITIALIZER; + error:(NSError **)error; /** * Initializes an `MPPTaskImage` object with the given pixel buffer and orientation. @@ -148,7 +148,7 @@ NS_SWIFT_NAME(MPPTaskImage) * given sample buffer is `nil` or invalid. */ - (nullable instancetype)initWithSampleBuffer:(CMSampleBufferRef)sampleBuffer - error:(NSError **)error NS_DESIGNATED_INITIALIZER; + error:(NSError **)error; /** * Initializes an `MPPTaskImage` object with the given sample buffer and orientation. From be829eb276aa29e4af4ffac15a8d41ceca661593 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Tue, 7 Feb 2023 19:17:00 +0530 Subject: [PATCH 6/6] Updated name of MPPTaskImage --- mediapipe/tasks/ios/vision/core/BUILD | 8 +- .../sources/{MPPTaskImage.h => MPPImage.h} | 84 +++++++++---------- .../sources/{MPPTaskImage.m => MPPImage.m} | 4 +- 3 files changed, 48 insertions(+), 48 deletions(-) rename mediapipe/tasks/ios/vision/core/sources/{MPPTaskImage.h => MPPImage.h} (64%) rename mediapipe/tasks/ios/vision/core/sources/{MPPTaskImage.m => MPPImage.m} (97%) diff --git a/mediapipe/tasks/ios/vision/core/BUILD b/mediapipe/tasks/ios/vision/core/BUILD index f4c1aa848..816486391 100644 --- a/mediapipe/tasks/ios/vision/core/BUILD +++ b/mediapipe/tasks/ios/vision/core/BUILD @@ -3,10 +3,10 @@ package(default_visibility = ["//mediapipe/tasks:internal"]) licenses(["notice"]) objc_library( - name = "MPPTaskImage", - srcs = ["sources/MPPTaskImage.h"], - hdrs = ["sources/MPPTaskImage.m"], - module_name = "MPPTaskImage", + name = "MPPImage", + srcs = ["sources/MPPImage.h"], + hdrs = ["sources/MPPImage.m"], + module_name = "MPPImage", sdk_frameworks = [ "CoreMedia", "CoreVideo", diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.h b/mediapipe/tasks/ios/vision/core/sources/MPPImage.h similarity index 64% rename from mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.h rename to mediapipe/tasks/ios/vision/core/sources/MPPImage.h index f757a796c..6faaa0d31 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPImage.h @@ -19,17 +19,17 @@ NS_ASSUME_NONNULL_BEGIN /** Types of image sources. */ -typedef NSInteger MPPTaskImageSourceType NS_TYPED_ENUM NS_SWIFT_NAME(TaskImageSourceType); +typedef NSInteger MPPImageSourceType NS_TYPED_ENUM NS_SWIFT_NAME(MPImageSourceType); /** Image source is a `UIImage`. */ -static const MPPTaskImageSourceType MPPTaskImageSourceTypeImage = 0; +static const MPPImageSourceType MPPImageSourceTypeImage = 0; /** Image source is a `CVPixelBuffer`. */ -static const MPPTaskImageSourceType MPPTaskImageSourceTypePixelBuffer = 1; +static const MPPImageSourceType MPPImageSourceTypePixelBuffer = 1; /** Image source is a `CMSampleBuffer`. */ -static const MPPTaskImageSourceType MPPTaskImageSourceTypeSampleBuffer = 2; +static const MPPImageSourceType MPPImageSourceTypeSampleBuffer = 2; /** An image used in on-device machine learning using MediaPipe Task library. */ -NS_SWIFT_NAME(MPPTaskImage) -@interface MPPTaskImage : NSObject +NS_SWIFT_NAME(MPImage) +@interface MPPImage : NSObject /** Width of the image in pixels. */ @property(nonatomic, readonly) CGFloat width; @@ -38,16 +38,16 @@ NS_SWIFT_NAME(MPPTaskImage) @property(nonatomic, readonly) CGFloat height; /** - * The display orientation of the image. If `imageSourceType` is `MPPTaskImageSourceTypeImage`, the + * The display orientation of the image. If `imageSourceType` is `MPPImageSourceTypeImage`, the * default value is `image.imageOrientation`; otherwise the default value is `UIImageOrientationUp`. - * If the `MPPTaskImage` is being used as input for any MediaPipe vision tasks and is set to any + * If the `MPPImage` is being used as input for any MediaPipe vision tasks and is set to any * orientation other than `UIImageOrientationUp`, inference will be performed on a rotated copy of * the image according to the orientation. */ @property(nonatomic, readonly) UIImageOrientation orientation; /** The type of the image source. */ -@property(nonatomic, readonly) MPPTaskImageSourceType imageSourceType; +@property(nonatomic, readonly) MPPImageSourceType imageSourceType; /** The source image. `nil` if `imageSourceType` is not `.image`. */ @property(nonatomic, readonly, nullable) UIImage *image; @@ -59,35 +59,35 @@ NS_SWIFT_NAME(MPPTaskImage) @property(nonatomic, readonly, nullable) CMSampleBufferRef sampleBuffer; /** - * Initializes an `MPPTaskImage` object with the given `UIImage`. - * The orientation of the newly created `MPPTaskImage` will be `UIImageOrientationUp`. + * Initializes an `MPPImage` object with the given `UIImage`. + * The orientation of the newly created `MPPImage` will be `UIImageOrientationUp`. * Hence, if this image is used as input for any MediaPipe vision tasks, inference will be performed - * on the it without any rotation. To create an `MPPTaskImage` with a different orientation, please - * use `[MPPTaskImage initWithImage:orientation:error:]`. + * on the it without any rotation. To create an `MPPImage` with a different orientation, please + * use `[MPPImage initWithImage:orientation:error:]`. * * @param image The image to use as the source. Its `CGImage` property must not be `NULL`. * @param error An optional error parameter populated when there is an error in initializing the - * `MPPTaskImage`. + * `MPPImage`. * - * @return A new `MPPTaskImage` instance with the given image as the source. `nil` if the given + * @return A new `MPPImage` instance with the given image as the source. `nil` if the given * `image` is `nil` or invalid. */ - (nullable instancetype)initWithUIImage:(UIImage *)image error:(NSError **)error; /** - * Initializes an `MPPTaskImage` object with the given `UIImabe` and orientation. + * Initializes an `MPPImage` object with the given `UIImabe` and orientation. * - * If the newly created `MPPTaskImage` is used as input for any MediaPipe vision tasks, inference + * If the newly created `MPPImage` is used as input for any MediaPipe vision tasks, inference * will be performed on a copy of the image rotated according to the orientation. * * @param image The image to use as the source. Its `CGImage` property must not be `NULL`. * @param orientation The display orientation of the image. This will be stored in the property - * `orientation`. `MPPTaskImage`. + * `orientation`. `MPPImage`. * @param error An optional error parameter populated when there is an error in initializing the - * `MPPTaskImage`. + * `MPPImage`. * - * @return A new `MPPTaskImage` instance with the given image as the source. `nil` if the given + * @return A new `MPPImage` instance with the given image as the source. `nil` if the given * `image` is `nil` or invalid. */ - (nullable instancetype)initWithUIImage:(UIImage *)image @@ -95,37 +95,37 @@ NS_SWIFT_NAME(MPPTaskImage) error:(NSError **)error NS_DESIGNATED_INITIALIZER; /** - * Initializes an `MPPTaskImage` object with the given pixel buffer. + * Initializes an `MPPImage` object with the given pixel buffer. * - * The orientation of the newly created `MPPTaskImage` will be `UIImageOrientationUp`. + * The orientation of the newly created `MPPImage` will be `UIImageOrientationUp`. * Hence, if this image is used as input for any MediaPipe vision tasks, inference will be performed - * on the it without any rotation. To create an `MPPTaskImage` with a different orientation, please - * use `[MPPTaskImage initWithPixelBuffer:orientation:error:]`. + * on the it without any rotation. To create an `MPPImage` with a different orientation, please + * use `[MPPImage initWithPixelBuffer:orientation:error:]`. * * @param pixelBuffer The pixel buffer to use as the source. It will be retained by the new - * `MPPTaskImage` instance for the duration of its lifecycle. + * `MPPImage` instance for the duration of its lifecycle. * @param error An optional error parameter populated when there is an error in initializing the - * `MPPTaskImage`. + * `MPPImage`. * - * @return A new `MPPTaskImage` instance with the given pixel buffer as the source. `nil` if the + * @return A new `MPPImage` instance with the given pixel buffer as the source. `nil` if the * given pixel buffer is `nil` or invalid. */ - (nullable instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer error:(NSError **)error; /** - * Initializes an `MPPTaskImage` object with the given pixel buffer and orientation. + * Initializes an `MPPImage` object with the given pixel buffer and orientation. * - * If the newly created `MPPTaskImage` is used as input for any MediaPipe vision tasks, inference + * If the newly created `MPPImage` is used as input for any MediaPipe vision tasks, inference * will be performed on a copy of the image rotated according to the orientation. * * @param pixelBuffer The pixel buffer to use as the source. It will be retained by the new - * `MPPTaskImage` instance for the duration of its lifecycle. + * `MPPImage` instance for the duration of its lifecycle. * @param orientation The display orientation of the image. * @param error An optional error parameter populated when there is an error in initializing the - * `MPPTaskImage`. + * `MPPImage`. * - * @return A new `MPPTaskImage` instance with the given orientation and pixel buffer as the source. + * @return A new `MPPImage` instance with the given orientation and pixel buffer as the source. * `nil` if the given pixel buffer is `nil` or invalid. */ - (nullable instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer @@ -133,35 +133,35 @@ NS_SWIFT_NAME(MPPTaskImage) NS_DESIGNATED_INITIALIZER; /** - * Initializes an `MPPTaskImage` object with the given sample buffer. + * Initializes an `MPPImage` object with the given sample buffer. * - * The orientation of the newly created `MPPTaskImage` will be `UIImageOrientationUp`. + * The orientation of the newly created `MPPImage` will be `UIImageOrientationUp`. * Hence, if this image is used as input for any MediaPipe vision tasks, inference will be performed - * on the it without any rotation. To create an `MPPTaskImage` with a different orientation, please - * use `[MPPTaskImage initWithSampleBuffer:orientation:error:]`. + * on the it without any rotation. To create an `MPPImage` with a different orientation, please + * use `[MPPImage initWithSampleBuffer:orientation:error:]`. * * @param sampleBuffer The sample buffer to use as the source. It will be retained by the new - * `MPPTaskImage` instance for the duration of its lifecycle. The sample buffer must be based on + * `MPPImage` instance for the duration of its lifecycle. The sample buffer must be based on * a pixel buffer (not compressed data). In practice, it should be the video output of the camera on * an iOS device, not other arbitrary types of `CMSampleBuffer`s. - * @return A new `MPPTaskImage` instance with the given sample buffer as the source. `nil` if the + * @return A new `MPPImage` instance with the given sample buffer as the source. `nil` if the * given sample buffer is `nil` or invalid. */ - (nullable instancetype)initWithSampleBuffer:(CMSampleBufferRef)sampleBuffer error:(NSError **)error; /** - * Initializes an `MPPTaskImage` object with the given sample buffer and orientation. + * Initializes an `MPPImage` object with the given sample buffer and orientation. * - * If the newly created `MPPTaskImage` is used as input for any MediaPipe vision tasks, inference + * If the newly created `MPPImage` is used as input for any MediaPipe vision tasks, inference * will be performed on a copy of the image rotated according to the orientation. * * @param sampleBuffer The sample buffer to use as the source. It will be retained by the new - * `MPPTaskImage` instance for the duration of its lifecycle. The sample buffer must be based on + * `MPPImage` instance for the duration of its lifecycle. The sample buffer must be based on * a pixel buffer (not compressed data). In practice, it should be the video output of the camera on * an iOS device, not other arbitrary types of `CMSampleBuffer`s. * @param orientation The display orientation of the image. - * @return A new `MPPTaskImage` instance with the given orientation and sample buffer as the source. + * @return A new `MPPImage` instance with the given orientation and sample buffer as the source. * `nil` if the given sample buffer is `nil` or invalid. */ - (nullable instancetype)initWithSampleBuffer:(CMSampleBufferRef)sampleBuffer diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.m b/mediapipe/tasks/ios/vision/core/sources/MPPImage.m similarity index 97% rename from mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.m rename to mediapipe/tasks/ios/vision/core/sources/MPPImage.m index 4ef5020fc..b8591953a 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.m +++ b/mediapipe/tasks/ios/vision/core/sources/MPPImage.m @@ -12,12 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -#import "mediapipe/tasks/ios/vision/core/sources/MPPTaskImage.h" +#import "mediapipe/tasks/ios/vision/core/sources/MPPImage.h" #import "mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h" NS_ASSUME_NONNULL_BEGIN -@implementation MPPTaskImage +@implementation MPPImage - (nullable instancetype)initWithUIImage:(UIImage *)image error:(NSError **)error { return [self initWithUIImage:image orientation:image.orientation error:error];