From de9acdfa6847443b1f17bf99fe37cfb52af2ddd7 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Tue, 13 Jun 2023 22:17:41 +0530 Subject: [PATCH 01/15] Added iOS segmentation mask --- mediapipe/tasks/ios/vision/core/BUILD | 16 ++ .../tasks/ios/vision/core/sources/MPPMask.h | 107 ++++++++++++ .../tasks/ios/vision/core/sources/MPPMask.mm | 157 ++++++++++++++++++ 3 files changed, 280 insertions(+) create mode 100644 mediapipe/tasks/ios/vision/core/sources/MPPMask.h create mode 100644 mediapipe/tasks/ios/vision/core/sources/MPPMask.mm diff --git a/mediapipe/tasks/ios/vision/core/BUILD b/mediapipe/tasks/ios/vision/core/BUILD index a97410e1..7efa1e7e 100644 --- a/mediapipe/tasks/ios/vision/core/BUILD +++ b/mediapipe/tasks/ios/vision/core/BUILD @@ -64,3 +64,19 @@ objc_library( "@com_google_absl//absl/status:statusor", ], ) + +objc_library( + name = "MPPMask", + srcs = ["sources/MPPMask.mm"], + hdrs = ["sources/MPPMask.h"], + copts = [ + "-ObjC++", + "-std=c++17", + ], + deps = [ + "//mediapipe/tasks/ios/common:MPPCommon", + "//mediapipe/tasks/ios/common/utils:MPPCommonUtils", + "//mediapipe/tasks/ios/core:MPPTaskRunner", + "//third_party/apple_frameworks:CoreVideo", + ], +) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h new file mode 100644 index 00000000..37f253c6 --- /dev/null +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h @@ -0,0 +1,107 @@ +// 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 + +NS_ASSUME_NONNULL_BEGIN + +/** The underlying type of the segmentation mask. */ +typedef NS_ENUM(NSUInteger, MPPMaskDataType) { + + /** Represents the native `UInt8 *` type. */ + MPPMaskDataTypeUInt8, + + /** Represents the native `float *` type. */ + MPPMaskDataTypeFloat32, + +} NS_SWIFT_NAME(MaskDataType); + +/** + * The wrapper class for MediaPipe segmentation masks. + * + * Masks are stored as `UInt8 *` or `float *` objects. + * Every mask is has an underlying type which can be accessed using `dataType`. You can access the + * mask as any other type using the appropriate properties. For eg:, if the underlying type is + * `MPPMaskDataTypeUInt8`, in addition to accessing the mask using `uint8Array`, you can access + * 'floatArray` to get the float 32 data. The first time you access the data as a type different + * from the underlying type, an expensive type conversion is performed. Subsequent accesses return a + * pointer to the memory location fo the same type converted array. As type conversions can be + * expensive, it is recommended to limit the accesses to data of types different from the underlying + * type. + * + * Masks that are returned from a MediaPipe Tasks are owned by by the underlying C++ Task. If you + * need to extend the lifetime of these objects, you can invoke the `[MPPMask copy:]` method. + */ +NS_SWIFT_NAME(Mask) +@interface MPPMask : NSObject + +/** The width of the mask. */ +@property(nonatomic, readonly) CGFloat width; + +/** The height of the mask. */ +@property(nonatomic, readonly) CGFloat height; + +/** The data type of the mask. */ +@property(nonatomic, readonly) MPPMaskDataType dataType; + +/** + * The pointer to the memory location where the underlying mask as a single channel `UInt8` array is + * stored. + */ +@property(nonatomic, readonly, assign) const UInt8 *uint8Data; + +/** + * The pointer to the memory location where the underlying mask as a single channel float 32 array + * is stored. + */ +@property(nonatomic, readonly, assign) const float *float32Data; + +/** + * Initializes an `MPPMask` object of tyep `MPPMaskDataTypeUInt8` with the given `UInt8*` data, + * width and height. + * + * @param uint8Data A pointer to the memory location of the `UInt8` data array. + * @param width The width of the mask. + * @param height The height of the mask. + * + * @return A new `MPPMask` instance with the given `UInt8*` data, width and height. + */ +- (nullable instancetype)initWithUInt8Data:(const UInt8 *)uint8Data + width:(NSInteger)width + height:(NSInteger)height NS_DESIGNATED_INITIALIZER; + +/** + * Initializes an `MPPMask` object of tyep `MPPMaskDataTypeFloat32` with the given `float*` data, + * width and height. + * + * @param uint8Data A pointer to the memory location of the `float` data array. + * @param width The width of the mask. + * @param height The height of the mask. + * + * @return A new `MPPMask` instance with the given `float*` data, width and height. + */ +- (nullable instancetype)initWithFloat32Data:(const float *)float32Data + width:(NSInteger)width + height:(NSInteger)height + error:(NSError **)error 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/MPPMask.mm b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm new file mode 100644 index 00000000..cc633267 --- /dev/null +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm @@ -0,0 +1,157 @@ +// 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/MPPMask.h" +#import "mediapipe/tasks/ios/common/sources/MPPCommon.h" +#import "mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h" + +namespace { +template +T *allocateDataPtr(std::unique_ptr &data, size_t length) { + data = std::unique_ptr(new T[length]); + return data.get(); +} + +template +void copyData(const T *destination, const T *source, size_t length) { + memcpy((void *)destination, source, length * sizeof(T)); +} +} // namespace + +@interface MPPMask () { + const UInt8 *_uint8Data; + const float *_float32Data; + std::unique_ptr _allocatedUInt8Data; + std::unique_ptr _allocatedFloat32Data; +} +@end + +@implementation MPPMask + +- (nullable instancetype)initWithWidth:(NSInteger)width + height:(NSInteger)height + dataType:(MPPMaskDataType)dataType + error:(NSError **)error { + if (dataType < MPPMaskDataTypeUInt8 || dataType > MPPMaskDataTypeFloat32) { + [MPPCommonUtils createCustomError:error + withCode:MPPTasksErrorCodeInvalidArgumentError + description:@"Invalid value for data type."]; + return nil; + } + + self = [super init]; + if (self) { + _width = width; + _height = height; + _dataType = dataType; + } + return self; +} + +- (nullable instancetype)initWithUInt8Data:(const UInt8 *)uint8Data + width:(NSInteger)width + height:(NSInteger)height { + self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeUInt8 error:nil]; + if (self) { + _uint8Data = uint8Data; + } + return self; +} + +- (nullable instancetype)initWithFloat32Data:(const float *)float32Data + width:(NSInteger)width + height:(NSInteger)height { + self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil]; + if (self) { + _float32Data = float32Data; + } + return self; +} + +- (instancetype)initWithUInt8DataToCopy:(const UInt8 *)uint8DataToCopy + width:(NSInteger)width + height:(NSInteger)height { + self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeUInt8 error:nil]; + if (self) { + _uint8Data = allocateDataPtr(_allocatedUInt8Data, _width * _height); + copyData(_uint8Data, uint8DataToCopy, _width * _height); + } + return self; +} + +- (instancetype)initWithFloat32DataToCopy:(const float *)float32DataToCopy + width:(NSInteger)width + height:(NSInteger)height { + self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil]; + if (self) { + _float32Data = allocateDataPtr(_allocatedFloat32Data, _width * _height); + copyData(_float32Data, float32DataToCopy, _width * _height); + } + return self; +} + +- (const UInt8 *)uint8Data { + switch (_dataType) { + case MPPMaskDataTypeUInt8: { + return _uint8Data; + } + case MPPMaskDataTypeFloat32: { + if (_allocatedUInt8Data) { + return _allocatedUInt8Data.get(); + } + UInt8 *data = allocateDataPtr(_allocatedUInt8Data, _width * _height); + for (int i = 0; i < _width * _height; i++) { + data[i] = _float32Data[i] * 255; + } + return data; + } + default: + return NULL; + } +} + +- (const float *)float32Data { + switch (_dataType) { + case MPPMaskDataTypeUInt8: { + if (_allocatedFloat32Data) { + return _allocatedFloat32Data.get(); + } + float *data = allocateDataPtr(_allocatedFloat32Data, _width * _height); + for (int i = 0; i < _width * _height; i++) { + data[i] = _uint8Data[i] / 255; + } + return data; + } + case MPPMaskDataTypeFloat32: { + return _float32Data; + } + default: + return NULL; + } +} + +- (id)copyWithZone:(NSZone *)zone { + switch (_dataType) { + case MPPMaskDataTypeUInt8: + return [[MPPMask alloc] initWithUInt8DataToCopy:self.uint8Data + width:self.width + height:self.height]; + case MPPMaskDataTypeFloat32: + return [[MPPMask alloc] initWithFloat32DataToCopy:self.float32Data + width:self.width + height:self.height]; + } +} + +@end From 5e2bb0e1dbe34fb7ba5018f688cb853e0a2bbaa4 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Tue, 13 Jun 2023 22:19:40 +0530 Subject: [PATCH 02/15] Updated documentation of MPPMask --- mediapipe/tasks/ios/vision/core/sources/MPPMask.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h index 37f253c6..4f16b99b 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h @@ -97,6 +97,10 @@ NS_SWIFT_NAME(Mask) height:(NSInteger)height error:(NSError **)error NS_DESIGNATED_INITIALIZER; + +// TODO: Add methods for CVPixelBuffer conversion. + + /** Unavailable. */ - (instancetype)init NS_UNAVAILABLE; From dddbcc4449c7a4519444259f79a54e89025da661 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Tue, 13 Jun 2023 22:28:09 +0530 Subject: [PATCH 03/15] Updated data types of width and height --- mediapipe/tasks/ios/vision/core/sources/MPPMask.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h index 4f16b99b..c8064cb8 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h @@ -48,10 +48,10 @@ NS_SWIFT_NAME(Mask) @interface MPPMask : NSObject /** The width of the mask. */ -@property(nonatomic, readonly) CGFloat width; +@property(nonatomic, readonly) NSInteger width; /** The height of the mask. */ -@property(nonatomic, readonly) CGFloat height; +@property(nonatomic, readonly) NSInteger height; /** The data type of the mask. */ @property(nonatomic, readonly) MPPMaskDataType dataType; From 2cdb291e544905d85a5a50c085316dd46a53a58e Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Tue, 13 Jun 2023 22:29:15 +0530 Subject: [PATCH 04/15] Removed core video import --- mediapipe/tasks/ios/vision/core/sources/MPPMask.h | 1 - 1 file changed, 1 deletion(-) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h index c8064cb8..1e70ef45 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#import #import NS_ASSUME_NONNULL_BEGIN From c8f85ac060f3cb1358fdd574e3378afdbd230441 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Thu, 15 Jun 2023 14:06:52 +0530 Subject: [PATCH 05/15] Updated signature of initializer in MPPMask --- mediapipe/tasks/ios/vision/core/sources/MPPMask.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h index 1e70ef45..8cdf3af6 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h @@ -93,8 +93,7 @@ NS_SWIFT_NAME(Mask) */ - (nullable instancetype)initWithFloat32Data:(const float *)float32Data width:(NSInteger)width - height:(NSInteger)height - error:(NSError **)error NS_DESIGNATED_INITIALIZER; + height:(NSInteger)height NS_DESIGNATED_INITIALIZER; // TODO: Add methods for CVPixelBuffer conversion. From a7f555fcc2006059b459831e7828a42850a727a2 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Thu, 15 Jun 2023 14:07:33 +0530 Subject: [PATCH 06/15] Fixed float calculations in MPPMask --- mediapipe/tasks/ios/vision/core/sources/MPPMask.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm index cc633267..87e96799 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm @@ -129,7 +129,7 @@ void copyData(const T *destination, const T *source, size_t length) { } float *data = allocateDataPtr(_allocatedFloat32Data, _width * _height); for (int i = 0; i < _width * _height; i++) { - data[i] = _uint8Data[i] / 255; + data[i] = (float)_uint8Data[i] / 255; } return data; } From aa1ab18000a987868143474c0645cb2d2ed99a6f Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Thu, 15 Jun 2023 14:09:22 +0530 Subject: [PATCH 07/15] Updated documentation in MPPMask --- mediapipe/tasks/ios/vision/core/sources/MPPMask.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h index 8cdf3af6..6aa5e3a5 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h @@ -34,7 +34,7 @@ typedef NS_ENUM(NSUInteger, MPPMaskDataType) { * Every mask is has an underlying type which can be accessed using `dataType`. You can access the * mask as any other type using the appropriate properties. For eg:, if the underlying type is * `MPPMaskDataTypeUInt8`, in addition to accessing the mask using `uint8Array`, you can access - * 'floatArray` to get the float 32 data. The first time you access the data as a type different + * 'floatArray` to get the 32 bit float data. The first time you access the data as a type different * from the underlying type, an expensive type conversion is performed. Subsequent accesses return a * pointer to the memory location fo the same type converted array. As type conversions can be * expensive, it is recommended to limit the accesses to data of types different from the underlying From 9d0fed89ffcb20e6f9ec08a0633c4e36ef29a706 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Thu, 15 Jun 2023 14:11:08 +0530 Subject: [PATCH 08/15] Fixed documentation in MPPMask --- mediapipe/tasks/ios/vision/core/sources/MPPMask.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h index 6aa5e3a5..0df60d7d 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h @@ -68,7 +68,7 @@ NS_SWIFT_NAME(Mask) @property(nonatomic, readonly, assign) const float *float32Data; /** - * Initializes an `MPPMask` object of tyep `MPPMaskDataTypeUInt8` with the given `UInt8*` data, + * Initializes an `MPPMask` object of type `MPPMaskDataTypeUInt8` with the given `UInt8*` data, * width and height. * * @param uint8Data A pointer to the memory location of the `UInt8` data array. @@ -82,7 +82,7 @@ NS_SWIFT_NAME(Mask) height:(NSInteger)height NS_DESIGNATED_INITIALIZER; /** - * Initializes an `MPPMask` object of tyep `MPPMaskDataTypeFloat32` with the given `float*` data, + * Initializes an `MPPMask` object of type `MPPMaskDataTypeFloat32` with the given `float*` data, * width and height. * * @param uint8Data A pointer to the memory location of the `float` data array. From 327547ec2b94bea9cbada21bff1b40eb6b1f40a1 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Thu, 15 Jun 2023 14:16:34 +0530 Subject: [PATCH 09/15] Updated variable names in MPPMask --- .../tasks/ios/vision/core/sources/MPPMask.mm | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm index 87e96799..5aac59ec 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm @@ -32,8 +32,8 @@ void copyData(const T *destination, const T *source, size_t length) { @interface MPPMask () { const UInt8 *_uint8Data; const float *_float32Data; - std::unique_ptr _allocatedUInt8Data; - std::unique_ptr _allocatedFloat32Data; + std::unique_ptr _uint8DataPtr; + std::unique_ptr _float32DataPtr; } @end @@ -84,7 +84,7 @@ void copyData(const T *destination, const T *source, size_t length) { height:(NSInteger)height { self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeUInt8 error:nil]; if (self) { - _uint8Data = allocateDataPtr(_allocatedUInt8Data, _width * _height); + _uint8Data = allocateDataPtr(_uint8DataPtr, _width * _height); copyData(_uint8Data, uint8DataToCopy, _width * _height); } return self; @@ -95,7 +95,7 @@ void copyData(const T *destination, const T *source, size_t length) { height:(NSInteger)height { self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil]; if (self) { - _float32Data = allocateDataPtr(_allocatedFloat32Data, _width * _height); + _float32Data = allocateDataPtr(_float32DataPtr, _width * _height); copyData(_float32Data, float32DataToCopy, _width * _height); } return self; @@ -107,10 +107,10 @@ void copyData(const T *destination, const T *source, size_t length) { return _uint8Data; } case MPPMaskDataTypeFloat32: { - if (_allocatedUInt8Data) { - return _allocatedUInt8Data.get(); + if (_uint8DataPtr) { + return _uint8DataPtr.get(); } - UInt8 *data = allocateDataPtr(_allocatedUInt8Data, _width * _height); + UInt8 *data = allocateDataPtr(_uint8DataPtr, _width * _height); for (int i = 0; i < _width * _height; i++) { data[i] = _float32Data[i] * 255; } @@ -124,13 +124,15 @@ void copyData(const T *destination, const T *source, size_t length) { - (const float *)float32Data { switch (_dataType) { case MPPMaskDataTypeUInt8: { - if (_allocatedFloat32Data) { - return _allocatedFloat32Data.get(); + if (_float32DataPtr) { + NSLog(@"Get repeated"); + return _float32DataPtr.get(); } - float *data = allocateDataPtr(_allocatedFloat32Data, _width * _height); + float *data = allocateDataPtr(_float32DataPtr, _width * _height); for (int i = 0; i < _width * _height; i++) { data[i] = (float)_uint8Data[i] / 255; } + NSLog(@"Get new"); return data; } case MPPMaskDataTypeFloat32: { From 1f77fa9de43a9d0f995f4e9981478291aaefb701 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Thu, 15 Jun 2023 16:07:56 +0530 Subject: [PATCH 10/15] Removed generic methods for alloc and memcpy from MPPMask --- .../tasks/ios/vision/core/sources/MPPMask.h | 2 - .../tasks/ios/vision/core/sources/MPPMask.mm | 41 ++++++++----------- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h index 0df60d7d..65af32d1 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h @@ -95,10 +95,8 @@ NS_SWIFT_NAME(Mask) width:(NSInteger)width height:(NSInteger)height NS_DESIGNATED_INITIALIZER; - // TODO: Add methods for CVPixelBuffer conversion. - /** Unavailable. */ - (instancetype)init NS_UNAVAILABLE; diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm index 5aac59ec..84a4eb4b 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm @@ -16,19 +16,6 @@ #import "mediapipe/tasks/ios/common/sources/MPPCommon.h" #import "mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h" -namespace { -template -T *allocateDataPtr(std::unique_ptr &data, size_t length) { - data = std::unique_ptr(new T[length]); - return data.get(); -} - -template -void copyData(const T *destination, const T *source, size_t length) { - memcpy((void *)destination, source, length * sizeof(T)); -} -} // namespace - @interface MPPMask () { const UInt8 *_uint8Data; const float *_float32Data; @@ -84,8 +71,10 @@ void copyData(const T *destination, const T *source, size_t length) { height:(NSInteger)height { self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeUInt8 error:nil]; if (self) { - _uint8Data = allocateDataPtr(_uint8DataPtr, _width * _height); - copyData(_uint8Data, uint8DataToCopy, _width * _height); + size_t length = _width * _height; + _uint8DataPtr = std::unique_ptr(new UInt8[length]); + _uint8Data = _uint8DataPtr.get(); + memcpy((UInt8 *)_uint8Data, uint8DataToCopy, length * sizeof(UInt8)); } return self; } @@ -95,8 +84,10 @@ void copyData(const T *destination, const T *source, size_t length) { height:(NSInteger)height { self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil]; if (self) { - _float32Data = allocateDataPtr(_float32DataPtr, _width * _height); - copyData(_float32Data, float32DataToCopy, _width * _height); + size_t length = _width * _height; + _float32DataPtr = std::unique_ptr(new float[length]); + _float32Data = _float32DataPtr.get(); + memcpy((float *)_float32Data, float32DataToCopy, length * sizeof(float)); } return self; } @@ -110,8 +101,11 @@ void copyData(const T *destination, const T *source, size_t length) { if (_uint8DataPtr) { return _uint8DataPtr.get(); } - UInt8 *data = allocateDataPtr(_uint8DataPtr, _width * _height); - for (int i = 0; i < _width * _height; i++) { + + size_t length = _width * _height; + _uint8DataPtr = std::unique_ptr(new UInt8[length]); + UInt8 *data = _uint8DataPtr.get(); + for (int i = 0; i < length; i++) { data[i] = _float32Data[i] * 255; } return data; @@ -125,14 +119,15 @@ void copyData(const T *destination, const T *source, size_t length) { switch (_dataType) { case MPPMaskDataTypeUInt8: { if (_float32DataPtr) { - NSLog(@"Get repeated"); return _float32DataPtr.get(); } - float *data = allocateDataPtr(_float32DataPtr, _width * _height); - for (int i = 0; i < _width * _height; i++) { + + size_t length = _width * _height; + _float32DataPtr = std::unique_ptr(new float[length]); + float *data = _float32DataPtr.get(); + for (int i = 0; i < length; i++) { data[i] = (float)_uint8Data[i] / 255; } - NSLog(@"Get new"); return data; } case MPPMaskDataTypeFloat32: { From 83486ed01b7e948b4d7dd0d8f356a3ae4970821c Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Fri, 16 Jun 2023 19:56:04 +0530 Subject: [PATCH 11/15] Updated init method implementations in MPPMask --- mediapipe/framework/tool/ios.bzl | 2 +- .../tasks/ios/vision/core/sources/MPPMask.h | 17 +++++- .../tasks/ios/vision/core/sources/MPPMask.mm | 56 ++++++++----------- 3 files changed, 39 insertions(+), 36 deletions(-) diff --git a/mediapipe/framework/tool/ios.bzl b/mediapipe/framework/tool/ios.bzl index c97b092e..a0fe0be5 100644 --- a/mediapipe/framework/tool/ios.bzl +++ b/mediapipe/framework/tool/ios.bzl @@ -14,7 +14,7 @@ """MediaPipe Task Library Helper Rules for iOS""" -MPP_TASK_MINIMUM_OS_VERSION = "11.0" +MPP_TASK_MINIMUM_OS_VERSION = "12.0" # When the static framework is built with bazel, the all header files are moved # to the "Headers" directory with no header path prefixes. This auxiliary rule diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h index 65af32d1..176e9b20 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h @@ -71,21 +71,31 @@ NS_SWIFT_NAME(Mask) * Initializes an `MPPMask` object of type `MPPMaskDataTypeUInt8` with the given `UInt8*` data, * width and height. * + * If `shouldCopy` is set to `YES`, the newly created `MPPMask` stores a reference to a deep copied + * `uint8Data`. Since deep copies are expensive, it is recommended to not set `shouldCopy` unless + * the `MPPMask` must outlive the passed in `uint8Data`. + * * @param uint8Data A pointer to the memory location of the `UInt8` data array. * @param width The width of the mask. * @param height The height of the mask. + * @param shouldCopy The height of the mask. * * @return A new `MPPMask` instance with the given `UInt8*` data, width and height. */ - (nullable instancetype)initWithUInt8Data:(const UInt8 *)uint8Data width:(NSInteger)width - height:(NSInteger)height NS_DESIGNATED_INITIALIZER; + height:(NSInteger)height + shouldCopy:(BOOL)shouldCopy NS_DESIGNATED_INITIALIZER; /** * Initializes an `MPPMask` object of type `MPPMaskDataTypeFloat32` with the given `float*` data, * width and height. * - * @param uint8Data A pointer to the memory location of the `float` data array. + * If `shouldCopy` is set to `YES`, the newly created `MPPMask` stores a reference to a deep copied + * `float32Data`. Since deep copies are expensive, it is recommended to not set `shouldCopy` unless + * the `MPPMask` must outlive the passed in `float32Data`. + * + * @param float32Data A pointer to the memory location of the `float` data array. * @param width The width of the mask. * @param height The height of the mask. * @@ -93,7 +103,8 @@ NS_SWIFT_NAME(Mask) */ - (nullable instancetype)initWithFloat32Data:(const float *)float32Data width:(NSInteger)width - height:(NSInteger)height NS_DESIGNATED_INITIALIZER; + height:(NSInteger)height + shouldCopy:(BOOL)shouldCopy NS_DESIGNATED_INITIALIZER; // TODO: Add methods for CVPixelBuffer conversion. diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm index 84a4eb4b..3342218a 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm @@ -48,46 +48,36 @@ - (nullable instancetype)initWithUInt8Data:(const UInt8 *)uint8Data width:(NSInteger)width - height:(NSInteger)height { + height:(NSInteger)height + shouldCopy:(BOOL)shouldCopy { self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeUInt8 error:nil]; if (self) { - _uint8Data = uint8Data; + if (shouldCopy) { + size_t length = _width * _height; + _float32DataPtr = std::unique_ptr(new float[length]); + _float32Data = _float32DataPtr.get(); + memcpy((float *)_float32Data, float32DataToCopy, length * sizeof(float)); + } else { + _uint8Data = uint8Data; + } } return self; } - (nullable instancetype)initWithFloat32Data:(const float *)float32Data width:(NSInteger)width - height:(NSInteger)height { + height:(NSInteger)height + shouldCopy:(BOO)shouldCopy { self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil]; if (self) { - _float32Data = float32Data; - } - return self; -} - -- (instancetype)initWithUInt8DataToCopy:(const UInt8 *)uint8DataToCopy - width:(NSInteger)width - height:(NSInteger)height { - self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeUInt8 error:nil]; - if (self) { - size_t length = _width * _height; - _uint8DataPtr = std::unique_ptr(new UInt8[length]); - _uint8Data = _uint8DataPtr.get(); - memcpy((UInt8 *)_uint8Data, uint8DataToCopy, length * sizeof(UInt8)); - } - return self; -} - -- (instancetype)initWithFloat32DataToCopy:(const float *)float32DataToCopy - width:(NSInteger)width - height:(NSInteger)height { - self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil]; - if (self) { - size_t length = _width * _height; - _float32DataPtr = std::unique_ptr(new float[length]); - _float32Data = _float32DataPtr.get(); - memcpy((float *)_float32Data, float32DataToCopy, length * sizeof(float)); + if (shouldCopy) { + size_t length = _width * _height; + _uint8DataPtr = std::unique_ptr(new UInt8[length]); + _uint8Data = _uint8DataPtr.get(); + memcpy((UInt8 *)_uint8Data, uint8DataToCopy, length * sizeof(UInt8)); + } else { + _float32Data = float32Data; + } } return self; } @@ -143,11 +133,13 @@ case MPPMaskDataTypeUInt8: return [[MPPMask alloc] initWithUInt8DataToCopy:self.uint8Data width:self.width - height:self.height]; + height:self.height + shouldCopy:YES]; case MPPMaskDataTypeFloat32: return [[MPPMask alloc] initWithFloat32DataToCopy:self.float32Data width:self.width - height:self.height]; + height:self.height + shouldCopy:YES]; } } From 52f6b8d8993b8f1c79d8157a1933e2cfe7e96812 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Fri, 16 Jun 2023 19:56:23 +0530 Subject: [PATCH 12/15] Revert "Updated init method implementations in MPPMask" This reverts commit 83486ed01b7e948b4d7dd0d8f356a3ae4970821c. --- mediapipe/framework/tool/ios.bzl | 2 +- .../tasks/ios/vision/core/sources/MPPMask.h | 17 +----- .../tasks/ios/vision/core/sources/MPPMask.mm | 56 +++++++++++-------- 3 files changed, 36 insertions(+), 39 deletions(-) diff --git a/mediapipe/framework/tool/ios.bzl b/mediapipe/framework/tool/ios.bzl index a0fe0be5..c97b092e 100644 --- a/mediapipe/framework/tool/ios.bzl +++ b/mediapipe/framework/tool/ios.bzl @@ -14,7 +14,7 @@ """MediaPipe Task Library Helper Rules for iOS""" -MPP_TASK_MINIMUM_OS_VERSION = "12.0" +MPP_TASK_MINIMUM_OS_VERSION = "11.0" # When the static framework is built with bazel, the all header files are moved # to the "Headers" directory with no header path prefixes. This auxiliary rule diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h index 176e9b20..65af32d1 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h @@ -71,31 +71,21 @@ NS_SWIFT_NAME(Mask) * Initializes an `MPPMask` object of type `MPPMaskDataTypeUInt8` with the given `UInt8*` data, * width and height. * - * If `shouldCopy` is set to `YES`, the newly created `MPPMask` stores a reference to a deep copied - * `uint8Data`. Since deep copies are expensive, it is recommended to not set `shouldCopy` unless - * the `MPPMask` must outlive the passed in `uint8Data`. - * * @param uint8Data A pointer to the memory location of the `UInt8` data array. * @param width The width of the mask. * @param height The height of the mask. - * @param shouldCopy The height of the mask. * * @return A new `MPPMask` instance with the given `UInt8*` data, width and height. */ - (nullable instancetype)initWithUInt8Data:(const UInt8 *)uint8Data width:(NSInteger)width - height:(NSInteger)height - shouldCopy:(BOOL)shouldCopy NS_DESIGNATED_INITIALIZER; + height:(NSInteger)height NS_DESIGNATED_INITIALIZER; /** * Initializes an `MPPMask` object of type `MPPMaskDataTypeFloat32` with the given `float*` data, * width and height. * - * If `shouldCopy` is set to `YES`, the newly created `MPPMask` stores a reference to a deep copied - * `float32Data`. Since deep copies are expensive, it is recommended to not set `shouldCopy` unless - * the `MPPMask` must outlive the passed in `float32Data`. - * - * @param float32Data A pointer to the memory location of the `float` data array. + * @param uint8Data A pointer to the memory location of the `float` data array. * @param width The width of the mask. * @param height The height of the mask. * @@ -103,8 +93,7 @@ NS_SWIFT_NAME(Mask) */ - (nullable instancetype)initWithFloat32Data:(const float *)float32Data width:(NSInteger)width - height:(NSInteger)height - shouldCopy:(BOOL)shouldCopy NS_DESIGNATED_INITIALIZER; + height:(NSInteger)height NS_DESIGNATED_INITIALIZER; // TODO: Add methods for CVPixelBuffer conversion. diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm index 3342218a..84a4eb4b 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm @@ -48,36 +48,46 @@ - (nullable instancetype)initWithUInt8Data:(const UInt8 *)uint8Data width:(NSInteger)width - height:(NSInteger)height - shouldCopy:(BOOL)shouldCopy { + height:(NSInteger)height { self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeUInt8 error:nil]; if (self) { - if (shouldCopy) { - size_t length = _width * _height; - _float32DataPtr = std::unique_ptr(new float[length]); - _float32Data = _float32DataPtr.get(); - memcpy((float *)_float32Data, float32DataToCopy, length * sizeof(float)); - } else { - _uint8Data = uint8Data; - } + _uint8Data = uint8Data; } return self; } - (nullable instancetype)initWithFloat32Data:(const float *)float32Data width:(NSInteger)width - height:(NSInteger)height - shouldCopy:(BOO)shouldCopy { + height:(NSInteger)height { self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil]; if (self) { - if (shouldCopy) { - size_t length = _width * _height; - _uint8DataPtr = std::unique_ptr(new UInt8[length]); - _uint8Data = _uint8DataPtr.get(); - memcpy((UInt8 *)_uint8Data, uint8DataToCopy, length * sizeof(UInt8)); - } else { - _float32Data = float32Data; - } + _float32Data = float32Data; + } + return self; +} + +- (instancetype)initWithUInt8DataToCopy:(const UInt8 *)uint8DataToCopy + width:(NSInteger)width + height:(NSInteger)height { + self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeUInt8 error:nil]; + if (self) { + size_t length = _width * _height; + _uint8DataPtr = std::unique_ptr(new UInt8[length]); + _uint8Data = _uint8DataPtr.get(); + memcpy((UInt8 *)_uint8Data, uint8DataToCopy, length * sizeof(UInt8)); + } + return self; +} + +- (instancetype)initWithFloat32DataToCopy:(const float *)float32DataToCopy + width:(NSInteger)width + height:(NSInteger)height { + self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil]; + if (self) { + size_t length = _width * _height; + _float32DataPtr = std::unique_ptr(new float[length]); + _float32Data = _float32DataPtr.get(); + memcpy((float *)_float32Data, float32DataToCopy, length * sizeof(float)); } return self; } @@ -133,13 +143,11 @@ case MPPMaskDataTypeUInt8: return [[MPPMask alloc] initWithUInt8DataToCopy:self.uint8Data width:self.width - height:self.height - shouldCopy:YES]; + height:self.height]; case MPPMaskDataTypeFloat32: return [[MPPMask alloc] initWithFloat32DataToCopy:self.float32Data width:self.width - height:self.height - shouldCopy:YES]; + height:self.height]; } } From fec2fc77e00f622f149519c49303b015deeff31e Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Fri, 16 Jun 2023 19:56:32 +0530 Subject: [PATCH 13/15] Revert "Revert "Updated init method implementations in MPPMask"" This reverts commit 52f6b8d8993b8f1c79d8157a1933e2cfe7e96812. --- mediapipe/framework/tool/ios.bzl | 2 +- .../tasks/ios/vision/core/sources/MPPMask.h | 17 +++++- .../tasks/ios/vision/core/sources/MPPMask.mm | 56 ++++++++----------- 3 files changed, 39 insertions(+), 36 deletions(-) diff --git a/mediapipe/framework/tool/ios.bzl b/mediapipe/framework/tool/ios.bzl index c97b092e..a0fe0be5 100644 --- a/mediapipe/framework/tool/ios.bzl +++ b/mediapipe/framework/tool/ios.bzl @@ -14,7 +14,7 @@ """MediaPipe Task Library Helper Rules for iOS""" -MPP_TASK_MINIMUM_OS_VERSION = "11.0" +MPP_TASK_MINIMUM_OS_VERSION = "12.0" # When the static framework is built with bazel, the all header files are moved # to the "Headers" directory with no header path prefixes. This auxiliary rule diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h index 65af32d1..176e9b20 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h @@ -71,21 +71,31 @@ NS_SWIFT_NAME(Mask) * Initializes an `MPPMask` object of type `MPPMaskDataTypeUInt8` with the given `UInt8*` data, * width and height. * + * If `shouldCopy` is set to `YES`, the newly created `MPPMask` stores a reference to a deep copied + * `uint8Data`. Since deep copies are expensive, it is recommended to not set `shouldCopy` unless + * the `MPPMask` must outlive the passed in `uint8Data`. + * * @param uint8Data A pointer to the memory location of the `UInt8` data array. * @param width The width of the mask. * @param height The height of the mask. + * @param shouldCopy The height of the mask. * * @return A new `MPPMask` instance with the given `UInt8*` data, width and height. */ - (nullable instancetype)initWithUInt8Data:(const UInt8 *)uint8Data width:(NSInteger)width - height:(NSInteger)height NS_DESIGNATED_INITIALIZER; + height:(NSInteger)height + shouldCopy:(BOOL)shouldCopy NS_DESIGNATED_INITIALIZER; /** * Initializes an `MPPMask` object of type `MPPMaskDataTypeFloat32` with the given `float*` data, * width and height. * - * @param uint8Data A pointer to the memory location of the `float` data array. + * If `shouldCopy` is set to `YES`, the newly created `MPPMask` stores a reference to a deep copied + * `float32Data`. Since deep copies are expensive, it is recommended to not set `shouldCopy` unless + * the `MPPMask` must outlive the passed in `float32Data`. + * + * @param float32Data A pointer to the memory location of the `float` data array. * @param width The width of the mask. * @param height The height of the mask. * @@ -93,7 +103,8 @@ NS_SWIFT_NAME(Mask) */ - (nullable instancetype)initWithFloat32Data:(const float *)float32Data width:(NSInteger)width - height:(NSInteger)height NS_DESIGNATED_INITIALIZER; + height:(NSInteger)height + shouldCopy:(BOOL)shouldCopy NS_DESIGNATED_INITIALIZER; // TODO: Add methods for CVPixelBuffer conversion. diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm index 84a4eb4b..3342218a 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm @@ -48,46 +48,36 @@ - (nullable instancetype)initWithUInt8Data:(const UInt8 *)uint8Data width:(NSInteger)width - height:(NSInteger)height { + height:(NSInteger)height + shouldCopy:(BOOL)shouldCopy { self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeUInt8 error:nil]; if (self) { - _uint8Data = uint8Data; + if (shouldCopy) { + size_t length = _width * _height; + _float32DataPtr = std::unique_ptr(new float[length]); + _float32Data = _float32DataPtr.get(); + memcpy((float *)_float32Data, float32DataToCopy, length * sizeof(float)); + } else { + _uint8Data = uint8Data; + } } return self; } - (nullable instancetype)initWithFloat32Data:(const float *)float32Data width:(NSInteger)width - height:(NSInteger)height { + height:(NSInteger)height + shouldCopy:(BOO)shouldCopy { self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil]; if (self) { - _float32Data = float32Data; - } - return self; -} - -- (instancetype)initWithUInt8DataToCopy:(const UInt8 *)uint8DataToCopy - width:(NSInteger)width - height:(NSInteger)height { - self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeUInt8 error:nil]; - if (self) { - size_t length = _width * _height; - _uint8DataPtr = std::unique_ptr(new UInt8[length]); - _uint8Data = _uint8DataPtr.get(); - memcpy((UInt8 *)_uint8Data, uint8DataToCopy, length * sizeof(UInt8)); - } - return self; -} - -- (instancetype)initWithFloat32DataToCopy:(const float *)float32DataToCopy - width:(NSInteger)width - height:(NSInteger)height { - self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil]; - if (self) { - size_t length = _width * _height; - _float32DataPtr = std::unique_ptr(new float[length]); - _float32Data = _float32DataPtr.get(); - memcpy((float *)_float32Data, float32DataToCopy, length * sizeof(float)); + if (shouldCopy) { + size_t length = _width * _height; + _uint8DataPtr = std::unique_ptr(new UInt8[length]); + _uint8Data = _uint8DataPtr.get(); + memcpy((UInt8 *)_uint8Data, uint8DataToCopy, length * sizeof(UInt8)); + } else { + _float32Data = float32Data; + } } return self; } @@ -143,11 +133,13 @@ case MPPMaskDataTypeUInt8: return [[MPPMask alloc] initWithUInt8DataToCopy:self.uint8Data width:self.width - height:self.height]; + height:self.height + shouldCopy:YES]; case MPPMaskDataTypeFloat32: return [[MPPMask alloc] initWithFloat32DataToCopy:self.float32Data width:self.width - height:self.height]; + height:self.height + shouldCopy:YES]; } } From 4ab1a5de1b8d12bcb190ce6f91ea63dd623c3149 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Fri, 16 Jun 2023 19:59:59 +0530 Subject: [PATCH 14/15] Reverted changes to iOS tasks deployment target --- mediapipe/framework/tool/ios.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediapipe/framework/tool/ios.bzl b/mediapipe/framework/tool/ios.bzl index a0fe0be5..c97b092e 100644 --- a/mediapipe/framework/tool/ios.bzl +++ b/mediapipe/framework/tool/ios.bzl @@ -14,7 +14,7 @@ """MediaPipe Task Library Helper Rules for iOS""" -MPP_TASK_MINIMUM_OS_VERSION = "12.0" +MPP_TASK_MINIMUM_OS_VERSION = "11.0" # When the static framework is built with bazel, the all header files are moved # to the "Headers" directory with no header path prefixes. This auxiliary rule From d12dd88f518af3c7bc584e5044ffd55ceffdd8be Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Fri, 16 Jun 2023 20:00:30 +0530 Subject: [PATCH 15/15] Fixed implementation of init methods in MPPMask --- .../tasks/ios/vision/core/sources/MPPMask.mm | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm index 3342218a..b1a6ca21 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm @@ -54,9 +54,9 @@ if (self) { if (shouldCopy) { size_t length = _width * _height; - _float32DataPtr = std::unique_ptr(new float[length]); - _float32Data = _float32DataPtr.get(); - memcpy((float *)_float32Data, float32DataToCopy, length * sizeof(float)); + _uint8DataPtr = std::unique_ptr(new UInt8[length]); + _uint8Data = _uint8DataPtr.get(); + memcpy((UInt8 *)_uint8Data, uint8Data, length * sizeof(UInt8)); } else { _uint8Data = uint8Data; } @@ -67,14 +67,14 @@ - (nullable instancetype)initWithFloat32Data:(const float *)float32Data width:(NSInteger)width height:(NSInteger)height - shouldCopy:(BOO)shouldCopy { + shouldCopy:(BOOL)shouldCopy { self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil]; if (self) { if (shouldCopy) { - size_t length = _width * _height; - _uint8DataPtr = std::unique_ptr(new UInt8[length]); - _uint8Data = _uint8DataPtr.get(); - memcpy((UInt8 *)_uint8Data, uint8DataToCopy, length * sizeof(UInt8)); + size_t length = _width * _height; + _float32DataPtr = std::unique_ptr(new float[length]); + _float32Data = _float32DataPtr.get(); + memcpy((float *)_float32Data, float32Data, length * sizeof(float)); } else { _float32Data = float32Data; } @@ -131,12 +131,12 @@ - (id)copyWithZone:(NSZone *)zone { switch (_dataType) { case MPPMaskDataTypeUInt8: - return [[MPPMask alloc] initWithUInt8DataToCopy:self.uint8Data + return [[MPPMask alloc] initWithUInt8Data:self.uint8Data width:self.width height:self.height shouldCopy:YES]; case MPPMaskDataTypeFloat32: - return [[MPPMask alloc] initWithFloat32DataToCopy:self.float32Data + return [[MPPMask alloc] initWithFloat32Data:self.float32Data width:self.width height:self.height shouldCopy:YES];