Kevin2566
Kevin2566

Reputation: 451

Firebase ML Kit for iOS: "Unable to convert data to a string using the detected encoding" error when using local model

I'm trying to use Firebase's ML Kit to label images. When using my local model (instead of a remote model), I get an error message when trying to process my images under the local model.

Firebase/MLKit][I-MLK017001] Error in parsing model manifest file (/Users/cameron.hamidi/Library/Developer/CoreSimulator/Devices/153A2576-4171-4DB2-9F0C-56A184E35EBF/data/Containers/Bundle/Application/61817D0B-3B80-4FE3-BAE4-2EE5077938B0/TestVision.app/mobilenet_v1_1.0_224_quant.tflite): Error Domain=NSCocoaErrorDomain Code=3840 "Unable to convert data to a string using the detected encoding. The data may be corrupt." UserInfo={NSDebugDescription=Unable to convert data to a string using the detected encoding. The data may be corrupt.}

I have confirmed that the rest of my code works when using a remote model.

I've also confirmed that the .tflite model file, labels.txt, and manifest.json are all correctly in the Bundle. I've also used this model on a different Firebase/Xcode project and it worked there, albeit with a lot of cumbersome code that doesn't seem to be Firebase's recommended way to label images.

let initialConditions = ModelDownloadConditions(allowsCellularAccess: true,
                                                       allowsBackgroundDownloading: true)
        let updateConditions = ModelDownloadConditions(allowsCellularAccess: false,
                                                       allowsBackgroundDownloading: true)

        let localModel = LocalModel(name: "mobilenet_v1_1.0_224_quant", path: Bundle.main.path(forResource: "mobilenet_v1_1.0_224_quant", ofType: "tflite")!)
        ModelManager.modelManager().register(localModel)

        let images = getVisionImages() //Returns an array of UIImages to label
        let labelerOptions = VisionOnDeviceAutoMLImageLabelerOptions(remoteModelName: nil, localModelName: "mobilenet_v1_1.0_224_quant"
        )
        labelerOptions.confidenceThreshold = 0
        let labeler = Vision.vision().onDeviceAutoMLImageLabeler(options: labelerOptions) //this line is where I get the error

        images.forEach() { image in
            labeler.process(image) { labels, error in
                guard error == nil, let labels = labels else { return }
                print("\nnew image")
                labels.forEach() { label in
                    print("\n")
                    print(label.text)
                    print(label.entityID)
                    print(label.confidence)
                }
            }
}

Upvotes: 0

Views: 361

Answers (1)

Kevin2566
Kevin2566

Reputation: 451

I've fixed the issue. There were two problems: the first was that I should have used the Bundle path for the manifest.json, not for the tflite model file. So instead of let localModel = LocalModel(name: "mobilenet_v1_1.0_224_quant", path: Bundle.main.path(forResource: "mobilenet_v1_1.0_224_quant", ofType: "tflite")!) I should have put let localModel = LocalModel(name: "model", path: Bundle.main.path(forResource: "manifest", ofType: "json")!)

The second was that I didn't check the contents of the manifest.json file, and the modelFile and labelsFile fields did not reference the correct files for this project.

Upvotes: 1

Related Questions