简体   繁体   中英

How can I rescan an image in ARKit?

in my project I'm using ARKit for detecting a specific image, and when he is detected, the app show me the information. If I've already scan the image, and I want to rescan it for seeing the information, it doesn't work. This is the code that I used for the image recognition:

    sceneView.delegate = self
    sceneView.showsFPS = true
    sceneView.showsNodeCount = true

    if let scene = SKScene(fileNamed: "Scene") {
        sceneView.presentScene(scene)
    }

    guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "image", bundle: nil) else {
        fatalError("Missing expected asset catalog resources.")
    }

    let configuration = ARWorldTrackingConfiguration()
    configuration.detectionImages = referenceImages

    sceneView.session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
}


// MARK: - ARSKViewDelegate
func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode? {

    if let imageAnchor = anchor as? ARImageAnchor,
        let referenceImageName = imageAnchor.referenceImage.name,
        let scannedImage =  self.images[referenceImageName] {

        self.selectedImage = scannedImage

        self.performSegue(withIdentifier: "showImageInformation", sender: self)

    }

    return nil
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showImageInformation"{
        if let imageInformationVC = segue.destination as? ImageInformationViewController,
            let actualSelectedImage = selectedImage {
            imageInformationVC.imageInformation = actualSelectedImage
        }
    }
}


The only way is to reset your current session.
Example:

func resetExperience(session: ARSession, configuration: ARWorldTrackingConfiguration) {

    guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "image", bundle: nil) else {
        fatalError("Missing expected asset catalog resources.")
    }

    configuration.detectionImages = referenceImages
    session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
}

And some general info: ARWorldTrackingConfiguration .
Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM