C#?手寫識別的實現(xiàn)示例
書寫識別,網(wǎng)上的大佬們都有輸出。
書寫識別存在的2個問題:
- 直接拿官網(wǎng)的案例(將 Windows Ink 筆劃識別為文本和形狀 - Windows apps | Microsoft Learn),會發(fā)現(xiàn)輸出準確度不高。
- 另外如果書寫過快,詞組識別也是個問題,畢竟無法準確分割字之間的筆跡。
我結(jié)合之前開發(fā)經(jīng)驗,整理下書寫識別比較完善的方案。
單個字的識別方案
private List<string> Recognize(StrokeCollection strokes) { if (strokes == null || strokes.Count == 0) return null; // 創(chuàng)建識別器 var recognizers = new Recognizers(); var chineseRecognizer = recognizers.GetDefaultRecognizer(0x0804); using var recContext = chineseRecognizer.CreateRecognizerContext(); // 根據(jù)StrokeCollection構(gòu)造 Ink 類型的筆跡數(shù)據(jù)。 using var stream = new MemoryStream(); strokes.Save(stream); using var inkStorage = new Ink(); inkStorage.Load(stream.ToArray()); using var inkStrokes = inkStorage.Strokes; //設(shè)置筆畫數(shù)據(jù) using (recContext.Strokes = inkStrokes) { //識別筆畫數(shù)據(jù) var recognitionResult = recContext.Recognize(out var statusResult); // 如果識別過程中出現(xiàn)問題,則返回null return statusResult == RecognitionStatus.NoError ? recognitionResult.GetAlternatesFromSelection().OfType<RecognitionAlternate>().Select(i => i.ToString()).ToList() : null; } }
這里單字識別,想要提高識別率,可以將stroke合并成一個:
var points = new StylusPointCollection(); foreach (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 alternateCollection.OfType<AnalysisAlternate>().Select(x => x.RecognizedString); } return null; }
看下效果圖
環(huán)境及DLL引用
引用的命名空間是:Windows.Ink和MicroSoft.Ink,需要引用的DLL文件有四個。
- IACore.dll、IALoader.dll、IAWinFX.dll,這三個DLL文件都是Intel集成顯卡驅(qū)動的重要組成部分,包含了圖形處理模塊,尤其是IAWinFX為WPF應(yīng)用提供了支持硬件加速的圖形渲染。
- Microsoft.Ink.dll
值得說明一下,Windows.Ink與Microsoft.Ink在平臺支持上不同,如果有要適配不同版本的windows,需要去上方代碼修改下
- Microsoft.Ink支持Windows XP、Vista 和 Win7 等舊版 Windows,兼容性高。但Win10及以上版本,官方推薦使用Windows.Ink
- Windows.Ink,則僅支持Win8以上版本
引用了上面4個DLL文件后,還有2個環(huán)境問題:
- 在App.config文件中,對節(jié)點startup添加屬性 useLegacyV2RuntimeActivationPolicy="true"
- 修改項目配置為x86
到此這篇關(guān)于C# 手寫識別的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)C# 手寫識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實現(xiàn)將PPT轉(zhuǎn)換成HTML的方法
這篇文章主要介紹了C#實現(xiàn)將PPT轉(zhuǎn)換成HTML的方法,非常實用的功能,需要的朋友可以參考下2014-08-08