我有一个使用iOS13身体追踪的应用,使用ARKit在人体中实时放置一些标记。
下面的代码可以让我实时绘制标记,并在屏幕中获得它们的坐标。
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关节位置。使用didUpdate锚函数从 ARSessionDelegate
:
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
for anchor in anchors {
guard let bodyAnchor = anchor as? ARBodyAnchor else { continue }
// Accessing joints transform
}
}
你可以得到任何一个模型Transform,它将返回Transform与 root
锚。对于左脚变换的例子。
let leftFootModelTransform = bodyAnchor.skeleton.modelTransform(for: "left_foot_joint")
或者你可以访问本地变换,这是与... parent
关节。如果是足部,父关节为膝关节。"left_leg_joint"
:
let leftFootLocalTransform = bodyAnchor.skeleton.localTransform(for: "left_foot_joint")
你可以从WWDC视频中了解更多信息,将人们带入AR。https:/developer.apple.comvideosplaywwdc2019607。