Merge pull request #4497 from priankakariatyml:ios-hand-landmarker-utils

PiperOrigin-RevId: 538238944
This commit is contained in:
Copybara-Service 2023-06-06 11:17:45 -07:00
commit 70e00b4dbe
9 changed files with 572 additions and 0 deletions

View File

@ -60,3 +60,9 @@ objc_library(
srcs = ["sources/MPPLandmark.m"], srcs = ["sources/MPPLandmark.m"],
hdrs = ["sources/MPPLandmark.h"], hdrs = ["sources/MPPLandmark.h"],
) )
objc_library(
name = "MPPConnection",
srcs = ["sources/MPPConnection.m"],
hdrs = ["sources/MPPConnection.h"],
)

View File

@ -0,0 +1,44 @@
// 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>
NS_ASSUME_NONNULL_BEGIN
/** The value class representing a landmark connection. */
NS_SWIFT_NAME(Connection)
@interface MPPConnection : NSObject
@property(nonatomic, readonly) NSUInteger start;
@property(nonatomic, readonly) NSUInteger end;
/**
* Initializes a new `MPPConnection` with the start and end landmarks integer constants.
*
* @param start The integer representing the starting landmark of the connection.
* @param end The integer representing the ending landmark of the connection.
*
* @return An instance of `MPPConnection` initialized with the given start and end landmarks integer
* constants.
*/
- (instancetype)initWithStart:(NSUInteger)start end:(NSUInteger)end NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
@end
NS_ASSUME_NONNULL_END

View File

@ -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.
#import "mediapipe/tasks/ios/components/containers/sources/MPPConnection.h"
@implementation MPPConnection
- (instancetype)initWithStart:(NSUInteger)start end:(NSUInteger)end {
self = [super init];
if (self) {
_start = start;
_end = end;
}
return self;
}
@end

View File

@ -37,3 +37,26 @@ objc_library(
"//mediapipe/tasks/ios/vision/core:MPPRunningMode", "//mediapipe/tasks/ios/vision/core:MPPRunningMode",
], ],
) )
objc_library(
name = "MPPHandLandmarker",
hdrs = ["sources/MPPHandLandmarker.h"],
copts = [
"-ObjC++",
"-std=c++17",
"-x objective-c++",
],
module_name = "MPPHandLandmarker",
deps = [
":MPPHandLandmarkerOptions",
":MPPHandLandmarkerResult",
"//mediapipe/tasks/ios/components/containers:MPPConnection",
"//mediapipe/tasks/ios/vision/core:MPPImage",
],
)
objc_library(
name = "MPPHandLandmark",
hdrs = ["sources/MPPHandLandmark.h"],
module_name = "MPPHandLandmark",
)

View File

