【问题标题】:Issues with Google MLKit Object Detection on iOSiOS 上的 Google MLKit 对象检测问题
【发布时间】:2021-05-27 11:30:55
【问题描述】:

我目前正在尝试从https://developers.google.com/ml-kit/vision/object-detection/ios跟随开发者将MLKit与iOS集成

当打开相机并开始检测物体时,出现运行时错误:

    Precondition failed: NSArray element failed to match the Swift Array Element type 
    Expected MLKObject but found MLKObject: file Swift/ArrayBuffer.swift, line 354

如果我在本地模型中使用基本对象检测器而不是自定义对象检测器,则会出现同样的问题。

这是我用于设置和检测的内容:

1.

        let options = ObjectDetectorOptions()
        objectDetector = ObjectDetector.objectDetector(options: options)
        guard let modelPath = Bundle.main.path(forResource: "mobilenet_v1_0.25_128_quantized_1_metadata_1", ofType: "tflite") else {
            return
        }
        let localModel = LocalModel(path: modelPath)
        let options = CustomObjectDetectorOptions(localModel: localModel)
        objectDetector = ObjectDetector.objectDetector(options: options)
    func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {

        let image = VisionImage(buffer: sampleBuffer)
        image.orientation = imageOrientation()
        objectDetector?.process(image) { objects, error in
            guard error == nil, let detectedObjects = objects else { return }

            for object in detectedObjects { //error occurs here
              let frame = object.frame
              let trackingID = object.trackingID

              let description = object.labels.enumerated().map { (index, label) in
                "Label \(index): \(label.text), \(label.confidence)"
                }.joined(separator:"\n")

            }
        }
    }

【问题讨论】:

    标签: ios swift google-mlkit


    【解决方案1】:

    看起来像是一个 swift 语法问题(可能是由于最近的 xcode 升级?),我在 fatal error: NSArray element failed to match the Swift Array Element type 找到了类似的问题/答案。您可能想通过在声明中显式添加类类型来尝试一下(在本例中为 MLKObject)。 随时提供更多上下文以帮助进行故障排除。 :-)

    【讨论】:

      猜你喜欢
      • 2020-10-17
      • 2023-03-25
      • 2020-10-18
      • 2021-03-20
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多