From 6de355eab88d1528c81901690631a1d2641022c6 Mon Sep 17 00:00:00 2001 From: Mautisim Munir Date: Sun, 16 Oct 2022 04:36:09 +0500 Subject: [PATCH] swift lindera files --- mediapipe/swift/solutions/lindera/BUILD | 6 ++ .../swift/solutions/lindera/Lindera.swift | 96 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 mediapipe/swift/solutions/lindera/BUILD create mode 100644 mediapipe/swift/solutions/lindera/Lindera.swift diff --git a/mediapipe/swift/solutions/lindera/BUILD b/mediapipe/swift/solutions/lindera/BUILD new file mode 100644 index 000000000..414cebc1a --- /dev/null +++ b/mediapipe/swift/solutions/lindera/BUILD @@ -0,0 +1,6 @@ +load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library") + +swift_library( + name = "Lindera", + srcs = glob(["*.swift"]), +) diff --git a/mediapipe/swift/solutions/lindera/Lindera.swift b/mediapipe/swift/solutions/lindera/Lindera.swift new file mode 100644 index 000000000..d0794bd61 --- /dev/null +++ b/mediapipe/swift/solutions/lindera/Lindera.swift @@ -0,0 +1,96 @@ +final class Lindera { + + var cameraView: UIView { + self.linderaExerciseSession + } + + weak var delegate: LinderaDelegate? + + private lazy var linderaExerciseSession: LinderaExerciseSessionView = { + let session = LinderaExerciseSessionView() + session.detectionSpeed = .high + session.processCameraFrames = true + session.enable3DPoseDetection = true + session.pose3DDetectionHandler = { [weak self] event in + + guard let self = self, let pose = event.pose.map({ Asensei3DPose($0) }) else { + return + } + + self.delegate?.lindera(self, didDetect: .init(pose: pose, timestamp: event.sourceTimestamp)) + } + + return session + }() + + required init () { } + + func startCamera(_ completion: ((Result) -> Void)? = nil) { + DispatchQueue.main.async { [weak self] in + self?.linderaExerciseSession.startCamera { result in + switch result { + case .success: + completion?(.success(Void())) + case .failure(let error): + completion?(.failure(error)) + } + } + } + } + + func stopCamera() { + DispatchQueue.main.async { [weak self] in + self?.linderaExerciseSession.stopCamera() + } + } + + func switchCamera(_ completion: ((Result) -> Void)? = nil) { + DispatchQueue.main.async { [weak self] in + self?.linderaExerciseSession.switchCamera(completionHandler: completion) + } + } + + func selectCamera(_ position: AVCaptureDevice.Position, _ completion: ((Result) -> Void)? = nil) { + DispatchQueue.main.async { [weak self] in + self?.linderaExerciseSession.setUseFrontCamera(position == .front, completionHandler: completion) + } + } +} + +protocol LinderaDelegate: AnyObject { + + func lindera(_ lindera: Lindera, didDetect event: Asensei3DPose.Event) +} + +// MARK: - Helpers +extension Asensei3DPose { + + init(_ pose: Lindera3DPose) { + self.pelvis = pose.landmarks[.pelvis].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.rightHip = pose.landmarks[.rightHip].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.rightKnee = pose.landmarks[.rightKnee].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.rightAnkle = pose.landmarks[.rightAnkle].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.leftHip = pose.landmarks[.leftHip].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.leftKnee = pose.landmarks[.leftKnee].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.leftAnkle = pose.landmarks[.leftAnkle].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.spine = pose.landmarks[.spine].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.thorax = pose.landmarks[.thorax].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.neckNose = pose.landmarks[.neckToNose].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.headTop = pose.landmarks[.headTop].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.leftShoulder = pose.landmarks[.leftShoulder].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.leftElbow = pose.landmarks[.leftElbow].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.leftWrist = pose.landmarks[.leftWrist].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.rightShoulder = pose.landmarks[.rightShoulder].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.rightElbow = pose.landmarks[.rightElbow].map { .init(position: .init($0.position), confidence: $0.confidence) } + self.rightWrist = pose.landmarks[.rightWrist].map { .init(position: .init($0.position), confidence: $0.confidence) } + } +} + +extension Asensei3DPose.Vector3D { + + init(_ vector: Lindera3DVector) { + self.x = -vector.x + self.y = vector.z + self.z = vector.y + } +}