Adds delegate to enable GPU acceleration in MPPVisionTask

This commit is contained in:
Alex.P(Minjin Park) 2023-09-08 14:12:36 +09:00
parent e39119ae53
commit 0a68d8d502
5 changed files with 30 additions and 8 deletions

View File

@ -16,6 +16,11 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, MPPBaseOptionsDelegate) {
MPPBaseOptionsDelegateCPU = 0,
MPPBaseOptionsDelegateGPU = 1
} NS_SWIFT_NAME(BaseOptionsDelegate);
/** /**
* Holds the base options that is used for creation of any type of task. It has fields with * Holds the base options that is used for creation of any type of task. It has fields with
* important information acceleration configuration, TFLite model source etc. * important information acceleration configuration, TFLite model source etc.
@ -26,6 +31,12 @@ NS_SWIFT_NAME(BaseOptions)
/** The path to the model asset to open and mmap in memory. */ /** The path to the model asset to open and mmap in memory. */
@property(nonatomic, copy) NSString *modelAssetPath; @property(nonatomic, copy) NSString *modelAssetPath;
/**
* The delegate to run MediaPipe. If the delegate is not set, the default
* delegate CPU is used. Use `delegate_options` to configure advanced
* features of the selected delegate.
*/
@property(nonatomic) MPPBaseOptionsDelegate delegate;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

View File

@ -28,7 +28,7 @@
MPPBaseOptions *baseOptions = [[MPPBaseOptions alloc] init]; MPPBaseOptions *baseOptions = [[MPPBaseOptions alloc] init];
baseOptions.modelAssetPath = self.modelAssetPath; baseOptions.modelAssetPath = self.modelAssetPath;
baseOptions.delegate = self.delegate;
return baseOptions; return baseOptions;
} }

View File

@ -33,6 +33,16 @@ using BaseOptionsProto = ::mediapipe::tasks::core::proto::BaseOptions;
if (self.modelAssetPath) { if (self.modelAssetPath) {
baseOptionsProto->mutable_model_asset()->set_file_name(self.modelAssetPath.UTF8String); baseOptionsProto->mutable_model_asset()->set_file_name(self.modelAssetPath.UTF8String);
} }
switch (self.delegate) {
case MPPBaseOptionsDelegateCPU: {
baseOptionsProto->mutable_acceleration()->mutable_tflite();
break;
}
case MPPBaseOptionsDelegateGPU: {
baseOptionsProto->mutable_acceleration()->mutable_gpu();
break;
}
}
} }
@end @end

View File

@ -98,6 +98,8 @@ NS_ASSUME_NONNULL_BEGIN
if (imageBuffer == NULL) { if (imageBuffer == NULL) {
return nil; return nil;
} }
_pixelBuffer = imageBuffer;
CVPixelBufferRetain(imageBuffer);
self = [super init]; self = [super init];
if (self != nil) { if (self != nil) {

View File

@ -32,6 +32,10 @@ using ::mediapipe::Timestamp;
@implementation MPPVisionPacketCreator @implementation MPPVisionPacketCreator
+ (Packet)createPacketWithMPPImage:(MPPImage *)image error:(NSError **)error { + (Packet)createPacketWithMPPImage:(MPPImage *)image error:(NSError **)error {
if ((image.imageSourceType == MPPImageSourceTypePixelBuffer || image.imageSourceType == MPPImageSourceTypeSampleBuffer) && [image pixelBuffer] != nil) {
return MakePacket<mediapipe::Image>([image pixelBuffer]);
}
std::unique_ptr<ImageFrame> imageFrame = [image imageFrameWithError:error]; std::unique_ptr<ImageFrame> imageFrame = [image imageFrameWithError:error];
if (!imageFrame) { if (!imageFrame) {
@ -44,14 +48,9 @@ using ::mediapipe::Timestamp;
+ (Packet)createPacketWithMPPImage:(MPPImage *)image + (Packet)createPacketWithMPPImage:(MPPImage *)image
timestampInMilliseconds:(NSInteger)timestampInMilliseconds timestampInMilliseconds:(NSInteger)timestampInMilliseconds
error:(NSError **)error { error:(NSError **)error {
std::unique_ptr<ImageFrame> imageFrame = [image imageFrameWithError:error]; auto packet = [self createPacketWithMPPImage:image error:error];
if (!imageFrame) { return packet.At(Timestamp(int64(timestampInMilliseconds * kMicroSecondsPerMilliSecond)));
return Packet();
}
return MakePacket<Image>(std::move(imageFrame))
.At(Timestamp(int64(timestampInMilliseconds * kMicroSecondsPerMilliSecond)));
} }
+ (Packet)createPacketWithNormalizedRect:(NormalizedRect &)normalizedRect { + (Packet)createPacketWithNormalizedRect:(NormalizedRect &)normalizedRect {