在 iOS 系统中使用机器学习套件识别数字手写内容

借助机器学习套件的数字手写识别功能,您可以识别数字平面上数百种语言的手写文本,还可以对草图进行分类。

试试看

准备工作

  1. 在 Podfile 中添加以下机器学习套件库:

    pod 'GoogleMLKit/DigitalInkRecognition', '7.0.0'
    
    
  2. 安装或更新项目的 Pod 之后,请使用 Xcode 项目的 .xcworkspace 来打开项目。Xcode 13.2.1 版或更高版本支持机器学习套件。

现在,您可以开始识别 Ink 对象中的文本了。

构建 Ink 对象

构建 Ink 对象的主要方法是在触摸屏上绘制它。在 iOS 上,您可以将 UIImageView轻触事件处理脚本搭配使用,这些脚本会在屏幕上绘制笔触,并存储笔触的点以构建 Ink 对象。以下代码段演示了这种常规模式。如需查看更完整的示例,请参阅快速入门应用,其中将触摸事件处理、屏幕绘制和笔触数据管理分离开来。

Swift

@IBOutlet weak var mainImageView: UIImageView!
var kMillisecondsPerTimeInterval = 1000.0
var lastPoint = CGPoint.zero
private var strokes: [Stroke] = []
private var points: [StrokePoint] = []

func drawLine(from fromPoint: CGPoint, to toPoint: CGPoint) {
  UIGraphicsBeginImageContext(view.frame.size)
  guard let context = UIGraphicsGetCurrentContext() else {
    return
  }
  mainImageView.image?.draw(in: view.bounds)
  context.move(to: fromPoint)
  context.addLine(to: toPoint)
  context.setLineCap(.round)
  context.setBlendMode(.normal)
  context.setLineWidth(10.0)
  context.setStrokeColor(UIColor.white.cgColor)
  context.strokePath()
  mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()
  mainImageView.alpha = 1.0
  UIGraphicsEndImageContext()
}

override func touchesBegan(_ touches: Set, with event: UIEvent?) {
  guard let touch = touches.first else {
    return
  }
  lastPoint = touch.location(in: mainImageView)
  let t = touch.timestamp
  points = [StrokePoint.init(x: Float(lastPoint.x),
                             y: Float(lastPoint.y),
                             t: Int(t * kMillisecondsPerTimeInterval))]
  drawLine(from:lastPoint, to:lastPoint)
}

override func touchesMoved(_ touches: Set, with event: UIEvent?) {
  guard let touch = touches.first else {
    return
  }
  let currentPoint = touch.location(in: mainImageView)
  let t = touch.timestamp
  points.append(StrokePoint.init(x: Float(currentPoint.x),
                                 y: Float(currentPoint.y),
                                 t: Int(t * kMillisecondsPerTimeInterval)))
  drawLine(from: lastPoint, to: currentPoint)
  lastPoint = currentPoint
}

override func touchesEnded(_ touches: Set, with event: UIEvent?) {
  guard let touch = touches.first else {
    return
  }
  let currentPoint = touch.location(in: mainImageView)
  let t = touch.timestamp
  points.append(StrokePoint.init(x: Float(currentPoint.x),
                                 y: Float(currentPoint.y),
                                 t: Int(t * kMillisecondsPerTimeInterval)))
  drawLine(from: lastPoint, to: currentPoint)
  lastPoint = currentPoint
  strokes.append(Stroke.init(points: points))
  self.points = []
  doRecognition()
}

Objective-C

// Interface
@property (weak, nonatomic) IBOutlet UIImageView *mainImageView;
@property(nonatomic) CGPoint lastPoint;
@property(nonatomic) NSMutableArray *strokes;
@property(nonatomic) NSMutableArray *points;

// Implementations
static const double kMillisecondsPerTimeInterval = 1000.0;

- (void)drawLineFrom:(CGPoint)fromPoint to:(CGPoint)toPoint {
  UIGraphicsBeginImageContext(self.mainImageView.frame.size);
  [self.mainImageView.image drawInRect:CGRectMake(0, 0, self.mainImageView.frame.size.width,
                                                  self.mainImageView.frame.size.height)];
  CGContextMoveToPoint(UIGraphicsGetCurrentContext(), fromPoint.x, fromPoint.y);
  CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), toPoint.x, toPoint.y);
  CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
  CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
  CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1, 1, 1, 1);
  CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);
  CGContextStrokePath(UIGraphicsGetCurrentContext());
  CGContextFlush(UIGraphicsGetCurrentContext());
  self.mainImageView.image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
}

- (void)touchesBegan:(NSSet *)touches withEvent:(nullable UIEvent *)event {
  UITouch *touch = [touches anyObject];
  self.lastPoint = [touch locationInView:self.mainImageView];
  NSTimeInterval time = [touch timestamp];
  self.points = [NSMutableArray array];
  [self.points addObject:[[MLKStrokePoint alloc] initWithX:self.lastPoint.x
                                                         y:self.lastPoint.y
                                                         t:time * kMillisecondsPerTimeInterval]];
  [self drawLineFrom:self.lastPoint to:self.lastPoint];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(nullable UIEvent *)event {
  UITouch *touch = [touches anyObject];
  CGPoint currentPoint = [touch locationInView:self.mainImageView];
  NSTimeInterval time = [touch timestamp];
  [self.points addObject:[[MLKStrokePoint alloc] initWithX:currentPoint.x
                                                         y:currentPoint.y
                                                         t:time * kMillisecondsPerTimeInterval]];
  [self drawLineFrom:self.lastPoint to:currentPoint];
  self.lastPoint = currentPoint;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(nullable UIEvent *)event {
  UITouch *touch = [touches anyObject];
  CGPoint currentPoint = [touch locationInView:self.mainImageView];
  NSTimeInterval time = [touch timestamp];
  [self.points addObject:[[MLKStrokePoint alloc] initWithX:currentPoint.x
                                                         y:currentPoint.y
                                                         t:time * kMillisecondsPerTimeInterval]];
  [self drawLineFrom:self.lastPoint to:currentPoint];
  self.lastPoint = currentPoint;
  if (self.strokes == nil) {
    self.strokes = [NSMutableArray array];
  }
  [self.strokes addObject:[[MLKStroke alloc] initWithPoints:self.points]];
  self.points = nil;
  [self doRecognition];
}

请注意,该代码段包含一个示例函数,用于将笔触绘制到 UIImageView 中,您应根据需要对其进行调整,使其适用于您的应用。我们建议您在绘制线段时使用圆角,以便将长度为零的线段绘制为圆点(想象一下小写字母 i 上的圆点)。系统会在写入每条笔画后调用 doRecognition() 函数,该函数将在下文中进行定义。

获取 DigitalInkRecognizer 的实例

如需执行识别,我们需要将 Ink 对象传递给 DigitalInkRecognizer 实例。如需获取 DigitalInkRecognizer 实例,我们首先需要下载所需语言的识别器模型,并将该模型加载到 RAM 中。您可以使用以下代码段实现此目的,为简单起见,该代码段会放置在 viewDidLoad() 方法中,并使用硬编码的语言名称。如需查看如何向用户显示可用语言列表并下载所选语言的示例,请参阅快速入门应用

Swift

override func viewDidLoad() {
  super.viewDidLoad()
  let languageTag = "en-US"
  let identifier = DigitalInkRecognitionModelIdentifier(forLanguageTag: languageTag)
  if identifier == nil {
    // no model was found or the language tag couldn't be parsed, handle error.
  }
  let model = DigitalInkRecognitionModel.init(modelIdentifier: identifier!)
  let modelManager = ModelManager.modelManager()
  let conditions = ModelDownloadConditions.init(allowsCellularAccess: true,
                                         allowsBackgroundDownloading: true)
  modelManager.download(model, conditions: conditions)
  // Get a recognizer for the language
  let options: DigitalInkRecognizerOptions = DigitalInkRecognizerOptions.init(model: model)
  recognizer = DigitalInkRecognizer.digitalInkRecognizer(options: options)
}

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];
  NSString *languagetag = @"en-US";
  MLKDigitalInkRecognitionModelIdentifier *identifier =
      [MLKDigitalInkRecognitionModelIdentifier modelIdentifierForLanguageTag:languagetag];
  if (identifier == nil) {
    // no model was found or the language tag couldn't be parsed, handle error.
  }
  MLKDigitalInkRecognitionModel *model = [[MLKDigitalInkRecognitionModel alloc]
                                          initWithModelIdentifier:identifier];
  MLKModelManager *modelManager = [MLKModelManager modelManager];
  [modelManager downloadModel:model conditions:[[MLKModelDownloadConditions alloc]
                                                initWithAllowsCellularAccess:YES
                                                allowsBackgroundDownloading:YES]];
  MLKDigitalInkRecognizerOptions *options =
      [[MLKDigitalInkRecognizerOptions alloc] initWithModel:model];
  self.recognizer = [MLKDigitalInkRecognizer digitalInkRecognizerWithOptions:options];
}

快速入门应用包含其他代码,这些代码展示了如何同时处理多个下载,以及如何通过处理完成通知来确定哪个下载成功。

识别 Ink 对象

接下来,我们来看看 doRecognition() 函数,为简单起见,我们从 touchesEnded() 调用该函数。在其他应用中,您可能希望仅在超时后或用户按下按钮触发识别时调用识别功能。

Swift

func doRecognition() {
  let ink = Ink.init(strokes: strokes)
  recognizer.recognize(
    ink: ink,
    completion: {
      [unowned self]
      (result: DigitalInkRecognitionResult?, error: Error?) in
      var alertTitle = ""
      var alertText = ""
      if let result = result, let candidate = result.candidates.first {
        alertTitle = "I recognized this:"
        alertText = candidate.text
      } else {
        alertTitle = "I hit an error:"
        alertText = error!.localizedDescription
      }
      let alert = UIAlertController(title: alertTitle,
                                  message: alertText,
                           preferredStyle: UIAlertController.Style.alert)
      alert.addAction(UIAlertAction(title: "OK",
                                    style: UIAlertAction.Style.default,
                                  handler: nil))
      self.present(alert, animated: true, completion: nil)
    }
  )
}

Objective-C

- (void)doRecognition {
  MLKInk *ink = [[MLKInk alloc] initWithStrokes:self.strokes];
  __weak typeof(self) weakSelf = self;
  [self.recognizer
      recognizeInk:ink
        completion:^(MLKDigitalInkRecognitionResult *_Nullable result,
                     NSError *_Nullable error) {
    typeof(weakSelf) strongSelf = weakSelf;
    if (strongSelf == nil) {
      return;
    }
    NSString *alertTitle = nil;
    NSString *alertText = nil;
    if (result.candidates.count > 0) {
      alertTitle = @"I recognized this:";
      alertText = result.candidates[0].text;
    } else {
      alertTitle = @"I hit an error:";
      alertText = [error localizedDescription];
    }
    UIAlertController *alert =
        [UIAlertController alertControllerWithTitle:alertTitle
                                            message:alertText
                                     preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"OK"
                                              style:UIAlertActionStyleDefault
                                            handler:nil]];
    [strongSelf presentViewController:alert animated:YES completion:nil];
  }];
}

管理模型下载

我们已经了解了如何下载识别模型。以下代码段展示了如何检查模型是否已下载,或如何在不再需要模型时删除模型以恢复存储空间。

检查模型是否已下载

Swift

let model : DigitalInkRecognitionModel = ...
let modelManager = ModelManager.modelManager()
modelManager.isModelDownloaded(model)

Objective-C

MLKDigitalInkRecognitionModel *model = ...;
MLKModelManager *modelManager = [MLKModelManager modelManager];
[modelManager isModelDownloaded:model];

删除下载的模型

Swift

let model : DigitalInkRecognitionModel = ...
let modelManager = ModelManager.modelManager()

if modelManager.isModelDownloaded(model) {
  modelManager.deleteDownloadedModel(
    model!,
    completion: {
      error in
      if error != nil {
        // Handle error
        return
      }
      NSLog(@"Model deleted.");
    })
}

Objective-C

MLKDigitalInkRecognitionModel *model = ...;
MLKModelManager *modelManager = [MLKModelManager modelManager];

if ([self.modelManager isModelDownloaded:model]) {
  [self.modelManager deleteDownloadedModel:model
                                completion:^(NSError *_Nullable error) {
                                  if (error) {
                                    // Handle error.
                                    return;
                                  }
                                  NSLog(@"Model deleted.");
                                }];
}

提高文字识别准确度的提示

文本识别准确性因语言而异。准确性还取决于写作风格。虽然数字手写识别功能经过训练可处理多种手写风格,但结果可能会因用户而异。

以下是提高文本识别器准确性的一些方法。请注意,这些技术不适用于表情符号、AutoDraw 和形状的绘图分类器。

手写区域

许多应用都有明确定义的书写区域,供用户输入内容。符号的含义在一定程度上取决于其相对于包含它的书写区域的大小。例如,小写字母“o”或“c”与大写字母“O”或“C”之间的区别,以及英文逗号与正斜线之间的区别。

告知识别器手写区域的宽度和高度有助于提高准确性。不过,识别器假定手写区域只包含一行文本。如果实际手写区域足够大,可以让用户写两行或更多行,那么您可以传入一个 WritingArea,并将其高度设为您对单行文本高度的最佳估算值,以获得更好的结果。您传递给识别器的 WritingArea 对象不必与屏幕上的实际手写区域完全对应。以这种方式更改 WritingArea 高度在某些语言中比在其他语言中效果更好。

指定书写区域时,请使用与笔触坐标相同的单位指定其宽度和高度。x,y 坐标参数没有单位要求 - API 会对所有单位进行标准化,因此唯一重要的是笔触的相对大小和位置。您可以随意以对您的系统有意义的任何比例传入坐标。

前情境

前文本是指您尝试识别的 Ink 中的笔画前面的文本。您可以通过告知识别器前情境来帮助识别器。

例如,手写体字母“n”和“u”经常会被误认为是同一个字母。如果用户已输入部分字词“arg”,则可能会继续输入可识别为“ument”或“nment”的笔画。指定前情境“arg”可解析歧义,因为“argument”一词比“argnment”更可能。

前文还可以帮助识别程序识别单词间隔符和单词之间的空格。您可以输入空格字符,但无法绘制空格,那么识别器如何确定一个字词的结束和下一个字词的开始?如果用户已写出“hello”,并继续写出“world”,那么在没有前文的情况下,识别器会返回字符串“world”。但是,如果您指定前情境“hello”,模型将返回带有前导空格的字符串“world”,因为“hello world”比“helloword”更有意义。

您应提供尽可能长的上下文前字符串,最多 20 个字符(包括空格)。如果字符串更长,则识别器只会使用最后 20 个字符。

以下代码示例展示了如何定义写入区域,以及如何使用 RecognitionContext 对象指定前情境。

Swift

let ink: Ink = ...;
let recognizer: DigitalInkRecognizer =  ...;
let preContext: String = ...;
let writingArea = WritingArea.init(width: ..., height: ...);

let context: DigitalInkRecognitionContext.init(
    preContext: preContext,
    writingArea: writingArea);

recognizer.recognizeHandwriting(
  from: ink,
  context: context,
  completion: {
    (result: DigitalInkRecognitionResult?, error: Error?) in
    if let result = result, let candidate = result.candidates.first {
      NSLog("Recognized \(candidate.text)")
    } else {
      NSLog("Recognition error \(error)")
    }
  })

Objective-C

MLKInk *ink = ...;
MLKDigitalInkRecognizer *recognizer = ...;
NSString *preContext = ...;
MLKWritingArea *writingArea = [MLKWritingArea initWithWidth:...
                                              height:...];

MLKDigitalInkRecognitionContext *context = [MLKDigitalInkRecognitionContext
       initWithPreContext:preContext
       writingArea:writingArea];

[recognizer recognizeHandwritingFromInk:ink
            context:context
            completion:^(MLKDigitalInkRecognitionResult
                         *_Nullable result, NSError *_Nullable error) {
                               NSLog(@"Recognition result %@",
                                     result.candidates[0].text);
                         }];

笔画排序

识别准确性对笔画顺序非常敏感。识别器希望笔画的顺序与人们自然书写的顺序一致;例如,英文从左到右。任何偏离此模式的情况(例如从最后一个字词开始写英语句子)都会导致结果不准确。

再举一个例子,如果移除 Ink 中间的某个字词并将其替换为其他字词,修改内容可能位于句子中间,但修改内容的笔画位于笔画序列的末尾。在这种情况下,我们建议您将新写的字单独发送给 API,并使用您自己的逻辑将结果与之前的识别结果合并。

处理模糊的形状

在某些情况下,向识别器提供的形状的含义不明确。例如,边缘非常圆润的矩形可以被视为矩形或椭圆形。

在可用的情况下,可以使用识别得分来处理这些不确定的情况。只有形状分类器会提供得分。如果模型非常自信,则首个结果的分数将远远高于第二个结果的分数。如果存在不确定性,前两项结果的得分将会接近。此外,请注意,形状分类器会将整个 Ink 解读为单个形状。例如,如果 Ink 包含彼此相邻的矩形和椭圆形,则识别器可能会返回其中一个(或完全不同的东西),因为单个识别候选项无法代表两个形状。