如何使用ARKit和RealityKit检测2D图像

3
我希望使用 ARKit 和 RealityKit 来检测 2D 图像。因为很多基于 RealityKit 的实现,所以不想使用 SceneKit。我找不到任何在 RealityKit 上检测图像的示例。我参考了苹果的示例代码。它使用 Scenekit 和 ARSCNViewDelegate
let arConfiguration = ARWorldTrackingConfiguration()
arConfiguration.planeDetection = [.vertical, .horizontal]
arConfiguration.isLightEstimationEnabled = true
arConfiguration.environmentTexturing = .automatic

if let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "sanitzer", bundle: nil) {
    arConfiguration.maximumNumberOfTrackedImages = 1
    arConfiguration.detectionImages = referenceImages
}
self.session.run(arConfiguration, options: [.resetTracking, .removeExistingAnchors])

我已经实现了ARSessionDelegate,但是无法检测到图像?

func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
    //how to capture image anchor?
}   
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
    //how to capture image anchor?
}

苹果已经实现了ARSCNViewDelegate来捕获检测到的图像。在RealityKit中,相当于ARSCNViewDelegate的委托是什么?如何检测ARImageAnchor?

1个回答

6
ARKit/RealityKit 项目中,使用以下代码调用session()实例方法:
import ARKit
import RealityKit


class ViewController: UIViewController, ARSessionDelegate {

    func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {

        guard let imageAnchor = anchors.first as? ARImageAnchor,
              let _ = imageAnchor.referenceImage.name
        else { return }

        let anchor = AnchorEntity(anchor: imageAnchor)

        // Add Model Entity to anchor
        anchor.addChild(model)

        arView.scene.anchors.append(anchor)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        arView.session.delegate = self
        resetTrackingConfig()
    }

    func resetTrackingConfig() {

        guard let refImg = ARReferenceImage.referenceImages(inGroupNamed: "Sub",
                                                                  bundle: nil)
        else { return }

        let config = ARWorldTrackingConfiguration()
        config.detectionImages = refImg
        config.maximumNumberOfTrackedImages = 1

        let options = [ARSession.RunOptions.removeExistingAnchors,
                       ARSession.RunOptions.resetTracking]

        arView.session.run(config, options: ARSession.RunOptions(options))
    }
}

需要考虑的是,参考图像文件夹(以.png.jpg格式)必须使用.arresourcegroup扩展名。


我总是得到AREnvironmentProbeAnchor,但没有ARImageAnchor。我不知道为什么? - mahbaleshwar hegde
我已经更新了我的答案。使用我的代码,它可以正常工作。 - Andy Jazz
我尝试检查了一下,但对于AREnvironmentProbeAnchor仍然无法正常工作。 - vrat2801
@vrat2801,请发布您自己的问题(附上您编写的代码)。 - Andy Jazz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接