1

I am trying to detect multiple QR code in the screen and adding the information on top of it using ARKit .

I used Vision API for multiple QR code detection and it works completely fine but while hit testing the multiple QR code at once , HitTestResults is always nil . If the SINGLE QR code is detected it works fine with hittest and i add the node with its position ie hittest.worldtransform but while i try to hit test multiple detected qr code bounding box it always fails/becomes nil . Can anyone have same issue or help me with this problem?

I used Vision API for multiple QR code detection and it works completely fine but while hit testing the multiple QR code at once , HitTestResults is always nil . If the SINGLE QR code is detected it works fine with hittest and i add the node with its position ie hittest.worldtransform but while i try to hit test multiple detected qr code bounding box it always fails/becomes nil .

func startQrCodeDetection() {

    self.names.removeAll()

    // Create a Barcode Detection Request
    let request = VNDetectBarcodesRequest(completionHandler: self.requestHandler)

    request.symbologies = [.QR]
    self.qrRequests = [request]
}

func requestHandler(request: VNRequest, error: Error?) {
    // Get the result out of the results, if there are any

    if let results = request.results, let result = results.first as? VNBarcodeObservation {


        // Get the bounding box for the bar code and find the center


        DispatchQueue.main.async {
            for each in results {
                guard let observer = each as? VNBarcodeObservation else { return }

                 let name = observer.payloadStringValue

                var rect = observer.boundingBox
                // Flip coordinates
                rect = rect.applying(CGAffineTransform(scaleX: 1, y: -1))
                rect = rect.applying(CGAffineTransform(translationX: 0, y: 1))
                // Get center
                let center = CGPoint(x: rect.midX, y: rect.midY)
                print(name!)
                self.hitTestQrCode(center: center , result: name ?? "default" )
                self.processing = false
            }

        }
    } else {
        self.processing = false
    }
}


func hitTestQrCode(center: CGPoint , result : String) {

    if let hitTestResults = self.scnView?.hitTest(center, types: [.featurePoint] ) , let camera = session.currentFrame?.camera, case .normal = camera.trackingState , let hittestResult = hitTestResults.first {

        if hittestResult.distance < 0.8 {
            // Add a new anchor to the session

            let statusForName = names.contains(where: { (name) -> Bool in
                name == result
            })
            if statusForName {
                print("Already Loaded")
            } else {


                let detectedAnchor = ARAnchor(transform: hittestResult.worldTransform)
                self.detectedDataAnchor = detectedAnchor
              self.detectedDataAnchor!.setValue(result, forKeyPath:"name")
                self.session.add(anchor: detectedAnchor)



                names.append(result)

            }

        }
    }
}



//Adding the node after the QR is detected

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
    let node = SCNNode()
    // If this is our anchor, create a node
    if self.detectedDataAnchor?.identifier == anchor.identifier {

        let name : String = self.detectedDataAnchor!.value(forKey: "name") as! String
        let plane = SCNPlane(width: 0.2, height: 0.2)
        plane.cornerRadius = plane.width / 8
        let skScene = SKScene(size: CGSize(width: 200, height: 200))
        skScene.backgroundColor = UIColor.white.withAlphaComponent(0.5)

        let rectangle = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 200, height: 200), cornerRadius: 10)
        rectangle.fillColor = #colorLiteral(red: 0.807843148708344, green: 0.0274509806185961, blue: 0.333333343267441, alpha: 1.0)
        rectangle.strokeColor = #colorLiteral(red: 0.439215689897537, green: 0.0117647061124444, blue: 0.192156866192818, alpha: 1.0)
        rectangle.lineWidth = 5
        rectangle.alpha = 0.4
        let labelNode = SKLabelNode(text: name)
        labelNode.fontSize = 30
        labelNode.run(SKAction.repeat(SKAction.rotate(byAngle: .pi, duration: 1), count: 1))
        labelNode.fontName = "San Fransisco"
        labelNode.position = CGPoint(x:100,y:100)
        skScene.addChild(rectangle)
        skScene.addChild(labelNode)
        let action = SCNAction.repeatForever(SCNAction.rotate(by: .pi, around: SCNVector3(0, 1, 0), duration: 3))
        plane.firstMaterial?.isDoubleSided = true
        plane.firstMaterial?.diffuse.contents = skScene

        let planeNode = SCNNode(geometry: plane)
         planeNode.runAction(action)
        print(planeNode.position)

        planeNode.physicsBody?.isAffectedByGravity = false
        node.addChildNode(planeNode)

    }
    return node
}

I am trying to find how can we handle multiple QR code detection with hitTest and adding a node on it

Prajwal Kc
  • 11
  • 2

0 Answers0