我正在尝试使用 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.txt
和 manifest.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
文件的内容,modelFile
和labelsFile
字段也没有为该项目引用正确的文件。
关于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/