如何为iOS AVFoundation定义自定义检测器?

时间:2016-09-01 06:01:32

标签: ios swift avfoundation ios-camera

我想用iPhone检测相机输入中的一些三角形图案。我找到了一些可以使用AVFoundation检测QR /条形码的示例代码。主要部分似乎是AVMetadataMachineReadableCodeObject类。以下是AppCoda的一些示例代码:

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {

// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects == nil || metadataObjects.count == 0 {
    qrCodeFrameView?.frame = CGRectZero
    messageLabel.text = "No barcode/QR code is detected"
    return
}

// Get the metadata object.
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

// Here we use filter method to check if the type of metadataObj is supported
// Instead of hardcoding the AVMetadataObjectTypeQRCode, we check if the type
// can be found in the array of supported bar codes.
if supportedBarCodes.contains(metadataObj.type) {
    // if metadataObj.type == AVMetadataObjectTypeQRCode {
    // If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
    let barCodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj)
    qrCodeFrameView?.frame = barCodeObject!.bounds

    if metadataObj.stringValue != nil {
        messageLabel.text = metadataObj.stringValue
    }
}

在上面的代码中,一旦检测到QR码,将绘制边界框并更新文本字段。同样,AVMetadataFaceObject类用于面部检测应用程序。我从引用中看到,这两个类都是AVMetadataObject的子类。

我想知道是否可以通过编写AVMetadataObject的子类来定制三角形检测器,比如说,我们调用子类AVMetadataTriangleObject。 (我有一个随时可用的检测算法,并且使用Matlab编写代码。将其转换为swift应该不会很难。)如果这种方法不可行,那么有人可以提出替代方法来实现上述目标吗?

非常感谢你!

1 个答案:

答案 0 :(得分:0)

AVMetadataObject没有提供用于实现此类内容的钩子,除了AVAssetResourceLoader之外,AVFoundation不允许进行太多扩展。

我认为您应该将您的算法转录为swift,然后针对使用CMSampleBuffer捕获时捕获的AVCaptureVideoDataOutput图像运行它。