From c7703bfb21d378caf78ac752a8a681e41555af9e Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Thu, 25 May 2023 10:02:16 -0700 Subject: [PATCH] Add FaceLandmarkerOptions API PiperOrigin-RevId: 535292669 --- .../tasks/ios/vision/face_landmarker/BUILD | 27 ++++++++ .../sources/MPPFaceLandmarkerOptions.h | 64 +++++++++++++++++++ .../sources/MPPFaceLandmarkerOptions.m | 43 +++++++++++++ .../ios/vision/face_landmarker/utils/BUILD | 33 ++++++++++ .../MPPFaceLandmarkerOptions+Helpers.h | 36 +++++++++++ .../MPPFaceLandmarkerOptions+Helpers.mm | 53 +++++++++++++++ 6 files changed, 256 insertions(+) create mode 100644 mediapipe/tasks/ios/vision/face_landmarker/BUILD create mode 100644 mediapipe/tasks/ios/vision/face_landmarker/sources/MPPFaceLandmarkerOptions.h create mode 100644 mediapipe/tasks/ios/vision/face_landmarker/sources/MPPFaceLandmarkerOptions.m create mode 100644 mediapipe/tasks/ios/vision/face_landmarker/utils/BUILD create mode 100644 mediapipe/tasks/ios/vision/face_landmarker/utils/sources/MPPFaceLandmarkerOptions+Helpers.h create mode 100644 mediapipe/tasks/ios/vision/face_landmarker/utils/sources/MPPFaceLandmarkerOptions+Helpers.mm diff --git a/mediapipe/tasks/ios/vision/face_landmarker/BUILD b/mediapipe/tasks/ios/vision/face_landmarker/BUILD new file mode 100644 index 000000000..e2b0148fa --- /dev/null +++ b/mediapipe/tasks/ios/vision/face_landmarker/BUILD @@ -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. + +package(default_visibility = ["//mediapipe/tasks:internal"]) + +licenses(["notice"]) + +objc_library( + name = "MPPFaceLandmarkerOptions", + srcs = ["sources/MPPFaceLandmarkerOptions.m"], + hdrs = ["sources/MPPFaceLandmarkerOptions.h"], + deps = [ + "//mediapipe/tasks/ios/core:MPPTaskOptions", + "//mediapipe/tasks/ios/vision/core:MPPRunningMode", + ], +) diff --git a/mediapipe/tasks/ios/vision/face_landmarker/sources/MPPFaceLandmarkerOptions.h b/mediapipe/tasks/ios/vision/face_landmarker/sources/MPPFaceLandmarkerOptions.h new file mode 100644 index 000000000..873858a54 --- /dev/null +++ b/mediapipe/tasks/ios/vision/face_landmarker/sources/MPPFaceLandmarkerOptions.h @@ -0,0 +1,64 @@ +// 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" + +NS_ASSUME_NONNULL_BEGIN + +/** Options for setting up a `MPPFaceLandmarker`. */ +NS_SWIFT_NAME(FaceLandmarkerOptions) +@interface MPPFaceLandmarkerOptions : MPPTaskOptions + +/** + * Running mode of the face landmark dection task. Defaults to `MPPRunningModeImage`. + * `MPPFaceLandmarker` can be created with one of the following running modes: + * 1. `MPPRunningModeImage`: The mode for performing face detection on single image inputs. + * 2. `MPPRunningModeVideo`: The mode for performing face detection on the decoded frames of a + * video. + * 3. `MPPRunningModeLiveStream`: The mode for performing face detection on a live stream of + * input data, such as from the camera. + */ +@property(nonatomic) MPPRunningMode runningMode; + +/** The maximum number of faces can be detected by the FaceLandmarker. Defaults to 1. */ +@property(nonatomic) NSInteger numFaces; + +/** + * The minimum confidence score for the face detection to be considered successful. Defaults to 0.5. + */ +@property(nonatomic) float minFaceDetectionConfidence; + +/** + * The minimum confidence score of face presence score in the face landmark detection. Defaults to + * 0.5. + */ +@property(nonatomic) float minFacePresenceConfidence; + +/** + * The minimum confidence score for the face tracking to be considered successful. Defaults to 0.5. + */ +@property(nonatomic) float minTrackingConfidence; + +/** + * Whether FaceLandmarker outputs face blendshapes classification. Face blendshapes are used for + * rendering the 3D face model. + */ +@property(nonatomic) BOOL outputFaceBlendshapes; + +@end + +NS_ASSUME_NONNULL_END diff --git a/mediapipe/tasks/ios/vision/face_landmarker/sources/MPPFaceLandmarkerOptions.m b/mediapipe/tasks/ios/vision/face_landmarker/sources/MPPFaceLandmarkerOptions.m new file mode 100644 index 000000000..80350d12e --- /dev/null +++ b/mediapipe/tasks/ios/vision/face_landmarker/sources/MPPFaceLandmarkerOptions.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/face_landmarker/sources/MPPFaceLandmarkerOptions.h" + +@implementation MPPFaceLandmarkerOptions + +- (instancetype)init { + self = [super init]; + if (self) { + _numFaces = 1; + _minFaceDetectionConfidence = 0.5f; + _minFacePresenceConfidence = 0.5f; + _minTrackingConfidence = 0.5f; + _outputFaceBlendshapes = NO; + } + return self; +} + +- (id)copyWithZone:(NSZone *)zone { + MPPFaceLandmarkerOptions *faceLandmarkerOptions = [super copyWithZone:zone]; + + faceLandmarkerOptions.numFaces = self.numFaces; + faceLandmarkerOptions.minFaceDetectionConfidence = self.minFaceDetectionConfidence; + faceLandmarkerOptions.minFacePresenceConfidence = self.minFacePresenceConfidence; + faceLandmarkerOptions.minTrackingConfidence = self.minTrackingConfidence; + faceLandmarkerOptions.outputFaceBlendshapes = self.outputFaceBlendshapes; + + return faceLandmarkerOptions; +} + +@end diff --git a/mediapipe/tasks/ios/vision/face_landmarker/utils/BUILD b/mediapipe/tasks/ios/vision/face_landmarker/utils/BUILD new file mode 100644 index 000000000..594c91384 --- /dev/null +++ b/mediapipe/tasks/ios/vision/face_landmarker/utils/BUILD @@ -0,0 +1,33 @@ +# 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 = "MPPFaceLandmarkerOptionsHelpers", + srcs = ["sources/MPPFaceLandmarkerOptions+Helpers.mm"], + hdrs = ["sources/MPPFaceLandmarkerOptions+Helpers.h"], + deps = [ + "//mediapipe/framework:calculator_options_cc_proto", + "//mediapipe/tasks/cc/vision/face_detector/proto:face_detector_graph_options_cc_proto", + "//mediapipe/tasks/cc/vision/face_landmarker/proto:face_landmarker_graph_options_cc_proto", + "//mediapipe/tasks/cc/vision/face_landmarker/proto:face_landmarks_detector_graph_options_cc_proto", + "//mediapipe/tasks/ios/common/utils:NSStringHelpers", + "//mediapipe/tasks/ios/core:MPPTaskOptionsProtocol", + "//mediapipe/tasks/ios/core/utils:MPPBaseOptionsHelpers", + "//mediapipe/tasks/ios/vision/face_landmarker:MPPFaceLandmarkerOptions", + ], +) diff --git a/mediapipe/tasks/ios/vision/face_landmarker/utils/sources/MPPFaceLandmarkerOptions+Helpers.h b/mediapipe/tasks/ios/vision/face_landmarker/utils/sources/MPPFaceLandmarkerOptions+Helpers.h new file mode 100644 index 000000000..aa23c7deb --- /dev/null +++ b/mediapipe/tasks/ios/vision/face_landmarker/utils/sources/MPPFaceLandmarkerOptions+Helpers.h @@ -0,0 +1,36 @@ +// 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. + +#ifndef __cplusplus +#error "This file requires Objective-C++." +#endif // __cplusplus + +#include "mediapipe/framework/calculator_options.pb.h" +#import "mediapipe/tasks/ios/core/sources/MPPTaskOptionsProtocol.h" +#import "mediapipe/tasks/ios/vision/face_landmarker/sources/MPPFaceLandmarkerOptions.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MPPFaceLandmarkerOptions (Helpers) + +/** + * Populates the provided `CalculatorOptions` proto container with the current settings. + * + * @param optionsProto The `CalculatorOptions` proto object to copy the settings to. + */ +- (void)copyToProto:(::mediapipe::CalculatorOptions *)optionsProto; + +@end + +NS_ASSUME_NONNULL_END diff --git a/mediapipe/tasks/ios/vision/face_landmarker/utils/sources/MPPFaceLandmarkerOptions+Helpers.mm b/mediapipe/tasks/ios/vision/face_landmarker/utils/sources/MPPFaceLandmarkerOptions+Helpers.mm new file mode 100644 index 000000000..413b29500 --- /dev/null +++ b/mediapipe/tasks/ios/vision/face_landmarker/utils/sources/MPPFaceLandmarkerOptions+Helpers.mm @@ -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 "mediapipe/tasks/ios/vision/face_landmarker/utils/sources/MPPFaceLandmarkerOptions+Helpers.h" + +#import "mediapipe/tasks/ios/common/utils/sources/NSString+Helpers.h" +#import "mediapipe/tasks/ios/core/utils/sources/MPPBaseOptions+Helpers.h" + +#include "mediapipe/tasks/cc/vision/face_detector/proto/face_detector_graph_options.pb.h" +#include "mediapipe/tasks/cc/vision/face_landmarker/proto/face_landmarker_graph_options.pb.h" +#include "mediapipe/tasks/cc/vision/face_landmarker/proto/face_landmarks_detector_graph_options.pb.h" + +using CalculatorOptionsProto = ::mediapipe::CalculatorOptions; +using FaceDetectorGraphOptionsProto = + ::mediapipe::tasks::vision::face_detector::proto::FaceDetectorGraphOptions; +using FaceLandmarkerGraphOptionsProto = + ::mediapipe::tasks::vision::face_landmarker::proto::FaceLandmarkerGraphOptions; +using FaceLandmarksDetectorGraphOptionsProto = + ::mediapipe::tasks::vision::face_landmarker::proto::FaceLandmarksDetectorGraphOptions; + +@implementation MPPFaceLandmarkerOptions (Helpers) + +- (void)copyToProto:(CalculatorOptionsProto *)optionsProto { + FaceLandmarkerGraphOptionsProto *faceLandmarkerGraphOptions = + optionsProto->MutableExtension(FaceLandmarkerGraphOptionsProto::ext); + + faceLandmarkerGraphOptions->Clear(); + + [self.baseOptions copyToProto:faceLandmarkerGraphOptions->mutable_base_options()]; + faceLandmarkerGraphOptions->set_min_tracking_confidence(self.minTrackingConfidence); + + FaceLandmarksDetectorGraphOptionsProto *faceLandmarkerDetectorGraphOptions = + faceLandmarkerGraphOptions->mutable_face_landmarks_detector_graph_options(); + faceLandmarkerDetectorGraphOptions->set_min_detection_confidence(self.minFacePresenceConfidence); + + FaceDetectorGraphOptionsProto *faceDetctorGraphOptions = + faceLandmarkerGraphOptions->mutable_face_detector_graph_options(); + faceDetctorGraphOptions->set_num_faces(self.numFaces); + faceDetctorGraphOptions->set_min_detection_confidence(self.minFaceDetectionConfidence); +} + +@end