ARKit图像检测和从Assets.xcassets添加图像

问题描述 投票:1回答:1

我正在使用我从Apple Developer网站上的AR Image Detection下载的代码。我试图修改它以在检测到图像后在Resources / Assets.xcassets的AR Resource文件夹中显示特定图像。我看到2年前发布了类似的问题,我尝试了一个问题,但仅对此回答,但没有成功。有人可以帮忙吗?谢谢!

导入ARKit导入SceneKit导入UIKit

类ViewController:UIViewController,ARSCNViewDelegate {

@IBOutlet var sceneView: ARSCNView!

@IBOutlet weak var blurView: UIVisualEffectView!

/// The view controller that displays the status and "restart experience" UI.
lazy var statusViewController: StatusViewController = {
    return children.lazy.compactMap({ $0 as? StatusViewController }).first!
}()

/// A serial queue for thread safety when modifying the SceneKit node graph.
let updateQueue = DispatchQueue(label: Bundle.main.bundleIdentifier! +
    ".serialSceneKitQueue")

/// Convenience accessor for the session owned by ARSCNView.
var session: ARSession {
    return sceneView.session
}

// MARK: - View Controller Life Cycle

override func viewDidLoad() {
    super.viewDidLoad()

    sceneView.delegate = self
    sceneView.session.delegate = self

    // Hook up status view controller callback(s).
    statusViewController.restartExperienceHandler = { [unowned self] in
        self.restartExperience()
    }
}

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

    // Prevent the screen from being dimmed to avoid interuppting the AR experience.
    UIApplication.shared.isIdleTimerDisabled = true

    // Start the AR experience
    resetTracking()
}

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

    session.pause()
}

// MARK: - Session management (Image detection setup)

/// Prevents restarting the session while a restart is in progress.
var isRestartAvailable = true

/// Creates a new AR configuration to run on the `session`.
/// - Tag: ARReferenceImage-Loading
func resetTracking() {

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

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

    statusViewController.scheduleMessage("Look around to detect images", inSeconds: 7.5, messageType: .contentPlacement)
}

// MARK: - ARSCNViewDelegate (Image detection results)
/// - Tag: ARImageAnchor-Visualizing
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    guard let imageAnchor = anchor as? ARImageAnchor else { return }
    let referenceImage = imageAnchor.referenceImage
    updateQueue.async {

        // Create a plane to visualize the initial position of the detected image.
        let plane = SCNPlane(width: referenceImage.physicalSize.width,
                             height: referenceImage.physicalSize.height)
        let planeNode = SCNNode(geometry: plane)
        planeNode.opacity = 0.25

        /*
         `SCNPlane` is vertically oriented in its local coordinate space, but
         `ARImageAnchor` assumes the image is horizontal in its local space, so
         rotate the plane to match.
         */
        planeNode.eulerAngles.x = -.pi / 2

        /*
         Image anchors are not tracked after initial detection, so create an
         animation that limits the duration for which the plane visualization appears.
         */
        planeNode.runAction(self.imageHighlightAction)
        plane.materials = [SCNMaterial()]
        plane.materials[0].diffuse.contents = UIImage(named: "Macbook 12-inch")

        // Add the plane visualization to the scene.
        node.addChildNode(planeNode)



        DispatchQueue.main.async {
            let imageName = referenceImage.name ?? ""
            self.statusViewController.cancelAllScheduledMessages()
            self.statusViewController.showMessage("Detected image “\(imageName)”")
        }
    }


}

var imageHighlightAction: SCNAction {
    return .sequence([
        .wait(duration: 0.25),
        .fadeOpacity(to: 0.85, duration: 0.25),
        .fadeOpacity(to: 0.15, duration: 0.25),
        .fadeOpacity(to: 0.85, duration: 0.25),
        .fadeOut(duration: 0.5),
        .removeFromParentNode()
    ])
}

}

xcode image assets arkit detection
1个回答
0
投票

如果您想知道如何在Xcode中使用图像资产,请使用official web resource

在您的情况下,用于参考图像或对象的文件夹(以.png.jpg格式存储)必须具有扩展名.arresourcegroup

要创建自己的包含参考图像的文件夹,请按照下列步骤操作:

  • 在Xcode的左侧导航窗格中,选择一个文件夹Assets.xcassets
  • 在“ AR资源组”字段中,创建一个新文件夹DetectionImages
  • 在AR应用程序中放置需要检测的所有图像

就是这样。

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

let config = ARWorldTrackingConfiguration()
config.detectionImages = images
config.maximumNumberOfTrackedImages = 3

arView.session.run(config, options: [])
© www.soinside.com 2019 - 2024. All rights reserved.