MLKit数字墨水识别在初始化识别器时抛出错误



我正在开发一个Flutter应用程序,它使用ML工具包的数字墨水识别来识别手写。

我有以下代码在iOS上执行识别(swift代码)。

let ink = Ink.init(strokes: mlStrokes)
let identifier = DigitalInkRecognitionModelIdentifier(forLanguageTag: languageTag)
if identifier == nil {
flutterResult(FlutterError(code: "ERROR", message: "No model for language tag found or the language tag could not be parsed", details: nil))
}
let model = DigitalInkRecognitionModel.init(modelIdentifier: identifier!)
// Get a recognizer for the given language
let options: DigitalInkRecognizerOptions = DigitalInkRecognizerOptions.init(model: model)
digitalInkRecognizer = DigitalInkRecognizer.digitalInkRecognizer(options: options)
digitalInkRecognizer.recognize(
ink: ink,
completion: {
(result: DigitalInkRecognitionResult?, error: Error?) in
if let result = result {
var flutterCandidates : [[String: Any?]] = []
for candidate in result.candidates {
flutterCandidates.append(
["text" : candidate.text, "score" : candidate.score]
)
}
// Return recognised candidates to Flutter.
self.flutterResult(flutterCandidates)
} else {
self.flutterResult(FlutterError(code: "ERROR", message: "Unable to recognize handwriting", details: error))
}
})

我一次只能识别一个字母。

这在一段时间内工作得很好,在完成回调中返回正确的字母,然后发送回我的Flutter应用程序,但在调用该方法几次(~20次)后,在控制台上抛出以下错误,我的应用程序崩溃了。

I1025 10:03:01.607888       1 lstm_recognizer.cc:77] Loaded tflite model.
2021-10-25 10:03:01.621992+0200 Runner[796:125890] Unsupported value: Error Domain=com.google.mlkit Code=13 "Unable to initialize recognizer: generic::internal: Unable to mmap file descriptor (Cannot allocate memory). (research/handwriting/util/mmap_data.cc:49)" UserInfo={NSLocalizedDescription=Unable to initialize recognizer: generic::internal: Unable to mmap file descriptor (Cannot allocate memory). (research/handwriting/util/mmap_data.cc:49)} of type NSError
2021-10-25 10:03:01.623350+0200 Runner[796:125890] *** Assertion failure in -[FlutterStandardWriter writeValue:], FlutterStandardCodec.mm:334
2021-10-25 10:03:01.632824+0200 Runner[796:125890] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unsupported value for standard codec'
*** First throw call stack:
(0x20ff2e180 0x20f1069f8 0x20fe4788c 0x21090bb38 0x1034b8138 0x1034b8898 0x1034b5890 0x100c4f0f4 0x100c522fc 0x100c523a8 0x1052536f4 0x105254c78 0x1052626fc 0x20febfb20 0x20febaa58 0x20feb9fb4 0x2120bb79c 0x23c197c38 0x100c4f464 0x20f97d8e0)
libc++abi.dylib: terminating with uncaught exception of type NSException

这个错误似乎与内存问题(Unsupported value: Error Domain=com.google.mlkit Code=13 "Unable to initialize recognizer: generic::internal: Unable to mmap file descriptor (Cannot allocate memory))有关,但我找不到原因。

我必须在digitalInkRecognizer.recognize()方法被调用后释放一些东西吗?我在文档中找不到任何与此相关的内容。

谢谢你的建议。

我发现这个问题与ModelManager和识别器的生命周期管理有关,我每次执行数字墨水识别时都要重新创建它们。

更改此行为并在创建后保留对它们的引用后,问题就解决了。

这个例子有助于理解如何使用ModelManager(参见StrokeManager的实现)。

相关内容

  • 没有找到相关文章

最新更新