@ -0,0 +1,65 @@
// 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>
NS_ASSUME_NONNULL_BEGIN
/**
* The enum containing the 21 hand landmarks.
*/
typedef NS_ENUM(NSUInteger, MPPHandLandmark) {
MPPHandLandmarkWrist,
MPPHandLandmarkThumbCMC,
MPPHandLandmarkThumbMCP,
MPPHandLandmarkThumbIP,
MPPHandLandmarkIndexFingerMCP,
MPPHandLandmarkIndexFingerPIP,
MPPHandLandmarkIndexFingerDIP,
MPPHandLandmarkIndexFingerTIP,
MPPHandLandmarkMiddleFingerMCP,
MPPHandLandmarkMiddleFingerPIP,
MPPHandLandmarkMiddleFingerDIP,
MPPHandLandmarkMiddleFingerTIP,
MPPHandLandmarkRingFingerMCP,
MPPHandLandmarkRingFingerPIP,
MPPHandLandmarkRingFingerDIP,
MPPHandLandmarkRingFingerTIP,
MPPHandLandmarkPinkyMCP,
MPPHandLandmarkPinkyPIP,
MPPHandLandmarkPinkyDIP,
MPPHandLandmarkPinkyTIP,
} NS_SWIFT_NAME(HandLandmark);
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,205 @@
// 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/components/containers/sources/MPPConnection.h"
#import "mediapipe/tasks/ios/vision/core/sources/MPPImage.h"
#import "mediapipe/tasks/ios/vision/hand_landmarker/sources/MPPHandLandmarkerOptions.h"
#import "mediapipe/tasks/ios/vision/hand_landmarker/sources/MPPHandLandmarkerResult.h"
NS_ASSUME_NONNULL_BEGIN
/**
* @brief Performs hand landmarks detection on images.
*
* This API expects a pre-trained hand landmarks model asset bundle.
*/
NS_SWIFT_NAME(HandLandmarker)
@interface MPPHandLandmarker : NSObject
/**
* Creates a new instance of `MPPHandLandmarker` from an absolute path to a model asset bundle
* stored locally on the device and the default `MPPHandLandmarkerOptions`.
*
* @param modelPath An absolute path to a model asset bundle stored locally on the device.
* @param error An optional error parameter populated when there is an error in initializing the
* hand landmarker.
*
* @return A new instance of `MPPHandLandmarker` with the given model path. `nil` if there is an
* error in initializing the hand landmarker.
*/
- (nullable instancetype)initWithModelPath:(NSString *)modelPath error:(NSError **)error;
/**
* Creates a new instance of `MPPHandLandmarker` from the given `MPPHandLandmarkerOptions`.
*
* @param options The options of type `MPPHandLandmarkerOptions` to use for configuring the
* `MPPHandLandmarker`.
* @param error An optional error parameter populated when there is an error in initializing the
* hand landmarker.
*
* @return A new instance of `MPPHandLandmarker` with the given options. `nil` if there is an
* error in initializing the hand landmarker.
*/
- (nullable instancetype)initWithOptions:(MPPHandLandmarkerOptions *)options
error:(NSError **)error NS_DESIGNATED_INITIALIZER;
/**
* Performs hand landmarks detection on the provided `MPPImage` using the whole image as region of
* interest. Rotation will be applied according to the `orientation` property of the provided
* `MPPImage`. Only use this method when the `MPPHandLandmarker` is created with
* `MPPRunningModeImage`.
*
* This method supports performing hand landmarks detection on RGBA images. If your `MPPImage` has a
* source type of `MPPImageSourceTypePixelBuffer` or `MPPImageSourceTypeSampleBuffer`, the
* underlying pixel buffer must have one of the following pixel format types:
* 1. kCVPixelFormatType_32BGRA
* 2. kCVPixelFormatType_32RGBA
*
* If your `MPPImage` has a source type of `MPPImageSourceTypeImage` ensure that the color space is
* RGB with an Alpha channel.
*
* @param image The `MPPImage` on which hand landmarks detection is to be performed.
* @param error An optional error parameter populated when there is an error in performing hand
* landmarks detection on the input image.
*
* @return An `MPPHandLandmarkerResult` object that contains the hand hand landmarks detection
* results.
*/
- (nullable MPPHandLandmarkerResult *)detectInImage:(MPPImage *)image
error:(NSError **)error NS_SWIFT_NAME(detect(image:));
/**
* Performs hand landmarks detection on the provided video frame of type `MPPImage` using the whole
* image as region of interest. Rotation will be applied according to the `orientation` property of
* the provided `MPPImage`. Only use this method when the `MPPHandLandmarker` is created with
* `MPPRunningModeVideo`.
*
* It's required to provide the video frame's timestamp (in milliseconds). The input timestamps must
* be monotonically increasing.
*
* This method supports performing hand landmarks detection on RGBA images. If your `MPPImage` has a
* source type of `MPPImageSourceTypePixelBuffer` or `MPPImageSourceTypeSampleBuffer`, the
* underlying pixel buffer must have one of the following pixel format types:
* 1. kCVPixelFormatType_32BGRA
* 2. kCVPixelFormatType_32RGBA
*
* If your `MPPImage` has a source type of `MPPImageSourceTypeImage` ensure that the color space is
* RGB with an Alpha channel.
*
* @param image The `MPPImage` on which hand landmarks detection is to be performed.
* @param timestampInMilliseconds The video frame's timestamp (in milliseconds). The input
* timestamps must be monotonically increasing.
* @param error An optional error parameter populated when there is an error in performing hand
* landmarks detection on the input video frame.
*
* @return An `MPPHandLandmarkerResult` object that contains the hand hand landmarks detection
* results.
*/
- (nullable MPPHandLandmarkerResult *)detectInVideoFrame:(MPPImage *)image
timestampInMilliseconds:(NSInteger)timestampInMilliseconds
error:(NSError **)error
NS_SWIFT_NAME(detect(videoFrame:timestampInMilliseconds:));
/**
* Sends live stream image data of type `MPPImage` to perform hand landmarks detection using the
* whole image as region of interest. Rotation will be applied according to the `orientation`
* property of the provided `MPPImage`. Only use this method when the `MPPHandLandmarker` is created
* with `MPPRunningModeLiveStream`.
*
* The object which needs to be continuously notified of the available results of hand landmarks
* detection must confirm to `MPPHandLandmarkerLiveStreamDelegate` protocol and implement the
* `handLandmarker:didFinishDetectionWithResult:timestampInMilliseconds:error:`
* delegate method.
*
* It's required to provide a timestamp (in milliseconds) to indicate when the input image is sent
* to the hand landmarker. The input timestamps must be monotonically increasing.
*
* This method supports performing hand landmarks detection on RGBA images. If your `MPPImage` has a
* source type of `MPPImageSourceTypePixelBuffer` or `MPPImageSourceTypeSampleBuffer`, the
* underlying pixel buffer must have one of the following pixel format types:
* 1. kCVPixelFormatType_32BGRA
* 2. kCVPixelFormatType_32RGBA
*
* If the input `MPPImage` has a source type of `MPPImageSourceTypeImage` ensure that the color
* space is RGB with an Alpha channel.
*
* If this method is used for performing hand landmarks detection on live camera frames using
* `AVFoundation`, ensure that you request `AVCaptureVideoDataOutput` to output frames in
* `kCMPixelFormat_32RGBA` using its `videoSettings` property.
*
* @param image A live stream image data of type `MPPImage` on which hand landmarks detection is to
* be performed.
* @param timestampInMilliseconds The timestamp (in milliseconds) which indicates when the input
* image is sent to the hand landmarker. The input timestamps must be monotonically increasing.
* @param error An optional error parameter populated when there is an error in performing hand
* landmarks detection on the input live stream image data.
*
* @return `YES` if the image was sent to the task successfully, otherwise `NO`.
*/
- (BOOL)detectAsyncInImage:(MPPImage *)image
timestampInMilliseconds:(NSInteger)timestampInMilliseconds
error:(NSError **)error
NS_SWIFT_NAME(detectAsync(image:timestampInMilliseconds:));
- (instancetype)init NS_UNAVAILABLE;
/**
* Returns the connections between the landmarks in the palm.
*
* @return An array of connections between the landmarks in the palm.
*/
+ (NSArray<MPPConnection *> *)handPalmConnections;
/**
* Returns the connections between the landmarks in the index finger.
*
* @return An array of connections between the landmarks in the index finger.
*/
+ (NSArray<MPPConnection *> *)handIndexFingerConnections;
/**
* Returns the connections between the landmarks in the middle finger.
*
* @return An array of connections between the landmarks in the middle finger.
*/
+ (NSArray<MPPConnection *> *)handMiddleFingerConnections;
/**
* Returns the connections between the landmarks in the ring finger.
*
* @return An array of connections between the landmarks in the ring finger.
*/
+ (NSArray<MPPConnection *> *)handRingFingerConnections;
/**
* Returns the connections between the landmarks in the pinky.
*
* @return An array of connections between the landmarks in the pinky.
*/
+ (NSArray<MPPConnection *> *)handPinkyConnections;
/**
* Returns the connections between all the landmarks in the hand.
*
* @return An array of connections between all the landmarks in the hand.
*/
+ (NSArray<MPPConnection *> *)handConnections;
+ (instancetype)new NS_UNAVAILABLE;
@end
NS_ASSUME_NONNULL_END

