WPF中,如何针对笔迹,实现文本和形状的智能识别功能?

文本识别常规有两种方法:

  1. Microsoft.Ink.Recognizers
  2. System.Windows.Ink.InkAnalyzer

在使用前,需要添加第三方dll库

  1. IACore.dll
  2. IALoader.dll
  3. IAWinFX.dll
  4. Microsoft.Ink.Analysis.dll
  5. Microsoft.Ink.dll

以上依赖库,可自行网上下载。

以上两种方法的识别效果对比:

InkAnalyzer

该方法可识别文本和形状,多词文本识别尚可,但单个文本识别效果较差,故推荐使用此方法做形状识别。

Recognizer

只能做文本识别,单字和多字识别效果都挺好,跟windows自带的虚拟键盘识别效果差不多。

具体代码:


    //Recognizer 的实现

    public enum RecognizeLanguage
    {
        SimplifiedChinese = 0x0804,
        TraditionalChinese = 0x7c03,
        English = 0x0809
    }

    //识别文本
    public static string RecognizeText(StrokeCollection strokes, RecognizeLanguage language)
    {
        if (strokes == null || strokes.Count == 0)
            return string.Empty;

        var stream = new MemoryStream();

        strokes.Save(stream);

        using (var inkstorage = new Microsoft.Ink.Ink())
        {
            inkstorage.Load(stream.ToArray());

            var recognizers = new Recognizers();

            using (var recContext = recognizers.GetDefaultRecognizer((int)language).CreateRecognizerContext())
            {
                recContext.RecognitionFlags = RecognitionModes.None;
                    
                recContext.Strokes = inkstorage.Strokes;

                var recogResult = recContext.Recognize(out var statusResult);

                inkstorage.Strokes.Dispose();

                recContext.Strokes.Dispose();
                    
                return statusResult == RecognitionStatus.NoError ? recogResult.TopAlternate.ToString() : string.Empty;
                }
            }
        }


        //InkAnalyzer的实现
        //识别文本
        public static string RecognizeText(StrokeCollection strokes, RecognizeLanguage language)
        {
            if (strokes == null || strokes.Count == 0)
                return null;

            var analyzer = new InkAnalyzer();
            analyzer.AddStrokes(strokes);
            analyzer.SetStrokesType(strokes, StrokeType.Drawing);
            analyzer.SetStrokesLanguageId(strokes, (int)language);

            string recognizedString = string.Empty;

            if (analyzer.Analyze().Successful)
            {

                recognizedString = analyzer.GetRecognizedString();
            }
            analyzer.Dispose();

            return recognizedString;
        }

        //识别形状
        public static ShapeRecognizeResult RecognizeShape(StrokeCollection strokes)
        {
            if (strokes == null || strokes.Count == 0)
                return default;

            var analyzer = new InkAnalyzer();
            analyzer.AddStrokes(strokes);
            analyzer.SetStrokesType(strokes, StrokeType.Drawing);

            AnalysisAlternate analysisAlternate = null;
            var sfsaf = analyzer.Analyze();
            if (sfsaf.Successful)
            {
                var alternates = analyzer.GetAlternates();
                if (alternates.Count > 0)
                {
                    analysisAlternate = alternates[0];
                }
            }

            analyzer.Dispose();

            if (analysisAlternate != null && analysisAlternate.AlternateNodes.Count > 0)
            {
                var node = analysisAlternate.AlternateNodes[0] as InkDrawingNode;

                return new ShapeRecognizeResult(node.Centroid, node.HotPoints, analysisAlternate.RecognizedString);
            }

            return default;
        }

    public class ShapeRecognizeResult
    {
        public ShapeRecognizeResult(Point centroid, PointCollection hotPoints,string shapeType)
        {
            Centroid = centroid;
            HotPoints = hotPoints;
            ShapeType = shapeType;
        }

        public string ShapeType { get; }

        public Point Centroid { get; }

        public PointCollection HotPoints { get; }
    }

以上就是文本和形状识别的基础实现,如果要扩展可自行添加代码。如直线识别,表格识别等。


本文会经常更新,请阅读原文: https://huchengv5.gitee.io//post/WPF-%E6%96%87%E6%9C%AC%E5%92%8C%E5%BD%A2%E7%8A%B6%E8%AF%86%E5%88%AB.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名胡承(包含链接: https://huchengv5.gitee.io/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系