diff --git a/examples/face_landmarker/ios/FaceLandmarker/Base.lproj/Main.storyboard b/examples/face_landmarker/ios/FaceLandmarker/Base.lproj/Main.storyboard index f704b86c..10537bff 100644 --- a/examples/face_landmarker/ios/FaceLandmarker/Base.lproj/Main.storyboard +++ b/examples/face_landmarker/ios/FaceLandmarker/Base.lproj/Main.storyboard @@ -1,11 +1,12 @@ - + - + + @@ -260,6 +261,44 @@ + + + + + + + + + + + + + + + + @@ -268,6 +307,7 @@ + @@ -297,7 +337,9 @@ + + @@ -316,6 +358,7 @@ + diff --git a/examples/face_landmarker/ios/FaceLandmarker/Configurations/DefaultConstants.swift b/examples/face_landmarker/ios/FaceLandmarker/Configurations/DefaultConstants.swift index 95dfafab..27f76c42 100644 --- a/examples/face_landmarker/ios/FaceLandmarker/Configurations/DefaultConstants.swift +++ b/examples/face_landmarker/ios/FaceLandmarker/Configurations/DefaultConstants.swift @@ -14,6 +14,7 @@ import Foundation import UIKit +import MediaPipeTasksVision // MARK: Define default constants struct DefaultConstants { @@ -35,4 +36,41 @@ struct DefaultConstants { static var minFacePresenceConfidence: Float = 0.5 static var minTrackingConfidence: Float = 0.5 static let modelPath: String? = Bundle.main.path(forResource: "face_landmarker", ofType: "task") + static let delegate: FaceLandmarkerDelegate = .CPU } + +// MARK: FaceLandmarkerDelegate +enum FaceLandmarkerDelegate: CaseIterable { + case GPU + case CPU + + var name: String { + switch self { + case .GPU: + return "GPU" + case .CPU: + return "CPU" + } + } + + var delegate: Delegate { + switch self { + case .GPU: + return .GPU + case .CPU: + return .CPU + } + } + + init?(name: String) { + switch name { + case FaceLandmarkerDelegate.CPU.name: + self = FaceLandmarkerDelegate.CPU + case FaceLandmarkerDelegate.GPU.name: + self = FaceLandmarkerDelegate.GPU + default: + return nil + } + } +} + diff --git a/examples/face_landmarker/ios/FaceLandmarker/Configurations/InferenceConfigurationManager.swift b/examples/face_landmarker/ios/FaceLandmarker/Configurations/InferenceConfigurationManager.swift index 5fcbe08a..cdefb1f7 100644 --- a/examples/face_landmarker/ios/FaceLandmarker/Configurations/InferenceConfigurationManager.swift +++ b/examples/face_landmarker/ios/FaceLandmarker/Configurations/InferenceConfigurationManager.swift @@ -38,6 +38,10 @@ class InferenceConfigurationManager: NSObject { didSet { postConfigChangedNotification() } } + var delegate: FaceLandmarkerDelegate = DefaultConstants.delegate { + didSet { postConfigChangedNotification() } + } + static let sharedInstance = InferenceConfigurationManager() static let notificationName = Notification.Name.init(rawValue: "com.google.mediapipe.inferenceConfigChanged") @@ -46,5 +50,4 @@ class InferenceConfigurationManager: NSObject { NotificationCenter.default .post(name: InferenceConfigurationManager.notificationName, object: nil) } - } diff --git a/examples/face_landmarker/ios/FaceLandmarker/Services/FaceLandmarkerService.swift b/examples/face_landmarker/ios/FaceLandmarker/Services/FaceLandmarkerService.swift index d162f37d..63177aae 100644 --- a/examples/face_landmarker/ios/FaceLandmarker/Services/FaceLandmarkerService.swift +++ b/examples/face_landmarker/ios/FaceLandmarker/Services/FaceLandmarkerService.swift @@ -48,7 +48,9 @@ class FaceLandmarkerService: NSObject { private var minFaceDetectionConfidence: Float private var minFacePresenceConfidence: Float private var minTrackingConfidence: Float - var modelPath: String + private var modelPath: String + private var delegate: FaceLandmarkerDelegate + // MARK: - Custom Initializer private init?(modelPath: String?, @@ -56,7 +58,8 @@ class FaceLandmarkerService: NSObject { numFaces: Int, minFaceDetectionConfidence: Float, minFacePresenceConfidence: Float, - minTrackingConfidence: Float) { + minTrackingConfidence: Float, + delegate: FaceLandmarkerDelegate) { guard let modelPath = modelPath else { return nil } self.modelPath = modelPath self.runningMode = runningMode @@ -64,6 +67,7 @@ class FaceLandmarkerService: NSObject { self.minFaceDetectionConfidence = minFaceDetectionConfidence self.minFacePresenceConfidence = minFacePresenceConfidence self.minTrackingConfidence = minTrackingConfidence + self.delegate = delegate super.init() createFaceLandmarker() @@ -77,6 +81,7 @@ class FaceLandmarkerService: NSObject { faceLandmarkerOptions.minFacePresenceConfidence = minFacePresenceConfidence faceLandmarkerOptions.minTrackingConfidence = minTrackingConfidence faceLandmarkerOptions.baseOptions.modelAssetPath = modelPath + faceLandmarkerOptions.baseOptions.delegate = delegate.delegate if runningMode == .liveStream { faceLandmarkerOptions.faceLandmarkerLiveStreamDelegate = self } @@ -95,14 +100,16 @@ class FaceLandmarkerService: NSObject { minFaceDetectionConfidence: Float, minFacePresenceConfidence: Float, minTrackingConfidence: Float, - videoDelegate: FaceLandmarkerServiceVideoDelegate?) -> FaceLandmarkerService? { + videoDelegate: FaceLandmarkerServiceVideoDelegate?, + delegate: FaceLandmarkerDelegate) -> FaceLandmarkerService? { let faceLandmarkerService = FaceLandmarkerService( modelPath: modelPath, runningMode: .video, numFaces: numFaces, minFaceDetectionConfidence: minFaceDetectionConfidence, minFacePresenceConfidence: minFacePresenceConfidence, - minTrackingConfidence: minTrackingConfidence) + minTrackingConfidence: minTrackingConfidence, + delegate: delegate) faceLandmarkerService?.videoDelegate = videoDelegate return faceLandmarkerService } @@ -113,14 +120,16 @@ class FaceLandmarkerService: NSObject { minFaceDetectionConfidence: Float, minFacePresenceConfidence: Float, minTrackingConfidence: Float, - liveStreamDelegate: FaceLandmarkerServiceLiveStreamDelegate?) -> FaceLandmarkerService? { + liveStreamDelegate: FaceLandmarkerServiceLiveStreamDelegate?, + delegate: FaceLandmarkerDelegate) -> FaceLandmarkerService? { let faceLandmarkerService = FaceLandmarkerService( modelPath: modelPath, runningMode: .liveStream, numFaces: numFaces, minFaceDetectionConfidence: minFaceDetectionConfidence, minFacePresenceConfidence: minFacePresenceConfidence, - minTrackingConfidence: minTrackingConfidence) + minTrackingConfidence: minTrackingConfidence, + delegate: delegate) faceLandmarkerService?.liveStreamDelegate = liveStreamDelegate return faceLandmarkerService @@ -131,14 +140,16 @@ class FaceLandmarkerService: NSObject { numFaces: Int, minFaceDetectionConfidence: Float, minFacePresenceConfidence: Float, - minTrackingConfidence: Float) -> FaceLandmarkerService? { + minTrackingConfidence: Float, + delegate: FaceLandmarkerDelegate) -> FaceLandmarkerService? { let faceLandmarkerService = FaceLandmarkerService( modelPath: modelPath, runningMode: .image, numFaces: numFaces, minFaceDetectionConfidence: minFaceDetectionConfidence, minFacePresenceConfidence: minFacePresenceConfidence, - minTrackingConfidence: minTrackingConfidence) + minTrackingConfidence: minTrackingConfidence, + delegate: delegate) return faceLandmarkerService } diff --git a/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/BottomSheetViewController.swift b/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/BottomSheetViewController.swift index ec9f673b..408c4a55 100644 --- a/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/BottomSheetViewController.swift +++ b/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/BottomSheetViewController.swift @@ -44,6 +44,7 @@ class BottomSheetViewController: UIViewController { @IBOutlet weak var minTrackingConfidenceValueLabel: UILabel! @IBOutlet weak var toggleBottomSheetButton: UIButton! + @IBOutlet weak var chooseDelegateButton: UIButton! // MARK: Instance Variables var isUIEnabled: Bool = false { @@ -77,8 +78,29 @@ class BottomSheetViewController: UIViewController { minTrackingConfidenceStepper.value = Double(InferenceConfigurationManager.sharedInstance.minTrackingConfidence) minTrackingConfidenceValueLabel.text = "\(InferenceConfigurationManager.sharedInstance.minTrackingConfidence)" + + // Chose delegate option + let selectedDelegateAction = {(action: UIAction) in + self.updateDelegate(title: action.title) + } + let delegateActions: [UIAction] = FaceLandmarkerDelegate.allCases.compactMap { delegate in + return UIAction( + title: delegate.name, + state: (InferenceConfigurationManager.sharedInstance.delegate == delegate) ? .on : .off, + handler: selectedDelegateAction + ) + } + + chooseDelegateButton.menu = UIMenu(children: delegateActions) + chooseDelegateButton.showsMenuAsPrimaryAction = true + chooseDelegateButton.changesSelectionAsPrimaryAction = true } - + + private func updateDelegate(title: String) { + guard let delegate = FaceLandmarkerDelegate(name: title) else { return } + InferenceConfigurationManager.sharedInstance.delegate = delegate + } + private func enableOrDisableClicks() { numFacesStepper.isEnabled = isUIEnabled minFaceDetectionConfidenceStepper.isEnabled = isUIEnabled diff --git a/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/CameraViewController.swift b/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/CameraViewController.swift index 8ea80fd5..e01eac64 100644 --- a/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/CameraViewController.swift +++ b/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/CameraViewController.swift @@ -155,7 +155,8 @@ class CameraViewController: UIViewController { minFaceDetectionConfidence: InferenceConfigurationManager.sharedInstance.minFaceDetectionConfidence, minFacePresenceConfidence: InferenceConfigurationManager.sharedInstance.minFacePresenceConfidence, minTrackingConfidence: InferenceConfigurationManager.sharedInstance.minTrackingConfidence, - liveStreamDelegate: self) + liveStreamDelegate: self, + delegate: InferenceConfigurationManager.sharedInstance.delegate) } private func clearFaceLandmarkerServiceOnSessionInterruption() { diff --git a/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/MediaLibraryViewController.swift b/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/MediaLibraryViewController.swift index fa55313b..7925ce7c 100644 --- a/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/MediaLibraryViewController.swift +++ b/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/MediaLibraryViewController.swift @@ -300,7 +300,8 @@ extension MediaLibraryViewController: UIImagePickerControllerDelegate, UINavigat numFaces: InferenceConfigurationManager.sharedInstance.numFaces, minFaceDetectionConfidence: InferenceConfigurationManager.sharedInstance.minFaceDetectionConfidence, minFacePresenceConfidence: InferenceConfigurationManager.sharedInstance.minFacePresenceConfidence, - minTrackingConfidence: InferenceConfigurationManager.sharedInstance.minTrackingConfidence) + minTrackingConfidence: InferenceConfigurationManager.sharedInstance.minTrackingConfidence, + delegate: InferenceConfigurationManager.sharedInstance.delegate) case .video: faceLandmarkerService = FaceLandmarkerService.videoFaceLandmarkerService( modelPath: InferenceConfigurationManager.sharedInstance.modelPath, @@ -308,7 +309,8 @@ extension MediaLibraryViewController: UIImagePickerControllerDelegate, UINavigat minFaceDetectionConfidence: InferenceConfigurationManager.sharedInstance.minFaceDetectionConfidence, minFacePresenceConfidence: InferenceConfigurationManager.sharedInstance.minFacePresenceConfidence, minTrackingConfidence: InferenceConfigurationManager.sharedInstance.minTrackingConfidence, - videoDelegate: self) + videoDelegate: self, + delegate: InferenceConfigurationManager.sharedInstance.delegate) default: break; } diff --git a/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/RootViewController.swift b/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/RootViewController.swift index 878c535f..1e7d93cd 100644 --- a/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/RootViewController.swift +++ b/examples/face_landmarker/ios/FaceLandmarker/ViewContoller/RootViewController.swift @@ -35,7 +35,7 @@ class RootViewController: UIViewController { // MARK: Constants private struct Constants { - static let inferenceBottomHeight = 260.0 + static let inferenceBottomHeight = 304.0 static let expandButtonHeight = 41.0 static let expandButtonTopSpace = 10.0 static let mediaLibraryViewControllerStoryBoardId = "MEDIA_LIBRARY_VIEW_CONTROLLER" diff --git a/examples/face_landmarker/ios/Podfile b/examples/face_landmarker/ios/Podfile index d788f7eb..aeb6f601 100644 --- a/examples/face_landmarker/ios/Podfile +++ b/examples/face_landmarker/ios/Podfile @@ -5,7 +5,7 @@ target 'FaceLandmarker' do # Comment the next line if you don't want to use dynamic frameworks use_frameworks! - pod 'MediaPipeTasksVision', '0.10.5' + pod 'MediaPipeTasksVision', '0.10.12' end diff --git a/examples/face_landmarker/ios/Podfile.lock b/examples/face_landmarker/ios/Podfile.lock index 2d37b530..325a2e1b 100644 --- a/examples/face_landmarker/ios/Podfile.lock +++ b/examples/face_landmarker/ios/Podfile.lock @@ -1,10 +1,10 @@ PODS: - - MediaPipeTasksCommon (0.10.5) - - MediaPipeTasksVision (0.10.5): - - MediaPipeTasksCommon (= 0.10.5) + - MediaPipeTasksCommon (0.10.12) + - MediaPipeTasksVision (0.10.12): + - MediaPipeTasksCommon (= 0.10.12) DEPENDENCIES: - - MediaPipeTasksVision (= 0.10.5) + - MediaPipeTasksVision (= 0.10.12) SPEC REPOS: trunk: @@ -12,9 +12,9 @@ SPEC REPOS: - MediaPipeTasksVision SPEC CHECKSUMS: - MediaPipeTasksCommon: 235e81afa9f7bd5b5b39f00a858bfb2539d1a52f - MediaPipeTasksVision: d58f3a82c9fbda8c3be1bf2a1bf4d73e56a457e3 + MediaPipeTasksCommon: 254e6bff77804b262f6ecf180477142ea551e802 + MediaPipeTasksVision: 78d5c47cd7996b4d815bacba0a52dbf01458dfaf -PODFILE CHECKSUM: 190e0b8d95f1326900e7ad10066ef87dc9a47676 +PODFILE CHECKSUM: 67c95a3ac9f403fadf44f5735ace6ec9fcf479a6 -COCOAPODS: 1.12.1 +COCOAPODS: 1.14.3