iOS13 中的 ARBodyTracking:如何获取 body 关节的真实世界坐标?

ARBodyTracking in iOS13: how to get real world coordinates of body joints?

我有一个使用 iOS 13 body 跟踪的应用程序,使用 ARKit 实时在人体 body 中放置一些标记。

下面的代码允许我实时绘制标记并在屏幕上获取它们的坐标。

func session(_ session: ARSession, didUpdate frame: ARFrame) {


    if let detectedBody = frame.detectedBody {

        guard let interfaceOrientation = arView.window?.windowScene?.interfaceOrientation else { return }
        let transform = frame.displayTransform(for: interfaceOrientation, viewportSize: arView.frame.size)

       //This array contains only the lower body joints
             var arrayOfJoints = [detectedBody.skeleton.jointLandmarks[8],detectedBody.skeleton.jointLandmarks[9],detectedBody.skeleton.jointLandmarks[10],detectedBody.skeleton.jointLandmarks[11],detectedBody.skeleton.jointLandmarks[12],detectedBody.skeleton.jointLandmarks[13],detectedBody.skeleton.jointLandmarks[16]]



        //Todo lo de este for estaba en el .forEach
        for i in 0...arrayOfJoints.count - 1 {

            let normalizedCenter = CGPoint(x: CGFloat(arrayOfJoints[i][0]), y: CGFloat(arrayOfJoints[i][1])).applying(transform)
            let center = normalizedCenter.applying(CGAffineTransform.identity.scaledBy(x: arView.frame.width, y: arView.frame.height))

            let circleWidth: CGFloat = 10
            let circleHeight: CGFloat = 10
            let rect = CGRect(origin: CGPoint(x: center.x - circleWidth/2, y: center.y - circleHeight/2), size: CGSize(width: circleWidth, height: circleHeight))
            let circleLayer = CAShapeLayer()


            circleLayer.fillColor = .init(srgbRed: 255, green: 255, blue: 255, alpha: 1.0)


            circleLayer.path = UIBezierPath(ovalIn: rect).cgPath

            arView.layer.addSublayer(circleLayer)


        }

    }

}

但是,我找不到获取真实世界坐标的方法来查看,例如,下蹲过程中以厘米为单位的臀部位移是多少。甚至可以使用 ARKit 将这些点转换为现实世界坐标吗?

非常感谢

您可以使用 ARBodyAnchor 访问 3D 关节位置。使用来自 ARSessionDelegate:

的 didUpdate 锚函数
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
    for anchor in anchors {
        guard let bodyAnchor = anchor as? ARBodyAnchor else { continue }
        // Accessing joints transform
    }
}

您可以获得任一模型转换,它将 return 相对于 root 锚进行转换。以左脚变换为例:

let leftFootModelTransform = bodyAnchor.skeleton.modelTransform(for: "left_foot_joint")

或者您可以访问与 parent 关节相关的局部变换。如果是脚,parent 将是膝关节 "left_leg_joint" :

let leftFootLocalTransform = bodyAnchor.skeleton.localTransform(for: "left_foot_joint")

您可以从 WWDC 视频 Bringing people into AR 中了解更多信息: https://developer.apple.com/videos/play/wwdc2019/607/