【问题标题】:ARKit does not recognize reference imagesARKit 无法识别参考图像
【发布时间】:2021-12-26 14:31:50
【问题描述】:

我正在尝试使用 ARKit 和 RealityKit 将 3D 模型放置在已识别的图像之上 - 全部以编程方式进行。在启动 ARView 之前,我正在下载要在检测到参考图像时显示的模型。

这是我目前的设置:

override func viewDidLoad() {
    super.viewDidLoad()
    
    arView.session.delegate = self
    
    // Check if the device supports the AR experience
    if (!ARConfiguration.isSupported) {
        TLogger.shared.error_objc("Device does not support Augmented Reality")
        return
    }
    
    guard let qrCodeReferenceImage = UIImage(named: "QRCode") else { return }
    let detectionImages: Set<ARReferenceImage> = convertToReferenceImages([qrCodeReferenceImage])
    
    let configuration = ARWorldTrackingConfiguration()
    configuration.detectionImages = detectionImages
    
    arView.session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
}

我使用 ARSessionDelegate 在添加新图像锚点时收到通知,这意味着检测到参考图像:

func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
    print("Hello")
    for anchor in anchors {
        guard let imageAnchor = anchor as? ARImageAnchor else { return }
        let referenceImage = imageAnchor.referenceImage
        
        addEntity(self.localModelPath!)
    }
}

但是,委托方法永远不会被调用,而其他委托函数(如 func session(ARSession, didUpdate: ARFrame) )正在被调用,所以我假设会话只是没有检测到图像。图像分辨率很好,打印的图像很大,所以它肯定会被 ARSession 识别。在将其添加到配置之前,我还检查了该图像是否已找到。

任何人都可以在这里引导我走向正确的方向吗?

【问题讨论】:

  • 您是否尝试将图像添加到项目中以查看是否有效?如果图像不适合识别,AR 资源文件夹会警告您。

标签: ios swift arkit realitykit


【解决方案1】:

看起来您的配置设置正确。识别参考图像时,应调用您的委托函数。确保您的配置不会在代码中的任何位置被覆盖。

【讨论】:

  • 配置实际上被 viewWillAppear 函数覆盖了。我不知道为什么它会在 viewDidLoad 之后执行,但是在我从代码中删除 viewWillAppear 函数之后,问题就解决了。
  • 这不是答案,你是在向 OP 提问,应该是评论
  • 感谢您的反馈。这是我第一次回答问题,我对社区很陌生。我把最后一句话改成了建议。
猜你喜欢
  • 2018-08-06
  • 2019-01-26
  • 1970-01-01
  • 2018-11-25
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
  • 2017-12-14
  • 1970-01-01
相关资源
最近更新 更多