From dcb0414d4b6ef1b82d054487df8fc524220932dd Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Wed, 17 May 2023 21:53:25 +0530 Subject: [PATCH] Added MPPGestureRecognizer header --- .../tasks/ios/vision/gesture_recognizer/BUILD | 19 ++ .../sources/MPPGestureRecognizer.h | 274 ++++++++++++++++++ 2 files changed, 293 insertions(+) create mode 100644 mediapipe/tasks/ios/vision/gesture_recognizer/sources/MPPGestureRecognizer.h diff --git a/mediapipe/tasks/ios/vision/gesture_recognizer/BUILD b/mediapipe/tasks/ios/vision/gesture_recognizer/BUILD index 26fa4d6cf..a9a62d720 100644 --- a/mediapipe/tasks/ios/vision/gesture_recognizer/BUILD +++ b/mediapipe/tasks/ios/vision/gesture_recognizer/BUILD @@ -38,3 +38,22 @@ objc_library( "//mediapipe/tasks/ios/vision/core:MPPRunningMode", ], ) + +objc_library( + name = "MPPGestureRecognizer", + srcs = ["sources/MPPGestureRecognizer.mm"], + hdrs = ["sources/MPPGestureRecognizer.h"], + copts = [ + "-ObjC++", + "-std=c++17", + "-x objective-c++", + ], + module_name = "MPPGestureRecognizer", + deps = [ + ":MPPGestureRecognizerOptions", + ":MPPGestureRecognizerResult", + "//mediapipe/tasks/ios/core:MPPTaskOptions", + "//mediapipe/tasks/ios/vision/core:MPPImage", + ], +) + diff --git a/mediapipe/tasks/ios/vision/gesture_recognizer/sources/MPPGestureRecognizer.h b/mediapipe/tasks/ios/vision/gesture_recognizer/sources/MPPGestureRecognizer.h new file mode 100644 index 000000000..2e3ca937b --- /dev/null +++ b/mediapipe/tasks/ios/vision/gesture_recognizer/sources/MPPGestureRecognizer.h @@ -0,0 +1,274 @@ +// 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/MPPImage.h" +#import "mediapipe/tasks/ios/vision/gesture_recognizer/sources/MPPGestureRecognizerOptions.h" +#import "mediapipe/tasks/ios/vision/gesture_recognizer/sources/MPPGestureRecognizerResult.h" + +NS_ASSUME_NONNULL_BEGIN + +/** + * @brief Performs gesture recognition on images. + * + * This API expects a pre-trained TFLite hand gesture recognizer model or a custom one created using + * MediaPipe Solutions Model Maker. See + * https://developers.google.com/mediapipe/solutions/model_maker. + */ +NS_SWIFT_NAME(GestureRecognizer) +@interface MPPGestureRecognizer : NSObject + +/** + * Creates a new instance of `MPPGestureRecognizer` from an absolute path to a TensorFlow Lite model + * file stored locally on the device and the default `MPPGestureRecognizerOptions`. + * + * @param modelPath An absolute path to a TensorFlow Lite model file stored locally on the device. + * @param error An optional error parameter populated when there is an error in initializing the + * gesture recognizer. + * + * @return A new instance of `MPPGestureRecognizer` with the given model path. `nil` if there is an + * error in initializing the gesture recognizer. + */ +- (nullable instancetype)initWithModelPath:(NSString *)modelPath error:(NSError **)error; + +/** + * Creates a new instance of `MPPGestureRecognizer` from the given `MPPGestureRecognizerOptions`. + * + * @param options The options of type `MPPGestureRecognizerOptions` to use for configuring the + * `MPPGestureRecognizer`. + * @param error An optional error parameter populated when there is an error in initializing the + * gesture recognizer. + * + * @return A new instance of `MPPGestureRecognizer` with the given options. `nil` if there is an + * error in initializing the gesture recognizer. + */ +- (nullable instancetype)initWithOptions:(MPPGestureRecognizerOptions *)options + error:(NSError **)error NS_DESIGNATED_INITIALIZER; + +/** + * Performs gesture recognition 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 `MPPGestureRecognizer` is created with + * `MPPRunningModeImage`. + * This method supports gesture recognition of 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 gesture recognition is to be performed. + * @param error An optional error parameter populated when there is an error in performing gesture + * recognition on the input image. + * + * @return An `MPPGestureRecognizerResult` object that contains the hand gesture recognition + * results. + */ +- (nullable MPPGestureRecognizerResult *)recognizeImage:(MPPImage *)image + error:(NSError **)error + NS_SWIFT_NAME(recognize(image:)); + +/** + * Performs gesture recognition on the provided `MPPImage` cropped to the specified region of + * interest. Rotation will be applied on the cropped image according to the `orientation` property + * of the provided `MPPImage`. Only use this method when the `MPPGestureRecognizer` is created with + * `MPPRunningModeImage`. + * + * This method supports gesture recognition of 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 gesture recognition is to be performed. + * @param roi A `CGRect` specifying the region of interest within the given `MPPImage`, on which + * gesture recognition should be performed. + * @param error An optional error parameter populated when there is an error in performing gesture + * recognition on the input image. + * + * @return An `MPPGestureRecognizerResult` object that contains the hand gesture recognition + * results. + */ +- (nullable MPPGestureRecognizerResult *)recognizeImage:(MPPImage *)image + regionOfInterest:(CGRect)roi + error:(NSError **)error + NS_SWIFT_NAME(recognize(image:regionOfInterest:)); + +/** + * Performs gesture recognition 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 `MPPGestureRecognizer` 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 gesture recognition of 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 gesture recognition 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 gesture + * recognition on the input video frame. + * + * @return An `MPPGestureRecognizerResult` object that contains the hand gesture recognition + * results. + */ +- (nullable MPPGestureRecognizerResult *)recognizeVideoFrame:(MPPImage *)image + timestampInMilliseconds:(NSInteger)timestampInMilliseconds + error:(NSError **)error + NS_SWIFT_NAME(recognize(videoFrame:timestampInMilliseconds:)); + +/** + * Performs gesture recognition on the provided video frame of type `MPPImage` cropped to the + * specified region of interest. Rotation will be applied according to the `orientation` property of + * the provided `MPPImage`. Only use this method when the `MPPGestureRecognizer` 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 gesture recognition of 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 A live stream image data of type `MPPImage` on which gesture recognition is to be + * performed. + * @param timestampInMilliseconds The video frame's timestamp (in milliseconds). The input + * timestamps must be monotonically increasing. + * @param roi A `CGRect` specifying the region of interest within the video frame of type + * `MPPImage`, on which gesture recognition should be performed. + * @param error An optional error parameter populated when there is an error in performing gesture + * recognition on the input video frame. + * + * @return An `MPPGestureRecognizerResult` object that contains the hand gesture recognition + * results. + */ +- (nullable MPPGestureRecognizerResult *)recognizeVideoFrame:(MPPImage *)image + timestampInMilliseconds:(NSInteger)timestampInMilliseconds + regionOfInterest:(CGRect)roi + error:(NSError **)error + NS_SWIFT_NAME(recognize(videoFrame:timestampInMilliseconds:regionOfInterest:)); + +/** + * Sends live stream image data of type `MPPImage` to perform gesture recognition 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 `MPPGestureRecognizer` is created with + * `MPPRunningModeLiveStream`. + * + * The object which needs to be continuously notified of the available results of gesture + * recognition must confirm to `MPPGestureRecognizerLiveStreamDelegate` protocol and implement the + * `gestureRecognizer:didFinishRecognitionWithResult:timestampInMilliseconds:error:` + * delegate method. + * + * It's required to provide a timestamp (in milliseconds) to indicate when the input image is sent + * to the gesture recognizer. The input timestamps must be monotonically increasing. + * + * This method supports gesture recognition of 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 gesture recognition 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 gesture recognition is to be + * performed. + * @param timestampInMilliseconds The timestamp (in milliseconds) which indicates when the input + * image is sent to the gesture recognizer. The input timestamps must be monotonically increasing. + * @param error An optional error parameter populated when there is an error in performing gesture + * recognition on the input live stream image data. + * + * @return `YES` if the image was sent to the task successfully, otherwise `NO`. + */ +- (BOOL)recognizeAsyncImage:(MPPImage *)image + timestampInMilliseconds:(NSInteger)timestampInMilliseconds + error:(NSError **)error + NS_SWIFT_NAME(recognizeAsync(image:timestampInMilliseconds:)); + +/** + * Sends live stream image data of type `MPPImage` to perform gesture recognition, cropped to the + * specified region of interest.. Rotation will be applied according to the `orientation` property + * of the provided `MPPImage`. Only use this method when the `MPPGestureRecognizer` is created with + * `MPPRunningModeLiveStream`. + * + * The object which needs to be continuously notified of the available results of gesture + * recognition must confirm to `MPPGestureRecognizerLiveStreamDelegate` protocol and implement the + * `gestureRecognizer:didFinishRecognitionWithResult:timestampInMilliseconds:error:` delegate + * method. + * + * It's required to provide a timestamp (in milliseconds) to indicate when the input image is sent + * to the gesture recognizer. The input timestamps must be monotonically increasing. + * + * This method supports gesture recognition of 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 preforming gesture recognition 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 gesture recognition is to be + * performed. + * @param timestampInMilliseconds The timestamp (in milliseconds) which indicates when the input + * image is sent to the gesture recognizer. The input timestamps must be monotonically increasing. + * @param roi A `CGRect` specifying the region of interest within the given live stream image data + * of type `MPPImage`, on which gesture recognition should be performed. + * @param error An optional error parameter populated when there is an error in performing gesture + * recognition on the input live stream image data. + * + * @return `YES` if the image was sent to the task successfully, otherwise `NO`. + */ +- (BOOL)recognizeAsyncImage:(MPPImage *)image + timestampInMilliseconds:(NSInteger)timestampInMilliseconds + regionOfInterest:(CGRect)roi + error:(NSError **)error + NS_SWIFT_NAME(recognizeAsync(image:timestampInMilliseconds:regionOfInterest:)); + +- (instancetype)init NS_UNAVAILABLE; + ++ (instancetype)new NS_UNAVAILABLE; + +@end + +NS_ASSUME_NONNULL_END