开发者

C# 手写识别的实现示例

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

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

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

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

单个字的识别方案

     private List<string> Recognize(StrokeCollection strokes)
     {
         if (strokes == null || strokes.Count == 0)
             return null;
         // 创建识别器
         var recognizers = new Recognizers();
python         var chineseRecognizer = recognizers.GetDefaultRecognizer(0x0804);
         using var recContext = chineseRecognizer.CreateRecognizerContext();
         // 根据StrokeCollection构造 Ink 类型的笔迹数据。
       using var stream = new MemoryStream();
       strokes.Save(stream);
       using var inkStorage = new Ink();
       inkStorage.Load(stream.ToArray());
       using var inkStrokes = inkStorage.Strokes;
       //设置笔画数据
       using (recContext.Strokes = inkStrokes)
       {
           //识别笔画数据
           var recognitionResult = recContext.Recognize(out var statusResult);
           // 如果识别过程中出现问题,则返回null
           return statusResult == RecognitionStatus.NoError ?
               recognitionResult.GetAlternatesFromSelection().OfType<RecognitionAlternate>().Select(i => i.ToString()).ToList() :
               null;
       }
   }

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

var points = new StylusPointCollection();
    fwww.devze.comoreach (var stroke in strokes)
    {
        points.Add(new StylusPointCollection(stroke.StylusPoints));
    }
    var newStroke = new StrokeCollection
    {
        new Stroke(points)
    };

多字的识别方案

 public IEnumerable<string> Recognize(StrokeCollection strokes)
     {
         if (strokes == null || strokes.Count == 0)
             return null;
         using var analyzer = new InkAnalyzer();
         analyzer.AddStrokes(strokes,0x0804);
         analyzer.SetStrokesType(strokes, StrokeType.Writing);
         var status = analyzer.Analyze();
       if (status.Successful)
       {
           var alternateCollection = analyzer.GetAlternates();
           return alternateCollectioqkSwbVyn.OfType<AnalysisAlternate>().Select(x => x.RecognizedString);
       }
       return null;
   }
 

看下效果图

C# 手写识别的实现示例

C# 手写识别的实现示例

环境及DLL引用

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

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

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

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

引用了上面4个DLL文件后,还有2个环境问题:

  • 在App.config文件中,对节点startup添加属性 useLegacyV2RuntimeActivationPolicy="true"
  • 修改项目配置为x86

到此这篇关于C# 手写识别的实现示例的文章就介绍到这了,更多相关C# 手写识别内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