From a259300bfe9680236908950419d5c07000988e86 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Fri, 15 Sep 2023 14:05:38 +0530 Subject: [PATCH 1/3] Added iOS face stylizer result --- .../tasks/ios/vision/face_stylizer/BUILD | 28 ++++++++++ .../sources/MPPFaceStylizerResult.h | 53 +++++++++++++++++++ .../sources/MPPFaceStylizerResult.m | 30 +++++++++++ 3 files changed, 111 insertions(+) create mode 100644 mediapipe/tasks/ios/vision/face_stylizer/BUILD create mode 100644 mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerResult.h create mode 100644 mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerResult.m diff --git a/mediapipe/tasks/ios/vision/face_stylizer/BUILD b/mediapipe/tasks/ios/vision/face_stylizer/BUILD new file mode 100644 index 000000000..1f0a25ac4 --- /dev/null +++ b/mediapipe/tasks/ios/vision/face_stylizer/BUILD @@ -0,0 +1,28 @@ +# 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. + +package(default_visibility = ["//mediapipe/tasks:internal"]) + +licenses(["notice"]) + +objc_library( + name = "MPPFaceStylizerResult", + srcs = ["sources/MPPFaceStylizerResult.m"], + hdrs = ["sources/MPPFaceStylizerResult.h"], + deps = [ + "//mediapipe/tasks/ios/core:MPPTaskResult", + "//mediapipe/tasks/ios/vision/core:MPPImage", + ], +) + diff --git a/mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerResult.h b/mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerResult.h new file mode 100644 index 000000000..65b22743b --- /dev/null +++ b/mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerResult.h @@ -0,0 +1,53 @@ +// 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 "mediapipe/tasks/ios/core/sources/MPPTaskResult.h" +#import "mediapipe/tasks/ios/vision/core/sources/MPPImage.h" + +NS_ASSUME_NONNULL_BEGIN + +/** Represents the stylized image generated by `FaceStylizer`. */ +NS_SWIFT_NAME(FaceStylizerResult) +@interface MPPFaceStylizerResult : MPPTaskResult + +/** + * An `MPImage` representing the stylized image of the most visible face. Empty if no face is + * detected in the input image. + */ +@property(nonatomic, readonly, nullable) MPPImage *stylizedImage; + +/** + * Initializes a new `FaceStylizerResult` with the given stylized image. + * + * @param stylizedImage An `MPImage` representing the stylized image of the most + * visible face. Empty if no face is detected in the input image. + * @param timestampInMilliseconds The timestamp for this result. + * + * @return An instance of `FaceStylizerResult` initialized with the given stylized image and + * timestamp. + * + */ +- (instancetype)initWithImage:(nullable MPPImage *)image + timestampInMilliseconds:(NSInteger)timestampInMilliseconds; + +- (instancetype)initWithTimestampInMilliseconds:(NSInteger)timestampInMilliseconds NS_UNAVAILABLE; + +- (instancetype)init NS_UNAVAILABLE; + ++ (instancetype)new NS_UNAVAILABLE; + +@end + +NS_ASSUME_NONNULL_END diff --git a/mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerResult.m b/mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerResult.m new file mode 100644 index 000000000..568f05f34 --- /dev/null +++ b/mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerResult.m @@ -0,0 +1,30 @@ +// 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 "mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerResult.h" + +@implementation MPPFaceStylizerResult + +- (instancetype)initWithImage:(MPPImage *)image + timestampInMilliseconds:(NSInteger)timestampInMilliseconds { + self = [super initWithTimestampInMilliseconds:timestampInMilliseconds]; + if (self) { + _stylizedImage = image; + } + return self; +} + +@end From bb93b775f4b418d8e9fd5062d51c9cb679517d14 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Fri, 15 Sep 2023 14:05:59 +0530 Subject: [PATCH 2/3] Added iOS face stylizer options --- .../tasks/ios/vision/face_stylizer/BUILD | 9 +++++++ .../sources/MPPFaceStylizerOptions.h | 27 +++++++++++++++++++ .../sources/MPPFaceStylizerOptions.m | 25 +++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerOptions.h create mode 100644 mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerOptions.m diff --git a/mediapipe/tasks/ios/vision/face_stylizer/BUILD b/mediapipe/tasks/ios/vision/face_stylizer/BUILD index 1f0a25ac4..b98a4de93 100644 --- a/mediapipe/tasks/ios/vision/face_stylizer/BUILD +++ b/mediapipe/tasks/ios/vision/face_stylizer/BUILD @@ -26,3 +26,12 @@ objc_library( ], ) +objc_library( + name = "MPPFaceStylizerOptions", + srcs = ["sources/MPPFaceStylizerOptions.m"], + hdrs = ["sources/MPPFaceStylizerOptions.h"], + deps = [ + "//mediapipe/tasks/ios/core:MPPTaskOptions", + ], +) + diff --git a/mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerOptions.h b/mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerOptions.h new file mode 100644 index 000000000..a62e10d8c --- /dev/null +++ b/mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerOptions.h @@ -0,0 +1,27 @@ +// 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 "mediapipe/tasks/ios/core/sources/MPPTaskOptions.h" + +NS_ASSUME_NONNULL_BEGIN + +/** Options for setting up a `FaceStylizer`. */ +NS_SWIFT_NAME(FaceStylizerOptions) +@interface MPPFaceStylizerOptions : MPPTaskOptions + +@end + +NS_ASSUME_NONNULL_END diff --git a/mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerOptions.m b/mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerOptions.m new file mode 100644 index 000000000..cf57be6ea --- /dev/null +++ b/mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerOptions.m @@ -0,0 +1,25 @@ +// 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/face_stylizer/sources/MPPFaceStylizerOptions.h" + +@implementation MPPFaceStylizerOptions + +- (id)copyWithZone:(NSZone *)zone { + MPPFaceStylizerOptions *faceStylizerOptions = [super copyWithZone:zone]; + + return faceStylizerOptions; +} + +@end From 21d000490c0908de7b3422ba9d5eee2ca1ac1ee6 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Fri, 15 Sep 2023 14:06:11 +0530 Subject: [PATCH 3/3] Added iOS face stylizer header --- .../tasks/ios/vision/face_stylizer/BUILD | 9 ++ .../face_stylizer/sources/MPPFaceStylizer.h | 111 ++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizer.h diff --git a/mediapipe/tasks/ios/vision/face_stylizer/BUILD b/mediapipe/tasks/ios/vision/face_stylizer/BUILD index b98a4de93..b80182409 100644 --- a/mediapipe/tasks/ios/vision/face_stylizer/BUILD +++ b/mediapipe/tasks/ios/vision/face_stylizer/BUILD @@ -35,3 +35,12 @@ objc_library( ], ) +objc_library( + name = "MPPFaceStylizer", + hdrs = ["sources/MPPFaceStylizer.h"], + deps = [ + ":MPPFaceStylizerOptions", + ":MPPFaceStylizerResult", + "//mediapipe/tasks/ios/vision/core:MPPImage", + ], +) diff --git a/mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizer.h b/mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizer.h new file mode 100644 index 000000000..83340e606 --- /dev/null +++ b/mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizer.h @@ -0,0 +1,111 @@ +// 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 "mediapipe/tasks/ios/vision/core/sources/MPPImage.h" +#import "mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerOptions.h" +#import "mediapipe/tasks/ios/vision/face_stylizer/sources/MPPFaceStylizerResult.h" + +NS_ASSUME_NONNULL_BEGIN + +/** + * Class that performs face stylization on images. + */ +NS_SWIFT_NAME(FaceStylizer) +@interface MPPFaceStylizer : NSObject + +/** + * Creates a new instance of `FaceStylizer` from an absolute path to a TensorFlow Lite model file + * stored locally on the device and the default `FaceStylizer`. + * + * @param modelPath An absolute path to a TensorFlow Lite model file stored locally on the device. + * + * @return A new instance of `FaceStylizer` with the given model path. `nil` if there is an error in + * initializing the face stylizer. + */ +- (nullable instancetype)initWithModelPath:(NSString *)modelPath error:(NSError **)error; + +/** + * Creates a new instance of `FaceStylizer` from the given `FaceStylizerOptions`. + * + * @param options The options of type `FaceStylizerOptions` to use for configuring the + * `FaceStylizer`. + * + * @return A new instance of `FaceStylizer` with the given options. `nil` if there is an error in + * initializing the face stylizer. + */ +- (nullable instancetype)initWithOptions:(MPPFaceStylizerOptions *)options + error:(NSError **)error NS_DESIGNATED_INITIALIZER; + +/** + * Performs face stylization on the provided `MPImage` and returns a `MPPFaceStylizerResult` + * containing a copy of the stylized image. This method should not be used in high-throughput + * applications since the returned image is copied. Rotation will be applied according to the + * `orientation` property of the provided `MPImage`. + * + * This method supports RGBA images. If your `MPPImage` has a source type of `.pixelBuffer` or + * `.sampleBuffer`, the underlying pixel buffer must have one of the following pixel format types: + * 1. kCVPixelFormatType_32BGRA + * 2. kCVPixelFormatType_32RGBA + * + * If your `MPImage` has a source type of `.image` ensure that the color space is RGB with an + * Alpha channel. + * + * @param image The `MPImage` on which face stylization is to be performed. + * + * @return A `FaceStylizerResult` that contains the stylized image of the most visible face. The + * returned image is copied. The stylized output image size is the same as the model output + * size. The `stylizedImage` of the `FaceStylizerResult` is `nil` if there is no face detected in + * the imput image. `FaceStylizerResult` is `nil` if there is an error in initializing the face + * stylizer. + */ +- (nullable MPPFaceStylizerResult *)stylizeImage:(MPPImage *)image + error:(NSError **)error NS_SWIFT_NAME(stylize(image:)); + +/** + * Performs face stylization on the provided `MPImage` using the whole image as region of interest + * and provides zero-copied results via the given completion handler block. The lifetime of the + * stylized image is only guaranteed for the duration of the block. The method returns synchronously + * once the completion handler returns. + * + * Rotation will be applied according to the `orientation` property of the provided `MPImage`. + * + * This method supports RGBA images. If your `MPImage` has a source type of `.pixelBuffer` or + * `.sampleBuffer`, the underlying pixel buffer must have one of the following pixel format types: + * 1. kCVPixelFormatType_32BGRA + * 2. kCVPixelFormatType_32RGBA + * + * If your `MPImage` has a source type of `.image` ensure that the color space is RGB with an Alpha + * channel. + * + * @param image The `MPImage` on which face stylization is to be performed. + * @param completionHandler A block to be invoked with the results of performing face stylization on + * the imput image. The block takes two arguments, the optional `FaceStylizerResult` that contains + * the zero-copied stylized image if face stylization was successful and an optional error populated + * upon failure. The lifetime of the stylized image is only guaranteed for the duration of the + * block. + */ +- (void)stylizeImage:(MPPImage *)image + withCompletionHandler:(void (^)(MPPFaceStylizerResult *_Nullable result, + NSError *_Nullable error))completionHandler + NS_SWIFT_NAME(stylize(image:completion:)); + +- (instancetype)init NS_UNAVAILABLE; + ++ (instancetype)new NS_UNAVAILABLE; + +@end + +NS_ASSUME_NONNULL_END