我可以使用ARKit 2获得眼睛的测量结果吗

时间:2018-07-26 05:26:44

标签: ios swift arkit iphone-x ios12

我正在研究一个项目,该项目要求使用iPhone X上的ARKit和True depth摄像头以及Xcode 10 beta 4上的iOS 12 beta 4,使用每只眼睛的大小和两眼之间的距离。

由于我是ARKit的新手,因此我很少了解在线搜索解决方案。但是,我遇到了demo code on apple developer portal。在该代码中,我尝试获取所需的值。

Also referred to the answer通过@rickster

我研究了ARSCNFaceGeometry和ARFaceAnchor以获取少量值。

func update(withFaceAnchor anchor: ARFaceAnchor) {
    let faceGeometry = geometry as! ARSCNFaceGeometry
    faceGeometry.update(from: anchor.geometry)
    print("\nLook at Point :", anchor.lookAtPoint, "\nLeft :", anchor.leftEyeTransform, "\nRight :", anchor.rightEyeTransform)
}

输出:

  

看点:float3(0.14502259,0.2905016,0.97809345)

     

左:simd_float4x4([[0.99312633,-0.030915117,-0.11289084,0.0)],   [0.0,0.9644885,-0.26412484,0.0)],[0.11704737,0.26230934,   0.9578589、0.0)],[0.03311049、0.028666705、0.028539032、1.0)]])

     

右:simd_float4x4([[0.9830616,-0.047965277,-0.17688738,0.0)],   [0.0,0.96514606,-0.26171172,0.0)],[0.18327524,0.25727874,   0.94879806,0.0)],[-0.032072492,0.028482603,0.028181288,1.0)]])

问题

  1. 如何转换这些值以利用它并获得一些 坐标?

  2. 此代码将帮助我实现每只眼睛的大小和距离 眼睛之间。如果没有,我该怎么做才能得到测量结果?

4 个答案:

答案 0 :(得分:2)

在ARKit 2 / Xcode 10 beta 6 / iOS 12中,“ lookAtPoint”为空;但我能够获得“ leftEyeTransform”和“ rightEyeTransform”,然后可以使用此代码(在两个人的不同距离上进行测试,并用尺子对其进行验证!)来计算眼睛之间的距离:

func update(withFaceAnchor anchor: ARFaceAnchor) {
    let faceGeometry = occlusionNode.geometry as! ARSCNFaceGeometry
    faceGeometry.update(from: anchor.geometry)
    print("\nLook at Point:", anchor.lookAtPoint, "\nLeft:", anchor.leftEyeTransform, "\nRight:", anchor.rightEyeTransform)

var leftEyePosition = SCNVector3(anchor.leftEyeTransform.columns.3.x, anchor.leftEyeTransform.columns.3.y, anchor.leftEyeTransform.columns.3.z)
var rightEyePosition = SCNVector3(anchor.rightEyeTransform.columns.3.x, anchor.rightEyeTransform.columns.3.y, anchor.rightEyeTransform.columns.3.z)
let d = distance(float3(leftEyePosition), float3(rightEyePosition))
print("\nInter-eye distance in centimeters: ", d * 100)

}

一些示例输出:

Look at Point: float3(0.0, 0.0, 0.0) 
Left: simd_float4x4([[0.9997796, -0.0031222918, 0.020761598, 0.0)], [0.0, 0.98888004, 0.14871553, 0.0)], [-0.020995062, -0.14868274, 0.98866206, 0.0)], [0.031608228, 0.031428084, 0.026632207, 1.0)]]) 
Right: simd_float4x4([[0.99887705, 0.007054036, -0.04684897, 0.0)], [0.0, 0.9888536, 0.14889139, 0.0)], [0.047377057, -0.1487242, 0.98774314, 0.0)], [-0.031623494, 0.03171173, 0.02669965, 1.0)]])

Inter-eye distance in centimeters:  6.32324

注意:

答案 1 :(得分:1)

您可以从转换中获取vector3:

var leftEyePosition = SCNVector3(leftTransform.columns.3.x, leftTransform.columns.3.y, leftTransform.columns.3.z)

var rightEyePosition = SCNVector3(rightTransform.columns.3.x, rightTransform.columns.3.y, rightTransform.columns.3.z)

然后您可以像这样获得距离形式2 SCNVector3。

let d = distance(float3(leftEyePosition), float3(rightEyePosition)) 

结果将以米为单位,因此您可能需要将d除以100

答案 2 :(得分:0)

没有简单的方法来实际获得学生的身材。有关更多信息,请参见此StackOverFlow线程和Rickster提供的答案:Getting The Sizes Of The Eyes In ARKit

不过,可以使用leftEyeTransformrightEyeTransforms来获得眼睛之间的估计距离,这很简单:

  

表示矩阵位置和方向的变换矩阵   脸的左眼和右眼。

enter image description here

您感兴趣的是其中的第三列,其中包含我们需要的数据。

一种简单的计算距离的方法是使用不需要任何其他扩展名的GLK帮助器。

这样的示例可能看起来像这样:

//----------------------
//MARK ARSCNViewDelegate
//----------------------

extension ViewController: ARSCNViewDelegate{

    /// Creates A GLKVector3 From a Simd_Float4
    ///
    /// - Parameter transform: simd_float4
    /// - Returns: GLKVector3
    func glkVector3FromARFaceAnchorTransform(_ transform: simd_float4) -> GLKVector3{

        return GLKVector3Make(transform.x, transform.y, transform.z)
    }

    func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {

        //1. Check We Have A Valid ARFaceAnchor
        guard let faceAnchor = anchor as? ARFaceAnchor else { return }


        //2. Get The Position Of The Left & Right Eyes
        let leftEyePosition = glkVector3FromARFaceAnchorTransform(faceAnchor.leftEyeTransform.columns.3)
        let righEyePosition = glkVector3FromARFaceAnchorTransform(faceAnchor.rightEyeTransform.columns.3)

        //3. Calculate The Distance Between Them
        let distanceBetweenEyesInMetres = GLKVector3Distance(leftEyePosition, righEyePosition)
        let distanceBetweenEyesInCM = distanceBetweenEyesInMetres/100
        print("The Distance Between The Eyes Is Approximatly \(distanceBetweenEyesInCM)")

    }

}

希望有帮助...

答案 3 :(得分:0)

针对学生距离的一线解决方案:

simd_distance(faceAnchor.rightEyeTransform.columns.3, faceAnchor.leftEyeTransform.columns.3)

乘以100即可得到以厘米为单位的答案。 如上所述,没有一种简单的方法可以测量虹膜或瞳孔大小,因为您需要开发一种算法来执行以下操作:

1)找到圆圈并过滤出描述虹膜的圆圈

2)使用ARKit计算像素到毫米的值,然后使用它计算光圈大小