ARKit: notification upon feature point detection? - ios

This answer and others explain how to get notified when ARKit detects anchors or planes, but how do you get notifications when ARKit detects feature points?

Looking at the APIs it's somewhat similar to the answers that you have linked to.
Using ARSessionDelegate session(_ session: ARSession, didUpdate frame: ARFrame) you can access the rawFeaturePoints of the ARFrame that just got passed in.
So it would look something like:
// Not actually tested
class MyARSessionDelegate: ARSessionDelegate {
var previouslyDetectedPointCount = 0
func session(_ session: ARSession, didUpdate frame: ARFrame) {
// Check if new points are detected
if previouslyDetectedPointCount != frame.rawFeaturePoints?.points.count {
// point count has changed
previouslyDetectedPointCount = frame.rawFeaturePoints!.points.count
}
}
}
Though as to why you would want to be looking for the specific points is curious. The documentation clearly states:
ARKit does not guarantee that the number and arrangement of raw
feature points will remain stable between software releases, or even
between subsequent frames in the same session.

This doesn't seem like the ideal solution, but it works. Implement the func session(_ session: ARSession, didUpdate frame: ARFrame) function from the ARSessionDelegate protocol, and check for feature points in each frame.
func session(_ session: ARSession, didUpdate frame: ARFrame) {
// Show tap icon once we find feature points
if !detectedFeaturePoints, let points = frame.rawFeaturePoints?.points, let firstPoint = points.first {
detectedFeaturePoints = true
}
}

Related

Horizontal plane detection limitations?

I'm trying to build an ARKit based app which requires detection of roads and placing virtual content 30 feet away from the camera. However horizontal plane detection is stopping to add anchors after about 10 feet. Is there a workaround for this problem?
public func session(_ session: ARSession, didUpdate frame: ARFrame) {
guard let usdzEntity = usdzEntity else { return }
let camera = frame.camera
let transform = camera.transform
if let rayCast = arView.scene.raycast(from: transform.translation, to: usdzEntity.transform.translation, query: .nearest, mask: .default, relativeTo: nil).first {
print(rayCast.distance)
}
}
look at this, hope this can give you some help

RealityKit ARRaycastQuery after ARView has looked away

I have a bit of a long winded process at the moment which retains an ARFrame from the ARSessionDelegate's func session(_ session: ARSession, didUpdate frame: ARFrame) callback.
I then do some processing which can take anywhere between 2-5 seconds in which the user of the app can move the camera and point it at a different location in AR then when I grabbed the ARFrame.
I noticed that the ARFrame object has it's own method to produce an ARRaycastQuery which I assume would be relative to that frame regardless of where the camera is currently pointed.
Here is a function where I use the ARRaycastQuery from an ARFrame and execute it against the ARSession
func getQuery(forPosition point: CGPoint, frame: ARFrame) -> ARRaycastResult? {
let estimatedPlane = ARRaycastQuery.Target.estimatedPlane
let alignment = ARRaycastQuery.TargetAlignment.any
let raycastQuery: ARRaycastQuery = frame.raycastQuery(from: point, allowing: estimatedPlane, alignment: alignment)
guard let raycastResult = arKitCoordinator.arView.session.raycast(raycastQuery).first else {
return nil
}
return raycastResult
}
However, If I have moved that camera (ARView view port) away from where I captured the ARFrame I always get nil. I would expect to be able to add objects into AR even if the user has moved the view port (iPhone) away from where I got the frame.
How can I add object into the "invisible" or "out of view" portions of the AR Space?

ARKit does not recognize reference images

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?
It looks like you have your configuration set up correctly. Your delegate-function should be called when the reference image is recognized. Make sure your configuration isn't overwritten at any point in your code.

How to stop CoreML from running when it's no longer needed in the app?

