首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ARKit条形码跟踪和视觉框架

ARKit条形码跟踪和视觉框架
EN

Stack Overflow用户
提问于 2018-08-27 21:21:44
回答 1查看 1.6K关注 0票数 5

我一直在尝试为ARSession过程中检测到的二维码绘制边界框。结果是:boundingbox 1 boundingbox 2

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

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

源码是:

代码语言:javascript
复制
 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)
 }
EN

回答 1

Stack Overflow用户

发布于 2018-10-18 11:25:11

我刚刚将我的应用程序中的条形码识别从AVFoundation迁移到了Vision,下面是我的大纲逻辑:

代码语言:javascript
复制
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
    }
}

之后,我再应用一个缩放转换,以适应显示轮廓的视图的大小。

我使用的是https://github.com/maxvol/RxVision库,它可以简单地传递经过处理的图像(在我的例子中是CVPixelBuffer)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52040397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档