From 5ad7c9fd89c463a065208f4872d21e03ab9f1955 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Fri, 26 May 2023 10:58:12 +0530 Subject: [PATCH] Added iOS hand landmarker options --- .../tasks/ios/vision/hand_landmarker/BUILD | 10 ++ .../sources/MPPHandLandmarkerOptions.h | 98 +++++++++++++++++++ .../sources/MPPHandLandmarkerOptions.m | 43 ++++++++ 3 files changed, 151 insertions(+) create mode 100644 mediapipe/tasks/ios/vision/hand_landmarker/sources/MPPHandLandmarkerOptions.h create mode 100644 mediapipe/tasks/ios/vision/hand_landmarker/sources/MPPHandLandmarkerOptions.m diff --git a/mediapipe/tasks/ios/vision/hand_landmarker/BUILD b/mediapipe/tasks/ios/vision/hand_landmarker/BUILD index 62ad4d4d0..2665142af 100644 --- a/mediapipe/tasks/ios/vision/hand_landmarker/BUILD +++ b/mediapipe/tasks/ios/vision/hand_landmarker/BUILD @@ -27,3 +27,13 @@ objc_library( ], ) +objc_library( + name = "MPPHandLandmarkerOptions", + srcs = ["sources/MPPHandLandmarkerOptions.m"], + hdrs = ["sources/MPPHandLandmarkerOptions.h"], + deps = [ + ":MPPHandLandmarkerResult", + "//mediapipe/tasks/ios/core:MPPTaskOptions", + "//mediapipe/tasks/ios/vision/core:MPPRunningMode", + ], +) diff --git a/mediapipe/tasks/ios/vision/hand_landmarker/sources/MPPHandLandmarkerOptions.h b/mediapipe/tasks/ios/vision/hand_landmarker/sources/MPPHandLandmarkerOptions.h new file mode 100644 index 000000000..27fdac2d8 --- /dev/null +++ b/mediapipe/tasks/ios/vision/hand_landmarker/sources/MPPHandLandmarkerOptions.h @@ -0,0 +1,98 @@ +// 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" +#import "mediapipe/tasks/ios/vision/core/sources/MPPRunningMode.h" +#import "mediapipe/tasks/ios/vision/hand_landmarker/sources/MPPHandLandmarkerResult.h" + +NS_ASSUME_NONNULL_BEGIN + +@class MPPHandLandmarker; + +/** + * This protocol defines an interface for the delegates of `MPPHandLandmarker` object to receive + * results of performing asynchronous hand landmark detection on images (i.e, when `runningMode` = `MPPRunningModeLiveStream`). + * + * The delegate of `MPPHandLandmarker` must adopt `MPPHandLandmarkerLiveStreamDelegate` protocol. + * The methods in this protocol are optional. + */ +NS_SWIFT_NAME(HandLandmarkerLiveStreamDelegate) +@protocol MPPHandLandmarkerLiveStreamDelegate + +@optional + +/** + * This method notifies a delegate that the results of asynchronous hand landmark detection of an image submitted to the `MPPHandLandmarker` is available. + * + * This method is called on a private serial dispatch queue created by the `MPPHandLandmarker` + * for performing the asynchronous delegates calls. + * + * @param handLandmarker The hand landmarker which performed the hand landmarking. + * This is useful to test equality when there are multiple instances of `MPPHandLandmarker`. + * @param result The `MPPHandLandmarkerResult` object that contains a list of detections, each + * detection has a bounding box that is expressed in the unrotated input frame of reference + * coordinates system, i.e. in `[0,image_width) x [0,image_height)`, which are the dimensions of the + * underlying image data. + * @param timestampInMilliseconds The timestamp (in milliseconds) which indicates when the input + * image was sent to the hand landmarker. + * @param error An optional error parameter populated when there is an error in performing hand landmark detection on the input live stream image data. + */ +- (void)handLandmarker:(MPPHandLandmarker *)handLandmarker + didFinishDetectionWithResult:(nullable MPPHandLandmarkerResult *)result + timestampInMilliseconds:(NSInteger)timestampInMilliseconds + error:(nullable NSError *)error + NS_SWIFT_NAME(handLandmarker(_:didFinishDetection:timestampInMilliseconds:error:)); +@end + +/** Options for setting up a `MPPHandLandmarker`. */ +NS_SWIFT_NAME(HandLandmarkerOptions) +@interface MPPHandLandmarkerOptions : MPPTaskOptions + +/** + * Running mode of the hand landmarker task. Defaults to `MPPRunningModeImage`. + * `MPPHandLandmarker` can be created with one of the following running modes: + * 1. `MPPRunningModeImage`: The mode for performing hand landmark detection on single image inputs. + * 2. `MPPRunningModeVideo`: The mode for performing hand landmark detection on the decoded frames of a + * video. + * 3. `MPPRunningModeLiveStream`: The mode for performing hand landmark detection on a live stream of + * input data, such as from the camera. + */ +@property(nonatomic) MPPRunningMode runningMode; + +/** + * An object that confirms to `MPPHandLandmarkerLiveStreamDelegate` protocol. This object must + * implement `handLandmarker:didFinishDetectionWithResult:timestampInMilliseconds:error:` to + * receive the results of performing asynchronous gesture recognition on images (i.e, when + * `runningMode` = `MPPRunningModeLiveStream`). + */ +@property(nonatomic, weak, nullable) id + handLandmarkerLiveStreamDelegate; + +/** The maximum number of hands that can be detected by the `MPPHandLandmarker`. */ +@property(nonatomic) NSInteger numHands; + +/** The minimum confidence score for the hand detection to be considered successful. */ +@property(nonatomic) float minHandDetectionConfidence; + +/** The minimum confidence score of hand presence score in the hand landmark detection. */ +@property(nonatomic) float minHandPresenceConfidence; + +/** The minimum confidence score for the hand tracking to be considered successful. */ +@property(nonatomic) float minTrackingConfidence; + +@end + +NS_ASSUME_NONNULL_END \ No newline at end of file diff --git a/mediapipe/tasks/ios/vision/hand_landmarker/sources/MPPHandLandmarkerOptions.m b/mediapipe/tasks/ios/vision/hand_landmarker/sources/MPPHandLandmarkerOptions.m new file mode 100644 index 000000000..d7b4a0252 --- /dev/null +++ b/mediapipe/tasks/ios/vision/hand_landmarker/sources/MPPHandLandmarkerOptions.m @@ -0,0 +1,43 @@ +// 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/MPPHandLandmarkerOptions.h" + +@implementation MPPHandLandmarkerOptions + +- (instancetype)init { + self = [super init]; + if (self) { + _numHands = 1; + _minHandDetectionConfidence = 0.5f; + _minHandPresenceConfidence = 0.5f; + _minTrackingConfidence = 0.5f; + } + return self; +} + +- (id)copyWithZone:(NSZone *)zone { + MPPHandLandmarkerOptions *handLandmarkerOptions = [super copyWithZone:zone]; + + handLandmarkerOptions.runningMode = self.runningMode; + handLandmarkerOptions.handLandmarkerLiveStreamDelegate = self.handLandmarkerLiveStreamDelegate; + handLandmarkerOptions.numHands = self.numHands; + handLandmarkerOptions.minHandDetectionConfidence = self.minHandDetectionConfidence; + handLandmarkerOptions.minHandPresenceConfidence = self.minHandPresenceConfidence; + handLandmarkerOptions.minTrackingConfidence = self.minTrackingConfidence; + + return handLandmarkerOptions; +} + +@end