ARKit条形码跟踪和视觉框架

时间:2018-08-27 13:21:44

标签: 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)
 }

1 个答案:

答案 0 :(得分:0)

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

{{1}}

在那之后,我又应用了一个比例变换来适应显示轮廓的视图的大小。

我正在使用https://github.com/maxvol/RxVision库,它使沿线传递处理后的图像(在我的情况下为CVPixelBuffer)变得很简单。