My app runs Vision on a CoreML model. The camera frames the machine learning model runs on are from an ARKit sceneView (basically, the camera). I have a method that's called loopCoreMLUpdate() that continuously runs CoreML so that we keep running the model on new camera frames. The code looks like this:
import UIKit
import SceneKit
import ARKit
class MyViewController: UIViewController {
var visionRequests = [VNRequest]()
let dispatchQueueML = DispatchQueue(label: "com.hw.dispatchqueueml") // A Serial Queue
override func viewDidLoad() {
super.viewDidLoad()
// Setup ARKit sceneview
// ...
// Begin Loop to Update CoreML
loopCoreMLUpdate()
}
// This is the problematic part.
// In fact - once it's run there's no way to stop it, is there?
func loopCoreMLUpdate() {
// Continuously run CoreML whenever it's ready. (Preventing 'hiccups' in Frame Rate)
dispatchQueueML.async {
// 1. Run Update.
self.updateCoreML()
// 2. Loop this function.
self.loopCoreMLUpdate()
}
}
func updateCoreML() {
///////////////////////////
// Get Camera Image as RGB
let pixbuff : CVPixelBuffer? = (sceneView.session.currentFrame?.capturedImage)
if pixbuff == nil { return }
let ciImage = CIImage(cvPixelBuffer: pixbuff!)
// Note: Not entirely sure if the ciImage is being interpreted as RGB, but for now it works with the Inception model.
// Note2: Also uncertain if the pixelBuffer should be rotated before handing off to Vision (VNImageRequestHandler) - regardless, for now, it still works well with the Inception model.
///////////////////////////
// Prepare CoreML/Vision Request
let imageRequestHandler = VNImageRequestHandler(ciImage: ciImage, options: [:])
// let imageRequestHandler = VNImageRequestHandler(cgImage: cgImage!, orientation: myOrientation, options: [:]) // Alternatively; we can convert the above to an RGB CGImage and use that. Also UIInterfaceOrientation can inform orientation values.
///////////////////////////
// Run Image Request
do {
try imageRequestHandler.perform(self.visionRequests)
} catch {
print(error)
}
}
}
As you can see the loop effect is created by a DispatchQueue with the label com.hw.dispatchqueueml that keeps calling loopCoreMLUpdate(). Is there any way to stop the queue once CoreML is not needed anymore? Full code is here.
I suggest instead o running coreML model here in viewDidLoad, you can use ARSessionDelegate function for the same.
func session(_ session: ARSession, didUpdate frame: ARFrame) method to get the frame, you can set the flag, here to enable when you want the the model to work and when you dont.
Like this below:
func session(_ session: ARSession, didUpdate frame: ARFrame) {
// This is where we will analyse our frame
// We return early if currentBuffer is not nil or the tracking state of camera is not normal
// TODO: - Core ML Functionality Commented
guard isMLFlow else { //
return
}
currentBuffer = frame.capturedImage
guard let buffer = currentBuffer, let image = UIImage(pixelBuffer: buffer) else { return }
<Code here to load model>
CoreMLManager.manager.updateClassifications(for: image)
}

How to Constantly get rotation/eulerAngles values of ARCamera

Hey I an trying to constantly get the value of the devices camera / ARCamera. As far as I know there is only one function the allows me to access these ARCamera traits. That is this function here:
Code:
// Only gets called couple times when camera state changes
func session(_ session: ARSession, cameraDidChangeTrackingState camera: ARCamera)
print("\(camera.eulerAngles)")
}
I've been thinking about maybe using some trickery like putting a repeating timer in the function that would call that value. But I can't call a local selectors that get booted out. What I'm more looking for is something along the lines of how this function is:
func renderer(_ aRenderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
// This constantly gets called.
}
I wonder if there is a way to incorporate the ARCamera into the function.
If you want to continuously get updates on camera state, implement ARSessionDelegate.session(_:didUpdate:):
class MyDelegate: ARSessionDelegate {
func session(_ session: ARSession, didUpdate frame: ARFrame) {
print("\(frame.camera)")
}
/* ... */
}
The ARFrame object shall contain camera field with all the necessary information.
If you just want to know when tracking state changes, you might want to store the state from session(_:cameraDidChangeTrackingState:) in a field, and refer to it in your rendering loop:
class MyDelegate: SCNSceneRendererDelegate, ARSessionObserver {
var camera: ARCamera! = nil
func session(_ session: ARSession, cameraDidChangeTrackingState camera: ARCamera) {
self.camera = camera
}
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
print("\(self.camera.trackingState)")
}
/* ... more methods ... */
}

Resources