View File

@ -31,3 +31,17 @@ objc_library(
"//mediapipe/tasks/ios/vision/hand_landmarker:MPPHandLandmarkerOptions", "//mediapipe/tasks/ios/vision/hand_landmarker:MPPHandLandmarkerOptions",
], ],
) )
objc_library(
name = "MPPHandLandmarkerResultHelpers",
srcs = ["sources/MPPHandLandmarkerResult+Helpers.mm"],
hdrs = ["sources/MPPHandLandmarkerResult+Helpers.h"],
deps = [
"//mediapipe/framework:packet",
"//mediapipe/framework/formats:classification_cc_proto",
"//mediapipe/framework/formats:landmark_cc_proto",
"//mediapipe/tasks/ios/components/containers/utils:MPPCategoryHelpers",
"//mediapipe/tasks/ios/components/containers/utils:MPPLandmarkHelpers",
"//mediapipe/tasks/ios/vision/hand_landmarker:MPPHandLandmarkerResult",
],
)

View File

@ -0,0 +1,65 @@
// 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/hand_landmarker/sources/MPPHandLandmarkerResult.h"
#include "mediapipe/framework/formats/classification.pb.h"
#include "mediapipe/framework/formats/landmark.pb.h"
#include "mediapipe/framework/packet.h"
NS_ASSUME_NONNULL_BEGIN
static const int kMicroSecondsPerMilliSecond = 1000;
@interface MPPHandLandmarkerResult (Helpers)
/**
* Creates an `MPPHandLandmarkerResult` from landmarks, world landmarks and handedness packets.
*
* @param landmarksPacket A MediaPipe packet wrapping a `std::vector<NormalizedlandmarkListProto>`.
* @param worldLandmarksPacket A MediaPipe packet wrapping a `std::vector<LandmarkListProto>`.
* @param handednessPacket a MediaPipe packet wrapping a `std::vector<ClassificationListProto>`.
*
* @return An `MPPHandLandmarkerResult` object that contains the hand landmark detection
* results.
*/
+ (MPPHandLandmarkerResult *)
handLandmarkerResultWithLandmarksPacket:(const mediapipe::Packet &)handLandmarksPacket
worldLandmarksPacket:(const mediapipe::Packet &)worldLandmarksPacket
handednessPacket:(const mediapipe::Packet &)handednessPacket;
/**
* Creates an `MPPHandLandmarkerResult` from landmarks, world landmarks and handedness proto
* vectors.
*
* @param landmarksProto A vector of protos of type `std::vector<NormalizedlandmarkListProto>`.
* @param worldLandmarksPacket A vector of protos of type `std::vector<LandmarkListProto>`.
* @param handednessPacket A vector of protos of type `std::vector<ClassificationListProto>`.
* @param timestampInMilliSeconds The timestamp of the Packet that contained the result.
*
* @return An `MPPHandLandmarkerResult` object that contains the hand landmark detection
* results.
*/
+ (MPPHandLandmarkerResult *)
handLandmarkerResultWithLandmarksProto:
(const std::vector<::mediapipe::NormalizedLandmarkList> &)landmarksProto
worldLandmarksProto:
(const std::vector<::mediapipe::LandmarkList> &)worldLandmarksProto
handednessProto:
(const std::vector<::mediapipe::ClassificationList> &)handednessProto
timestampInMilliSeconds:(NSInteger)timestampInMilliseconds;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,122 @@
// 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/hand_landmarker/utils/sources/MPPHandLandmarkerResult+Helpers.h"
#import "mediapipe/tasks/ios/components/containers/utils/sources/MPPCategory+Helpers.h"
#import "mediapipe/tasks/ios/components/containers/utils/sources/MPPLandmark+Helpers.h"
namespace {
using ClassificationListProto = ::mediapipe::ClassificationList;
using LandmarkListProto = ::mediapipe::LandmarkList;
using NormalizedLandmarkListProto = ::mediapipe::NormalizedLandmarkList;
using ::mediapipe::Packet;
} // namespace
@implementation MPPHandLandmarkerResult (Helpers)
+ (MPPHandLandmarkerResult *)emptyHandLandmarkerResultWithTimestampInMilliseconds:
(NSInteger)timestampInMilliseconds {
return [[MPPHandLandmarkerResult alloc] initWithLandmarks:@[]
worldLandmarks:@[]
handedness:@[]
timestampInMilliseconds:timestampInMilliseconds];
}
+ (MPPHandLandmarkerResult *)
handLandmarkerResultWithLandmarksProto:
(const std::vector<NormalizedLandmarkListProto> &)landmarksProto
worldLandmarksProto:
(const std::vector<LandmarkListProto> &)worldLandmarksProto
handednessProto:
(const std::vector<ClassificationListProto> &)handednessProto
timestampInMilliSeconds:(NSInteger)timestampInMilliseconds {
NSMutableArray<NSMutableArray<MPPNormalizedLandmark *> *> *multiHandLandmarks =
[NSMutableArray arrayWithCapacity:(NSUInteger)landmarksProto.size()];
for (const auto &landmarkListProto : landmarksProto) {
NSMutableArray<MPPNormalizedLandmark *> *landmarks =
[NSMutableArray arrayWithCapacity:(NSUInteger)landmarkListProto.landmark().size()];
for (const auto &normalizedLandmarkProto : landmarkListProto.landmark()) {
MPPNormalizedLandmark *normalizedLandmark =
[MPPNormalizedLandmark normalizedLandmarkWithProto:normalizedLandmarkProto];
[landmarks addObject:normalizedLandmark];
}
[multiHandLandmarks addObject:landmarks];
}
NSMutableArray<NSMutableArray<MPPLandmark *> *> *multiHandWorldLandmarks =
[NSMutableArray arrayWithCapacity:(NSUInteger)worldLandmarksProto.size()];
for (const auto &worldLandmarkListProto : worldLandmarksProto) {
NSMutableArray<MPPLandmark *> *worldLandmarks =
[NSMutableArray arrayWithCapacity:(NSUInteger)worldLandmarkListProto.landmark().size()];
for (const auto &landmarkProto : worldLandmarkListProto.landmark()) {
MPPLandmark *landmark = [MPPLandmark landmarkWithProto:landmarkProto];
[worldLandmarks addObject:landmark];
}
[multiHandWorldLandmarks addObject:worldLandmarks];
}
NSMutableArray<NSMutableArray<MPPCategory *> *> *multiHandHandedness =
[NSMutableArray arrayWithCapacity:(NSUInteger)handednessProto.size()];
for (const auto &classificationListProto : handednessProto) {
NSMutableArray<MPPCategory *> *handedness = [NSMutableArray
arrayWithCapacity:(NSUInteger)classificationListProto.classification().size()];
for (const auto &classificationProto : classificationListProto.classification()) {
MPPCategory *category = [MPPCategory categoryWithProto:classificationProto];
[handedness addObject:category];
}
[multiHandHandedness addObject:handedness];
}
MPPHandLandmarkerResult *handLandmarkerResult =
[[MPPHandLandmarkerResult alloc] initWithLandmarks:multiHandLandmarks
worldLandmarks:multiHandWorldLandmarks
handedness:multiHandHandedness
timestampInMilliseconds:timestampInMilliseconds];
return handLandmarkerResult;
}
+ (MPPHandLandmarkerResult *)
handLandmarkerResultWithLandmarksPacket:(const Packet &)landmarksPacket
worldLandmarksPacket:(const Packet &)worldLandmarksPacket
handednessPacket:(const Packet &)handednessPacket {
NSInteger timestampInMilliseconds =
(NSInteger)(landmarksPacket.Timestamp().Value() / kMicroSecondsPerMilliSecond);
if (landmarksPacket.IsEmpty()) {
return [MPPHandLandmarkerResult
emptyHandLandmarkerResultWithTimestampInMilliseconds:timestampInMilliseconds];
}
if (!handednessPacket.ValidateAsType<std::vector<ClassificationListProto>>().ok() ||
!landmarksPacket.ValidateAsType<std::vector<NormalizedLandmarkListProto>>().ok() ||
!worldLandmarksPacket.ValidateAsType<std::vector<LandmarkListProto>>().ok()) {
return [MPPHandLandmarkerResult
emptyHandLandmarkerResultWithTimestampInMilliseconds:timestampInMilliseconds];
}
return [MPPHandLandmarkerResult
handLandmarkerResultWithLandmarksProto:landmarksPacket
.Get<std::vector<NormalizedLandmarkListProto>>()
worldLandmarksProto:worldLandmarksPacket
.Get<std::vector<LandmarkListProto>>()
handednessProto:handednessPacket
.Get<std::vector<ClassificationListProto>>()
timestampInMilliSeconds:timestampInMilliseconds];
}
@end