0

C# 手写识别方案整理

 1 year ago
source link: https://www.cnblogs.com/kybs0/p/17361589.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

C# 手写识别方案整理

书写识别,网上的大佬们都有输出。

书写识别存在的2个问题:

  1. 直接拿官网的案例(将 Windows Ink 笔划识别为文本和形状 - Windows apps | Microsoft Learn),会发现输出准确度不高。
  2. 另外如果书写过快,词组识别也是个问题,毕竟无法准确分割字之间的笔迹。

我结合之前开发经验,整理下书写识别比较完善的方案。

单个字的识别方案:

 1     private List<string> Recognize(StrokeCollection strokes)
 2     {
 3         if (strokes == null || strokes.Count == 0)
 4             return null;
 5         // 创建识别器
 6         var recognizers = new Recognizers();
 7         var chineseRecognizer = recognizers.GetDefaultRecognizer(0x0804);
 8         using var recContext = chineseRecognizer.CreateRecognizerContext();
 9         // 根据StrokeCollection构造 Ink 类型的笔迹数据。
10         using var stream = new MemoryStream();
11         strokes.Save(stream);
12         using var inkStorage = new Ink();
13         inkStorage.Load(stream.ToArray());
14         using var inkStrokes = inkStorage.Strokes;
15         //设置笔画数据
16         using (recContext.Strokes = inkStrokes)
17         {
18             //识别笔画数据
19             var recognitionResult = recContext.Recognize(out var statusResult);
20             // 如果识别过程中出现问题,则返回null
21             return statusResult == RecognitionStatus.NoError ?
22                 recognitionResult.GetAlternatesFromSelection().OfType<RecognitionAlternate>().Select(i => i.ToString()).ToList() :
23                 null;
24         }
25     }

这里单字识别,想要提高识别率,可以将stroke合并成一个:

1     var points = new StylusPointCollection();
2     foreach (var stroke in strokes)
3     {
4         points.Add(new StylusPointCollection(stroke.StylusPoints));
5     }
6     var newStroke = new StrokeCollection
7     {
8         new Stroke(points)
9     };

多字的识别方案:

 1     public IEnumerable<string> Recognize(StrokeCollection strokes)
 2     {
 3         if (strokes == null || strokes.Count == 0)
 4             return null;
 5 
 6         using var analyzer = new InkAnalyzer();
 7         analyzer.AddStrokes(strokes,0x0804);
 8         analyzer.SetStrokesType(strokes, StrokeType.Writing);
 9         var status = analyzer.Analyze();
10         if (status.Successful)
11         {
12             var alternateCollection = analyzer.GetAlternates();
13             return alternateCollection.OfType<AnalysisAlternate>().Select(x => x.RecognizedString);
14         }
15         return null;
16     }

看下效果图

685541-20230428110928412-292123197.png
685541-20230428111001302-2054232069.png

 引用的命名空间是:Windows.Ink和MicroSoft.Ink,需要引用的DLL文件有四个。

IACore.dll、IALoader.dll、IAWinFX.dll,这三个DLL文件都是Intel集成显卡驱动的重要组成部分,包含了图形处理模块,尤其是IAWinFX为WPF应用提供了支持硬件加速的图形渲染。

以及Microsoft.Ink.dll

值得说明一下,Windows.Ink与Microsoft.Ink在平台支持上不同,如果有要适配不同版本的windows,需要去上方代码修改下

  • Microsoft.Ink支持Windows XP、Vista 和 Win7 等旧版 Windows,兼容性高。但Win10及以上版本,官方推荐使用Windows.Ink

  • Windows.Ink,则仅支持Win8以上版本

DLL引用环境遇到问题,可以参考这篇博客 【WPF】提高InkAnalyer手写汉字识别的准确率 - 大佛脚下 - 博客园 (cnblogs.com)

参考博客列表:

C# vs2012 如何实现手写识别?快来看看吧~_手写识别控件_想吃米豆腐的博客-CSDN博客

C# 实现手写输入功能_zls365365的博客-CSDN博客

【WPF】提高InkAnalyer手写汉字识别的准确率 - 大佛脚下 - 博客园 (cnblogs.com)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK