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

MLKit Digital Ink Recognition throws error when initializing recognizer

我正在开发一个 Flutter 应用程序,它利用 Digital Ink Recognition of ML Kit 来识别手写内容。

我有以下代码可以对 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()方法后是否需要释放一些东西?我在 documentation.

中找不到与此相关的任何内容

提前感谢任何想法。

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

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

这个example was helpful to understand how to work with the ModelManager (see implementation of the StrokeManager).