Updated iOS face detector to use refactored vision task runner
This commit is contained in:
parent
30802b80cd
commit
e7a0ed84e6
|
@ -55,7 +55,7 @@ objc_library(
|
||||||
"//mediapipe/tasks/ios/core:MPPTaskInfo",
|
"//mediapipe/tasks/ios/core:MPPTaskInfo",
|
||||||
"//mediapipe/tasks/ios/vision/core:MPPImage",
|
"//mediapipe/tasks/ios/vision/core:MPPImage",
|
||||||
"//mediapipe/tasks/ios/vision/core:MPPVisionPacketCreator",
|
"//mediapipe/tasks/ios/vision/core:MPPVisionPacketCreator",
|
||||||
"//mediapipe/tasks/ios/vision/core:MPPVisionTaskRunner",
|
"//mediapipe/tasks/ios/vision/core:MPPVisionTaskRunnerRefactored",
|
||||||
"//mediapipe/tasks/ios/vision/face_detector/utils:MPPFaceDetectorOptionsHelpers",
|
"//mediapipe/tasks/ios/vision/face_detector/utils:MPPFaceDetectorOptionsHelpers",
|
||||||
"//mediapipe/tasks/ios/vision/face_detector/utils:MPPFaceDetectorResultHelpers",
|
"//mediapipe/tasks/ios/vision/face_detector/utils:MPPFaceDetectorResultHelpers",
|
||||||
],
|
],
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
#import "mediapipe/tasks/ios/common/utils/sources/NSString+Helpers.h"
|
#import "mediapipe/tasks/ios/common/utils/sources/NSString+Helpers.h"
|
||||||
#import "mediapipe/tasks/ios/core/sources/MPPTaskInfo.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/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_detector/utils/sources/MPPFaceDetectorOptions+Helpers.h"
|
#import "mediapipe/tasks/ios/vision/face_detector/utils/sources/MPPFaceDetectorOptions+Helpers.h"
|
||||||
#import "mediapipe/tasks/ios/vision/face_detector/utils/sources/MPPFaceDetectorResult+Helpers.h"
|
#import "mediapipe/tasks/ios/vision/face_detector/utils/sources/MPPFaceDetectorResult+Helpers.h"
|
||||||
|
|
||||||
|
@ -49,6 +49,12 @@ static NSString *const kTaskName = @"faceDetector";
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define FaceDetectorResultWithOutputPacketMap(outputPacketMap) \
|
||||||
|
{ \
|
||||||
|
[MPPFaceDetectorResult \
|
||||||
|
faceDetectorResultWithDetectionsPacket:outputPacketMap[kDetectionsStreamName.cppString]] \
|
||||||
|
}
|
||||||
|
|
||||||
@interface MPPFaceDetector () {
|
@interface MPPFaceDetector () {
|
||||||
/** iOS Vision Task Runner */
|
/** iOS Vision Task Runner */
|
||||||
MPPVisionTaskRunner *_visionTaskRunner;
|
MPPVisionTaskRunner *_visionTaskRunner;
|
||||||
|
@ -102,11 +108,13 @@ static NSString *const kTaskName = @"faceDetector";
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
_visionTaskRunner =
|
_visionTaskRunner = [[MPPVisionTaskRunner alloc] initWithTaskInfo:taskInfo
|
||||||
[[MPPVisionTaskRunner alloc] initWithCalculatorGraphConfig:[taskInfo generateGraphConfig]
|
runningMode:options.runningMode
|
||||||
runningMode:options.runningMode
|
roiAllowed:NO
|
||||||
packetsCallback:std::move(packetsCallback)
|
packetsCallback:std::move(packetsCallback)
|
||||||
error:error];
|
imageInputStreamName:kImageInStreamName
|
||||||
|
normRectInputStreamName:kNormRectStreamName
|
||||||
|
error:error];
|
||||||
|
|
||||||
if (!_visionTaskRunner) {
|
if (!_visionTaskRunner) {
|
||||||
return nil;
|
return nil;
|
||||||
|
@ -124,95 +132,29 @@ static NSString *const kTaskName = @"faceDetector";
|
||||||
return [self initWithOptions:options error:error];
|
return [self initWithOptions:options error:error];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (std::optional<PacketMap>)inputPacketMapWithMPPImage:(MPPImage *)image
|
|
||||||
timestampInMilliseconds:(NSInteger)timestampInMilliseconds
|
|
||||||
error:(NSError **)error {
|
|
||||||
std::optional<NormalizedRect> 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.value()
|
|
||||||
timestampInMilliseconds:timestampInMilliseconds];
|
|
||||||
|
|
||||||
PacketMap inputPacketMap = InputPacketMap(imagePacket, normalizedRectPacket);
|
|
||||||
return inputPacketMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (nullable MPPFaceDetectorResult *)detectInImage:(MPPImage *)image error:(NSError **)error {
|
- (nullable MPPFaceDetectorResult *)detectInImage:(MPPImage *)image error:(NSError **)error {
|
||||||
std::optional<NormalizedRect> rect =
|
std::optional<PacketMap> outputPacketMap = [_visionTaskRunner processImage:image error:error];
|
||||||
[_visionTaskRunner normalizedRectWithImageOrientation:image.orientation
|
|
||||||
imageSize:CGSizeMake(image.width, image.height)
|
|
||||||
error:error];
|
|
||||||
if (!rect.has_value()) {
|
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
Packet imagePacket = [MPPVisionPacketCreator createPacketWithMPPImage:image error:error];
|
return [MPPFaceDetector faceDetectorResultWithOptionalOutputPacketMap:outputPacketMap];
|
||||||
if (imagePacket.IsEmpty()) {
|
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
Packet normalizedRectPacket =
|
|
||||||
[MPPVisionPacketCreator createPacketWithNormalizedRect:rect.value()];
|
|
||||||
|
|
||||||
PacketMap inputPacketMap = InputPacketMap(imagePacket, normalizedRectPacket);
|
|
||||||
|
|
||||||
std::optional<PacketMap> outputPacketMap = [_visionTaskRunner processImagePacketMap:inputPacketMap
|
|
||||||
error:error];
|
|
||||||
if (!outputPacketMap.has_value()) {
|
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
return [MPPFaceDetectorResult
|
|
||||||
faceDetectorResultWithDetectionsPacket:outputPacketMap
|
|
||||||
.value()[kDetectionsStreamName.cppString]];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (nullable MPPFaceDetectorResult *)detectInVideoFrame:(MPPImage *)image
|
- (nullable MPPFaceDetectorResult *)detectInVideoFrame:(MPPImage *)image
|
||||||
timestampInMilliseconds:(NSInteger)timestampInMilliseconds
|
timestampInMilliseconds:(NSInteger)timestampInMilliseconds
|
||||||
error:(NSError **)error {
|
error:(NSError **)error {
|
||||||
std::optional<PacketMap> inputPacketMap = [self inputPacketMapWithMPPImage:image
|
|
||||||
timestampInMilliseconds:timestampInMilliseconds
|
|
||||||
error:error];
|
|
||||||
if (!inputPacketMap.has_value()) {
|
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<PacketMap> outputPacketMap =
|
std::optional<PacketMap> outputPacketMap =
|
||||||
[_visionTaskRunner processVideoFramePacketMap:inputPacketMap.value() error:error];
|
[_visionTaskRunner processVideoFrame:image
|
||||||
|
timestampInMilliseconds:timestampInMilliseconds
|
||||||
|
error:error];
|
||||||
|
|
||||||
if (!outputPacketMap.has_value()) {
|
return [MPPFaceDetector faceDetectorResultWithOptionalOutputPacketMap:outputPacketMap];
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
return [MPPFaceDetectorResult
|
|
||||||
faceDetectorResultWithDetectionsPacket:outputPacketMap
|
|
||||||
.value()[kDetectionsStreamName.cppString]];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)detectAsyncInImage:(MPPImage *)image
|
- (BOOL)detectAsyncInImage:(MPPImage *)image
|
||||||
timestampInMilliseconds:(NSInteger)timestampInMilliseconds
|
timestampInMilliseconds:(NSInteger)timestampInMilliseconds
|
||||||
error:(NSError **)error {
|
error:(NSError **)error {
|
||||||
std::optional<PacketMap> inputPacketMap = [self inputPacketMapWithMPPImage:image
|
return [_visionTaskRunner processLiveStreamImage:image
|
||||||
timestampInMilliseconds:timestampInMilliseconds
|
timestampInMilliseconds:timestampInMilliseconds
|
||||||
error:error];
|
error:error];
|
||||||
if (!inputPacketMap.has_value()) {
|
|
||||||
return NO;
|
|
||||||
}
|
|
||||||
|
|
||||||
return [_visionTaskRunner processLiveStreamPacketMap:inputPacketMap.value() error:error];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)processLiveStreamResult:(absl::StatusOr<PacketMap>)liveStreamResult {
|
- (void)processLiveStreamResult:(absl::StatusOr<PacketMap>)liveStreamResult {
|
||||||
|
@ -237,9 +179,7 @@ static NSString *const kTaskName = @"faceDetector";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
MPPFaceDetectorResult *result = [MPPFaceDetectorResult
|
MPPFaceDetectorResult *result = FaceDetectorResultWithOutputPacketMap(liveStreamResult.value());
|
||||||
faceDetectorResultWithDetectionsPacket:liveStreamResult
|
|
||||||
.value()[kDetectionsStreamName.cppString]];
|
|
||||||
|
|
||||||
NSInteger timeStampInMilliseconds =
|
NSInteger timeStampInMilliseconds =
|
||||||
outputPacketMap[kImageOutStreamName.cppString].Timestamp().Value() /
|
outputPacketMap[kImageOutStreamName.cppString].Timestamp().Value() /
|
||||||
|
@ -252,4 +192,13 @@ static NSString *const kTaskName = @"faceDetector";
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
+ (nullable MPPFaceDetectorResult *)faceDetectorResultWithOptionalOutputPacketMap:
|
||||||
|
(std::optional<PacketMap>)outputPacketMap {
|
||||||
|
if (!outputPacketMap.has_value()) {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
return FaceDetectorResultWithOutputPacketMap(outputPacketMap.value());
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user