ios - 适用于 iOS 的 Firebase ML 套件 : "Unable to convert data to a string using the detected encoding" error when using local model

标签 ios swift firebase firebase-mlkit

我正在尝试使用 Firebase 的机器学习套件来标记图像。使用我的本地模型(而不是远程模型)时,我在尝试在本地模型下处理图像时收到错误消息。

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.}

我已经确认我的其余代码在使用远程模型时可以正常工作。

我还确认了 .tflite 模型文件、labels.txtmanifest.json 都正确地包含在 Bundle 中.我还在另一个 Firebase/Xcode 项目中使用了这个模型并且它在那里工作,尽管有很多繁琐的代码似乎不是 Firebase 推荐的标记图像的方式。

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

最佳答案

我已经解决了这个问题。有两个问题:第一个是我应该为 manifest.json 使用 Bundle 路径,而不是为 tflite 模型文件。所以而不是 让 localModel = LocalModel(名称:“mobilenet_v1_1.0_224_quant”,路径:Bundle.main.path(forResource:“mobilenet_v1_1.0_224_quant”,ofType:“tflite”)!) 我应该放 let localModel = LocalModel(name: "model", path: Bundle.main.path(forResource: "manifest", ofType: "json")!)

其次是我没有检查manifest.json文件的内容,modelFilelabelsFile字段也没有为该项目引用正确的文件。

关于ios - 适用于 iOS 的 Firebase ML 套件 : "Unable to convert data to a string using the detected encoding" error when using local model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57065663/

相关文章:

android - 我试图将我的 Android 应用程序连接到 google firebase,但它显示错误

ios - 创建 CoreData 关系导致代码中出现 20 多个错误

ios - 参数标签 '(_: RSKImageCropMode:)' 不匹配任何可用的重载

ios - 动态调整tableview的高度

ios - 如何将 content_available 添加到 Firebase Notification Composer

javascript - 使用 Angular 和 Firebase 存储日期

ios - 在 iPhone 中响应夏令时

ios - Text View 在 Placeholder Swift 中插入图像

ios - 从协议(protocol)中刷新数据

ios - segue 关闭时执行操作