0

I am currently trying to follow the developer to integrate MLKit with iOS from https://developers.google.com/ml-kit/vision/object-detection/ios

When open up the camera and start detecting objects, I'm getting a runtime error:

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

Same issue occurs if I use the base objector detector instead of a custom object detector with the local model.

Here is what I have for setting up and detecting:

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")

            }
        }
    }
TAP
  • 1

1 Answers1

0

Looks like a swift syntax issue(maybe due to recent xcode upgrade?), I found similar question/answer at fatal error: NSArray element failed to match the Swift Array Element type. You may want to give it a try by explicitly add class types in declaration (in this case it is MLKObject). Feel free to provide more context to help troubleshooting. :-)

vedopar
  • 56
  • 1