diff --git a/mediapipe/tasks/ios/vision/face_landmarker/BUILD b/mediapipe/tasks/ios/vision/face_landmarker/BUILD index 24a656b9..0ff88c6c 100644 --- a/mediapipe/tasks/ios/vision/face_landmarker/BUILD +++ b/mediapipe/tasks/ios/vision/face_landmarker/BUILD @@ -70,7 +70,7 @@ objc_library( "//mediapipe/tasks/ios/core:MPPTaskInfo", "//mediapipe/tasks/ios/vision/core:MPPImage", "//mediapipe/tasks/ios/vision/core:MPPVisionPacketCreator", - "//mediapipe/tasks/ios/vision/core:MPPVisionTaskRunner", + "//mediapipe/tasks/ios/vision/core:MPPVisionTaskRunnerRefactored", "//mediapipe/tasks/ios/vision/face_landmarker/utils:MPPFaceLandmarkerOptionsHelpers", "//mediapipe/tasks/ios/vision/face_landmarker/utils:MPPFaceLandmarkerResultHelpers", ], diff --git a/mediapipe/tasks/ios/vision/face_landmarker/sources/MPPFaceLandmarker.mm b/mediapipe/tasks/ios/vision/face_landmarker/sources/MPPFaceLandmarker.mm index eec0d66e..aada4bfa 100644 --- a/mediapipe/tasks/ios/vision/face_landmarker/sources/MPPFaceLandmarker.mm +++ b/mediapipe/tasks/ios/vision/face_landmarker/sources/MPPFaceLandmarker.mm @@ -19,7 +19,7 @@ #import "mediapipe/tasks/ios/common/utils/sources/NSString+Helpers.h" #import "mediapipe/tasks/ios/core/sources/MPPTaskInfo.h" #import "mediapipe/tasks/ios/vision/core/sources/MPPVisionPacketCreator.h" -#import "mediapipe/tasks/ios/vision/core/sources/MPPVisionTaskRunner.h" +#import "mediapipe/tasks/ios/vision/core/sources/MPPVisionTaskRunnerRefactored.h" #import "mediapipe/tasks/ios/vision/face_landmarker/sources/MPPFaceLandmarksConnections.h" #import "mediapipe/tasks/ios/vision/face_landmarker/utils/sources/MPPFaceLandmarkerOptions+Helpers.h" #import "mediapipe/tasks/ios/vision/face_landmarker/utils/sources/MPPFaceLandmarkerResult+Helpers.h" @@ -56,6 +56,16 @@ static NSString *const kTaskName = @"faceLandmarker"; } \ } +#define FaceLandmarkerResultWithOutputPacketMap(outputPacketMap) \ + { \ + [MPPFaceLandmarkerResult \ + faceLandmarkerResultWithLandmarksPacket:outputPacketMap[kLandmarksOutStreamName.cppString] \ + blendshapesPacket:outputPacketMap[kBlendshapesOutStreamName \ + .cppString] \ + transformationMatrixesPacket:outputPacketMap[kFaceGeometryOutStreamName \ + .cppString]] \ + } + @interface MPPFaceLandmarker () { /** iOS Vision Task Runner */ MPPVisionTaskRunner *_visionTaskRunner; @@ -71,6 +81,8 @@ static NSString *const kTaskName = @"faceLandmarker"; @implementation MPPFaceLandmarker +#pragma mark - Public + - (instancetype)initWithOptions:(MPPFaceLandmarkerOptions *)options error:(NSError **)error { self = [super init]; if (self) { @@ -124,12 +136,13 @@ static NSString *const kTaskName = @"faceLandmarker"; }; } - _visionTaskRunner = - [[MPPVisionTaskRunner alloc] initWithCalculatorGraphConfig:[taskInfo generateGraphConfig] - runningMode:options.runningMode - packetsCallback:std::move(packetsCallback) - error:error]; - + _visionTaskRunner = [[MPPVisionTaskRunner alloc] initWithTaskInfo:taskInfo + runningMode:options.runningMode + roiAllowed:NO + packetsCallback:std::move(packetsCallback) + imageInputStreamName:kImageInStreamName + normRectInputStreamName:kNormRectStreamName + error:error]; if (!_visionTaskRunner) { return nil; } @@ -144,138 +157,29 @@ static NSString *const kTaskName = @"faceLandmarker"; return [self initWithOptions:options error:error]; } -- (std::optional)inputPacketMapWithMPPImage:(MPPImage *)image - timestampInMilliseconds:(NSInteger)timestampInMilliseconds - error:(NSError **)error { - std::optional rect = - [_visionTaskRunner normalizedRectWithImageOrientation:image.orientation - imageSize:CGSizeMake(image.width, image.height) - error:error]; - if (!rect.has_value()) { - return std::nullopt; - } - - Packet imagePacket = [MPPVisionPacketCreator createPacketWithMPPImage:image - timestampInMilliseconds:timestampInMilliseconds - error:error]; - if (imagePacket.IsEmpty()) { - return std::nullopt; - } - - Packet normalizedRectPacket = - [MPPVisionPacketCreator createPacketWithNormalizedRect:*rect - timestampInMilliseconds:timestampInMilliseconds]; - - PacketMap inputPacketMap = InputPacketMap(imagePacket, normalizedRectPacket); - return inputPacketMap; -} - - (nullable MPPFaceLandmarkerResult *)detectInImage:(MPPImage *)image error:(NSError **)error { - std::optional rect = - [_visionTaskRunner normalizedRectWithImageOrientation:image.orientation - imageSize:CGSizeMake(image.width, image.height) - error:error]; - if (!rect.has_value()) { - return nil; - } + std::optional outputPacketMap = [_visionTaskRunner processImage:image error:error]; - Packet imagePacket = [MPPVisionPacketCreator createPacketWithMPPImage:image error:error]; - if (imagePacket.IsEmpty()) { - return nil; - } - - Packet normalizedRectPacket = [MPPVisionPacketCreator createPacketWithNormalizedRect:*rect]; - - PacketMap inputPacketMap = InputPacketMap(imagePacket, normalizedRectPacket); - - std::optional outputPacketMap = [_visionTaskRunner processImagePacketMap:inputPacketMap - error:error]; - if (!outputPacketMap.has_value()) { - return nil; - } - - return [MPPFaceLandmarkerResult - faceLandmarkerResultWithLandmarksPacket:outputPacketMap - .value()[kLandmarksOutStreamName.cppString] - blendshapesPacket:outputPacketMap - .value()[kBlendshapesOutStreamName.cppString] - transformationMatrixesPacket:outputPacketMap - .value()[kFaceGeometryOutStreamName.cppString]]; + return [MPPFaceLandmarker faceLandmarkerResultWithOptionalOutputPacketMap:outputPacketMap]; } - (nullable MPPFaceLandmarkerResult *)detectInVideoFrame:(MPPImage *)image timestampInMilliseconds:(NSInteger)timestampInMilliseconds error:(NSError **)error { - std::optional inputPacketMap = [self inputPacketMapWithMPPImage:image - timestampInMilliseconds:timestampInMilliseconds - error:error]; - if (!inputPacketMap.has_value()) { - return nil; - } - std::optional outputPacketMap = - [_visionTaskRunner processVideoFramePacketMap:*inputPacketMap error:error]; - if (!outputPacketMap.has_value()) { - return nil; - } + [_visionTaskRunner processVideoFrame:image + timestampInMilliseconds:timestampInMilliseconds + error:error]; - return [MPPFaceLandmarkerResult - faceLandmarkerResultWithLandmarksPacket:outputPacketMap - .value()[kLandmarksOutStreamName.cppString] - blendshapesPacket:outputPacketMap - .value()[kBlendshapesOutStreamName.cppString] - transformationMatrixesPacket:outputPacketMap - .value()[kFaceGeometryOutStreamName.cppString]]; + return [MPPFaceLandmarker faceLandmarkerResultWithOptionalOutputPacketMap:outputPacketMap]; } - (BOOL)detectAsyncInImage:(MPPImage *)image timestampInMilliseconds:(NSInteger)timestampInMilliseconds error:(NSError **)error { - std::optional inputPacketMap = [self inputPacketMapWithMPPImage:image - timestampInMilliseconds:timestampInMilliseconds - error:error]; - if (!inputPacketMap.has_value()) { - return NO; - } - - return [_visionTaskRunner processLiveStreamPacketMap:*inputPacketMap error:error]; -} - -- (void)processLiveStreamResult:(absl::StatusOr)liveStreamResult { - NSError *callbackError; - if (![MPPCommonUtils checkCppError:liveStreamResult.status() toError:&callbackError]) { - dispatch_async(_callbackQueue, ^{ - [_faceLandmarkerLiveStreamDelegate faceLandmarker:self - didFinishDetectionWithResult:nil - timestampInMilliseconds:Timestamp::Unset().Value() - error:callbackError]; - }); - return; - } - - PacketMap &outputPacketMap = *liveStreamResult; - if (outputPacketMap[kImageOutStreamName.cppString].IsEmpty()) { - // The graph did not return a result. We therefore do not raise the user callback. This mirrors - // returning `nil` in the other methods and is acceptable for the live stream delegate since - // it is expected that we drop frames and don't return results for every input. - return; - } - - MPPFaceLandmarkerResult *result = [MPPFaceLandmarkerResult - faceLandmarkerResultWithLandmarksPacket:outputPacketMap[kLandmarksOutStreamName.cppString] - blendshapesPacket:outputPacketMap[kBlendshapesOutStreamName.cppString] - transformationMatrixesPacket:outputPacketMap[kFaceGeometryOutStreamName - .cppString]]; - - NSInteger timeStampInMilliseconds = - outputPacketMap[kImageOutStreamName.cppString].Timestamp().Value() / - kMicrosecondsPerMillisecond; - dispatch_async(_callbackQueue, ^{ - [_faceLandmarkerLiveStreamDelegate faceLandmarker:self - didFinishDetectionWithResult:result - timestampInMilliseconds:timeStampInMilliseconds - error:callbackError]; - }); + return [_visionTaskRunner processLiveStreamImage:image + timestampInMilliseconds:timestampInMilliseconds + error:error]; } + (NSArray *)lipsConnections { @@ -322,4 +226,48 @@ static NSString *const kTaskName = @"faceLandmarker"; return MPPFaceConnections; } +#pragma mark - Private + ++ (nullable MPPFaceLandmarkerResult *)faceLandmarkerResultWithOptionalOutputPacketMap: + (std::optional)outputPacketMap { + if (!outputPacketMap.has_value()) { + return nil; + } + + return FaceLandmarkerResultWithOutputPacketMap(outputPacketMap.value()); +} + +- (void)processLiveStreamResult:(absl::StatusOr)liveStreamResult { + NSError *callbackError; + if (![MPPCommonUtils checkCppError:liveStreamResult.status() toError:&callbackError]) { + dispatch_async(_callbackQueue, ^{ + [_faceLandmarkerLiveStreamDelegate faceLandmarker:self + didFinishDetectionWithResult:nil + timestampInMilliseconds:Timestamp::Unset().Value() + error:callbackError]; + }); + return; + } + + PacketMap &outputPacketMap = *liveStreamResult; + if (outputPacketMap[kImageOutStreamName.cppString].IsEmpty()) { + // The graph did not return a result. We therefore do not raise the user callback. This mirrors + // returning `nil` in the other methods and is acceptable for the live stream delegate since + // it is expected that we drop frames and don't return results for every input. + return; + } + + MPPFaceLandmarkerResult *result = FaceLandmarkerResultWithOutputPacketMap(outputPacketMap); + + NSInteger timeStampInMilliseconds = + outputPacketMap[kImageOutStreamName.cppString].Timestamp().Value() / + kMicrosecondsPerMillisecond; + dispatch_async(_callbackQueue, ^{ + [_faceLandmarkerLiveStreamDelegate faceLandmarker:self + didFinishDetectionWithResult:result + timestampInMilliseconds:timeStampInMilliseconds + error:callbackError]; + }); +} + @end