Home > Blockchain >  ARKit does not recognize reference images
ARKit does not recognize reference images

Time:11-16

I'm trying to place a 3D model on top of a recognized image with ARKit and RealityKit - all programmatically. Before I start the ARView I'm downloading the model I want to show when the reference image is detected.

This is my current setup:

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])
}

I use the ARSessionDelegate to get notified when a new image anchor was added which means the reference image got detected:

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!)
    }
}

However, the delegate method never gets called while other delegate functions like func session(ARSession, didUpdate: ARFrame) are getting called so I assume that the session just doesn't detect the image. The image resolution is good and the printed image the big so it should definitely get recognized by the ARSession. I also checked that the image has been found before adding it to the configuration.

Can anyone lead me in the right direction here?

CodePudding user response:

It looks like you have your configuration set up correctly. Your delegate-function should be called when the reference image is recognized. Could you check if there is any point in your code where the configuration is overwritten?

  • Related