swift - ARKit 条形码跟踪和视觉框架

标签 swift barcode arkit apple-vision

我一直在尝试为 ARSession 期间检测到的 QR 码绘制边界框。结果是: boundingbox 1 boundingbox 2

正在跟踪条形码,但边界框的几何形状是错误的。

如何获取正确的边界框坐标?

源代码是:

 public func session(_ session: ARSession, didUpdate frame: ARFrame) {

     // Only run one Vision request at a time
     if self.processing {
         return
     }

    self.processing = true

    let request = VNDetectBarcodesRequest { (request, error) in

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

            DispatchQueue.main.async {

                let path = CGMutablePath()

                for result in results {
                    guard let barcode = result as? VNBarcodeObservation else { continue }
                    let topLeft = self.convert(point: barcode.topLeft)
                    path.move(to: topLeft)
                    let topRight = self.convert(point: barcode.topRight)
                    path.addLine(to: topRight)
                    let bottomRight = self.convert(point: barcode.bottomRight)
                    path.addLine(to: bottomRight)
                    let bottomLeft = self.convert(point: barcode.bottomLeft)
                    path.addLine(to: bottomLeft)
                    path.addLine(to: topLeft)
                }                   
                self.drawLayer.path = path
                self.processing = false
            }
        } else {
            self.processing = false
        }
    }

    DispatchQueue.global(qos: .userInitiated).async {
        do {
            request.symbologies = [.QR]
            let imageRequestHandler = VNImageRequestHandler(cvPixelBuffer: frame.capturedImage, orientation: .right, options: [:])                
            try imageRequestHandler.perform([request])
        } catch {               
        }
    }
}

 private func convert(point: CGPoint) -> CGPoint {
     return CGPoint(x: point.x * view.bounds.size.width,
                   y: (1 - point.y) * view.bounds.size.height)
 }

最佳答案

我刚刚将我的应用程序中的条形码识别从 AVFoundation 迁移到 Vision,以下是对我有用的概述逻辑:

extension CVPixelBuffer {
    var size: CGSize {
        get {
            let width = CGFloat(CVPixelBufferGetWidth(self))
            let height = CGFloat(CVPixelBufferGetHeight(self))
            return CGSize(width: width, height: height)
        }
    }
}
extension VNRectangleObservation {    
    func outline(in cvPixelBuffer: CVPixelBuffer, with color: UIColor) -> CALayer {
        let outline = CAShapeLayer()
        outline.path = self.path(in: cvPixelBuffer).cgPath
        outline.fillColor = UIColor.clear.cgColor
        outline.strokeColor =  color.cgColor
        return outline
    }
    
    func path(in cvPixelBuffer: CVPixelBuffer) -> UIBezierPath {
        let size = cvPixelBuffer.size
        let transform = CGAffineTransform.identity
            .scaledBy(x: 1, y: -1)
            .translatedBy(x: 0, y: -size.height)
            .scaledBy(x: size.width, y: size.height)
        
        let convertedTopLeft = self.topLeft.applying(transform)
        let convertedTopRight = self.topRight.applying(transform)
        let convertedBottomLeft = self.bottomLeft.applying(transform)
        let convertedBottomRight = self.bottomRight.applying(transform)
        
        let path = UIBezierPath()
        path.move(to: convertedTopLeft)
        path.addLine(to: convertedTopRight)
        path.addLine(to: convertedBottomRight)
        path.addLine(to: convertedBottomLeft)
        path.close()
        
        path.lineWidth = 2.0
        return path
    }
}

之后,我再应用一次缩放变换以适应显示轮廓的 View 的大小。

我正在使用 https://github.com/maxvol/RxVision库使得沿线传递处理后的图像(在我的例子中是 CVPixelBuffer)变得简单。

关于swift - ARKit 条形码跟踪和视觉框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52040397/

相关文章:

ios - 在 Alamofire 中设置超时

ios - AVAudioRecorder最小的音频类型是什么

ios - swift - 在 Controller 之间切换后更新计时器并更新标签

java - 是否有任何数据库/或网络服务或链接,我们可以在其中搜索 GS1 - 128 代码并查找来自不同商店的产品信息?

ios - 如何使 SCNNode 面向 ARAnchor

ios - 如何将相机坐标转换为场景坐标空间?

ios - 为什么我们总是必须在 HitTest 中使用集合的第一个元素,而不是最后一个?

ios - 在 BluetoothManager Delegate 类中访问 ViewController 按钮

ios - 在 iOS 应用程序中生成条形码

printing - 从网页将条形码打印到Zebra打印机