Esempio n. 1
0
        private async void recognizeText_ClickAsync(object sender, RoutedEventArgs e)
        {
            strokesText = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();
            // Ensure an ink stroke is present.
            if (strokesText.Count > 0)
            {
                analyzerText.AddDataForStrokes(strokesText);

                resultText = await analyzerText.AnalyzeAsync();

                if (resultText.Status == InkAnalysisStatus.Updated)
                {
                    words = analyzerText.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord);
                    foreach (var word in words)
                    {
                        InkAnalysisInkWord concreteWord = (InkAnalysisInkWord)word;
                        foreach (string s in concreteWord.TextAlternates)
                        {
                            recognitionResult.Text += s;
                        }
                    }
                }
                analyzerText.ClearDataForAllStrokes();
            }
            strokesText = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();
            // Ensure an ink stroke is present.
            if (strokesText.Count > 0)
            {
                analyzerText.AddDataForStrokes(strokesText);

                // Force analyzer to process strokes as handwriting.
                foreach (var stroke in strokesText)
                {
                    analyzerText.SetStrokeDataKind(stroke.Id, InkAnalysisStrokeKind.Writing);
                }

                // Clear recognition results string.
                recognitionResult.Text = "";

                resultText = await analyzerText.AnalyzeAsync();

                if (resultText.Status == InkAnalysisStatus.Updated)
                {
                    var text = analyzerText.AnalysisRoot.RecognizedText;
                    words = analyzerText.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord);
                    foreach (var word in words)
                    {
                        InkAnalysisInkWord concreteWord = (InkAnalysisInkWord)word;
                        foreach (string s in concreteWord.TextAlternates)
                        {
                            recognitionResult.Text += s + " ";
                        }
                        recognitionResult.Text += " / ";
                    }
                }
                analyzerText.ClearDataForAllStrokes();
            }
        }
Esempio n. 2
0
        public async Task <InkTransformResult> TransformTextAndShapesAsync()
        {
            var result     = new InkTransformResult(_drawingCanvas);
            var inkStrokes = GetStrokesToConvert();

            if (inkStrokes.Any())
            {
                _inkAnalyzer.ClearDataForAllStrokes();
                _inkAnalyzer.AddDataForStrokes(inkStrokes);
                var inkAnalysisResults = await _inkAnalyzer.AnalyzeAsync();

                if (inkAnalysisResults.Status == InkAnalysisStatus.Updated)
                {
                    var words  = AnalyzeWords();
                    var shapes = AnalyzeShapes();

                    //Generate result
                    result.Strokes.AddRange(inkStrokes);
                    result.TextAndShapes.AddRange(words);
                    result.TextAndShapes.AddRange(shapes);
                }
            }

            return(result);
        }
Esempio n. 3
0
        // End "Step 5: Support handwriting recognition"

        // Begin "Step 6: Recognize shapes"
        //On click recognise shape
        private async void recognizeShape_ClickAsync(object sender, RoutedEventArgs e)
        {
            strokesShape = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();

            if (strokesShape.Count > 0)
            {
                analyzerShape.AddDataForStrokes(strokesShape);

                resultShape = await analyzerShape.AnalyzeAsync();

                if (resultShape.Status == InkAnalysisStatus.Updated)
                {
                    var drawings = analyzerShape.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkDrawing);

                    foreach (var drawing in drawings)
                    {
                        var shape = (InkAnalysisInkDrawing)drawing;
                        if (shape.DrawingKind == InkAnalysisDrawingKind.Drawing)
                        {
                            // Catch and process unsupported shapes (lines and so on) here.
                        }
                        else
                        {
                            // Process recognized shapes here.
                            if (shape.DrawingKind == InkAnalysisDrawingKind.Circle || shape.DrawingKind == InkAnalysisDrawingKind.Ellipse)
                            {
                                DrawEllipse(shape);
                            }
                            else
                            {
                                DrawPolygon(shape);
                            }
                            foreach (var strokeId in shape.GetStrokeIds())
                            {
                                var stroke = inkCanvas.InkPresenter.StrokeContainer.GetStrokeById(strokeId);
                                stroke.Selected = true;
                            }
                        }
                        analyzerShape.RemoveDataForStrokes(shape.GetStrokeIds());
                    }
                    inkCanvas.InkPresenter.StrokeContainer.DeleteSelected();
                }
            }
        }
Esempio n. 4
0
 private void InkPresenter_StrokesCollected(Windows.UI.Input.Inking.InkPresenter sender, Windows.UI.Input.Inking.InkStrokesCollectedEventArgs args)
 {
     strokeTimer.Stop();
     inkAnalyzer.AddDataForStrokes(args.Strokes);
     foreach (InkStroke stroke in args.Strokes)
     {
         // since this is a drawing application, we can tell InkAnalyzer about
         // the nature of the stroke - it's optional but will improve the shape
         // recognition result for small drawings that may get confused with
         // handwritten letters
         inkAnalyzer.SetStrokeDataKind(stroke.Id, InkAnalysisStrokeKind.Drawing);
     }
     strokeTimer.Start();
 }
        private async static void recognize(InkPresenter presenter)
        {
            IReadOnlyList <InkStroke> strokes = presenter.StrokeContainer.GetStrokes();

            if (strokes.Any())
            {
                inkAnalyzer.AddDataForStrokes(strokes);
                InkAnalysisResult results = await inkAnalyzer.AnalyzeAsync();

                if (results.Status == InkAnalysisStatus.Updated)
                {
                    ConvertShapes(presenter, inkAnalyzer.AnalysisRoot);
                }
            }

            inkAnalyzer.ClearDataForAllStrokes();
        }
Esempio n. 6
0
        private async void RecognizeButton_Click(object sender, RoutedEventArgs e)
        {
            var inkStrokes = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();

            // Ensure an ink stroke is present.
            if (inkStrokes.Count > 0)
            {
                _inkAnalyzer.AddDataForStrokes(inkStrokes);
                var inkAnalysisResults = await _inkAnalyzer.AnalyzeAsync();

                // Have ink strokes on the canvas changed?
                if (inkAnalysisResults.Status == InkAnalysisStatus.Updated)
                {
                    var inkdrawingNodes =
                        _inkAnalyzer.AnalysisRoot.FindNodes(
                            InkAnalysisNodeKind.InkDrawing);
                    foreach (InkAnalysisInkDrawing node in inkdrawingNodes)
                    {
                        if (node.DrawingKind != InkAnalysisDrawingKind.Drawing)
                        {
                            // Draw an Ellipse object on the recognitionCanvas (circle is a specialized ellipse).
                            if (node.DrawingKind == InkAnalysisDrawingKind.Circle || node.DrawingKind == InkAnalysisDrawingKind.Ellipse)
                            {
                                DrawEllipse(node);
                            }
                            // Draw a Polygon object on the recognitionCanvas.
                            else
                            {
                                DrawPolygon(node);
                            }
                            foreach (var strokeId in node.GetStrokeIds())
                            {
                                var stroke = inkCanvas.InkPresenter.StrokeContainer.GetStrokeById(strokeId);
                                stroke.Selected = true;
                            }
                        }
                        _inkAnalyzer.RemoveDataForStrokes(node.GetStrokeIds());
                    }
                    inkCanvas.InkPresenter.StrokeContainer.DeleteSelected();
                }
            }
        }
Esempio n. 7
0
        internal async void RecognizeStrokes(IEnumerable <InkStroke> strokes)
        {
            inkAnalyzer.AddDataForStrokes(strokes);
            inkAnalysisResults = await inkAnalyzer.AnalyzeAsync();

            // Find all strokes that are recognized as a drawing and create a corresponding ink analysis InkDrawing node.
            var inkdrawingNodes = inkAnalyzer.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkDrawing);

            foreach (InkAnalysisInkDrawing node in inkdrawingNodes)
            {
                // Draw an Ellipse object on the recognitionCanvas (circle is a specialized ellipse).
                if (node.DrawingKind == InkAnalysisDrawingKind.Circle || node.DrawingKind == InkAnalysisDrawingKind.Ellipse)
                {
                    DrawEllipse(node);
                    inkAnalyzer.RemoveDataForStrokes(node.GetStrokeIds());
                }
                else if (node.DrawingKind != InkAnalysisDrawingKind.Drawing) // Draw a Polygon object on the inkCanvas.
                {
                    DrawPolygon(node);
                    inkAnalyzer.RemoveDataForStrokes(node.GetStrokeIds());
                }
            }
        }
Esempio n. 8
0
 private void InkPresenter_StrokesCollected(InkPresenter sender, InkStrokesCollectedEventArgs args)
 {
     dispatcherTimer.Stop();
     inkAnalyzer.AddDataForStrokes(args.Strokes);
     dispatcherTimer.Start();
 }
Esempio n. 9
0
 private void InkPresenter_StrokesCollected(InkPresenter sender, InkStrokesCollectedEventArgs args)
 {
     _inkAnalyzer.AddDataForStrokes(args.Strokes);
 }
Esempio n. 10
0
 private async void InkPresenter_StrokesCollected(Windows.UI.Input.Inking.InkPresenter sender, Windows.UI.Input.Inking.InkStrokesCollectedEventArgs args)
 {
     _analyzer.AddDataForStrokes(args.Strokes);
     await Analyze();
 }
Esempio n. 11
0
        private async Task FreeFormHandWriting()
        {
            inkStrokes = InputCanvas.InkPresenter.StrokeContainer.GetStrokes();
            // Ensure an ink stroke is present.
            if (inkStrokes.Count > 0)
            {
                inkAnalyzer.AddDataForStrokes(inkStrokes);

                // In this example, we try to recognizing both
                // writing and drawing, so the platform default
                // of "InkAnalysisStrokeKind.Auto" is used.
                // If you're only interested in a specific type of recognition,
                // such as writing or drawing, you can constrain recognition
                // using the SetStrokDataKind method as follows:
                // foreach (var stroke in strokesText)
                // {
                //     analyzerText.SetStrokeDataKind(
                //      stroke.Id, InkAnalysisStrokeKind.Writing);
                // }
                // This can improve both efficiency and recognition results.
                inkAnalysisResults = await inkAnalyzer.AnalyzeAsync();

                // Have ink strokes on the canvas changed?
                if (inkAnalysisResults.Status == InkAnalysisStatus.Updated)
                {
                    // Find all strokes that are recognized as handwriting and
                    // create a corresponding ink analysis InkWord node.
                    var inkwordNodes =
                        inkAnalyzer.AnalysisRoot.FindNodes(
                            InkAnalysisNodeKind.InkWord);

                    // Iterate through each InkWord node.
                    // Draw primary recognized text on recognitionCanvas
                    // (for this example, we ignore alternatives), and delete
                    // ink analysis data and recognized strokes.
                    foreach (InkAnalysisInkWord node in inkwordNodes)
                    {
                        // Draw a TextBlock object on the recognitionCanvas.
                        DrawText(node.RecognizedText, node.BoundingRect);

                        foreach (var strokeId in node.GetStrokeIds())
                        {
                            var stroke =
                                InputCanvas.InkPresenter.StrokeContainer.GetStrokeById(strokeId);
                            stroke.Selected = true;
                        }
                        inkAnalyzer.RemoveDataForStrokes(node.GetStrokeIds());
                    }
                    InputCanvas.InkPresenter.StrokeContainer.DeleteSelected();

                    // Find all strokes that are recognized as a drawing and
                    // create a corresponding ink analysis InkDrawing node.
                    var inkdrawingNodes =
                        inkAnalyzer.AnalysisRoot.FindNodes(
                            InkAnalysisNodeKind.InkDrawing);
                    // Iterate through each InkDrawing node.
                    // Draw recognized shapes on recognitionCanvas and
                    // delete ink analysis data and recognized strokes.
                    foreach (InkAnalysisInkDrawing node in inkdrawingNodes)
                    {
                        if (node.DrawingKind == InkAnalysisDrawingKind.Drawing)
                        {
                            // Catch and process unsupported shapes (lines and so on) here.
                        }
                        // Process generalized shapes here (ellipses and polygons).
                        else
                        {
                            // Draw an Ellipse object on the recognitionCanvas (circle is a specialized ellipse).
                            if (node.DrawingKind == InkAnalysisDrawingKind.Circle || node.DrawingKind == InkAnalysisDrawingKind.Ellipse)
                            {
                                DrawEllipse(node);
                            }
                            // Draw a Polygon object on the recognitionCanvas.
                            else
                            {
                                DrawPolygon(node);
                            }
                            foreach (var strokeId in node.GetStrokeIds())
                            {
                                var stroke = InputCanvas.InkPresenter.StrokeContainer.GetStrokeById(strokeId);
                                stroke.Selected = true;
                            }
                        }
                        inkAnalyzer.RemoveDataForStrokes(node.GetStrokeIds());
                    }
                    InputCanvas.InkPresenter.StrokeContainer.DeleteSelected();
                }
            }
        }
Esempio n. 12
0
        private async void Recognize_OnClick(object sender, RoutedEventArgs e)
        {
            var analyzer = new InkAnalyzer();
            var strokes  = InkCanvas.InkPresenter.StrokeContainer.GetStrokes();

            if (strokes.Count <= 0)
            {
                return;
            }

            analyzer.AddDataForStrokes(strokes);

            var results = await analyzer.AnalyzeAsync();

            if (results.Status != InkAnalysisStatus.Updated)
            {
                return;
            }

            // find recognized strokes
            var textNodes = analyzer.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord);

            // draw all text
            foreach (InkAnalysisInkWord node in textNodes)
            {
                DrawText(node.RecognizedText, node.BoundingRect);

                foreach (var strokeId in node.GetStrokeIds())
                {
                    var stroke = InkCanvas.InkPresenter.StrokeContainer.GetStrokeById(strokeId);
                    if (UseSystemFocusVisuals)
                    {
                        stroke.Selected = true;
                    }
                }
                analyzer.RemoveDataForStrokes(node.GetStrokeIds());
            }
            InkCanvas.InkPresenter.StrokeContainer.DeleteSelected();

            var shapeNodes = analyzer.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkDrawing);

            // draw shapes
            foreach (InkAnalysisInkDrawing node in shapeNodes)
            {
                if (node.DrawingKind == InkAnalysisDrawingKind.Drawing)
                {
                    // unsupported shape
                    continue;
                }

                Draw(node);

                foreach (var strokeId in node.GetStrokeIds())
                {
                    var stroke = InkCanvas.InkPresenter.StrokeContainer.GetStrokeById(strokeId);
                    stroke.Selected = true;
                }

                analyzer.RemoveDataForStrokes(node.GetStrokeIds());
            }
            InkCanvas.InkPresenter.StrokeContainer.DeleteSelected();
        }
Esempio n. 13
0
        /// <summary>
        /// Process drawn ink strokes into a bounding box to track
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void UIUpdateButton_Click(object sender, RoutedEventArgs e)
        {
            var inkStrokes = UIInkCanvasOverlay.InkPresenter.StrokeContainer.GetStrokes();

            // Ensure an ink stroke is present.
            if (inkStrokes.Count > 0)
            {
                m_inkAnalyzer.AddDataForStrokes(inkStrokes);

                // Only analyze as drawings
                foreach (var strokeNode in inkStrokes)
                {
                    m_inkAnalyzer.SetStrokeDataKind(strokeNode.Id, InkAnalysisStrokeKind.Drawing);
                }

                var inkAnalysisResults = await m_inkAnalyzer.AnalyzeAsync();

                // Have ink strokes on the canvas changed?
                if (inkAnalysisResults.Status == InkAnalysisStatus.Updated)
                {
                    // Find all strokes that are recognized as a drawing and
                    // create a corresponding ink analysis InkDrawing node.
                    var inkdrawingNodes = m_inkAnalyzer.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkDrawing);
                    // Draw recognized shapes on UICanvasOverlay and
                    // delete ink analysis data and recognized strokes.
                    List <Rect> drawnRects = new List <Rect>();
                    foreach (InkAnalysisInkDrawing node in inkdrawingNodes)
                    {
                        if (node.DrawingKind == InkAnalysisDrawingKind.Drawing)
                        {
                            // Catch and process unsupported shapes (lines and so on) here.
                            foreach (var strokeId in node.GetStrokeIds())
                            {
                                var stroke            = UIInkCanvasOverlay.InkPresenter.StrokeContainer.GetStrokeById(strokeId);
                                var drawingAttributes = stroke.DrawingAttributes;
                                drawingAttributes.Color  = Windows.UI.Colors.Red;
                                stroke.DrawingAttributes = drawingAttributes;
                            }
                        }
                        // Process generalized shapes here (ellipses and polygons).
                        else
                        {
                            // Normalized rect
                            var boundingRectNormalized = new Rect(node.BoundingRect.X / UIInkCanvasOverlay.ActualWidth,
                                                                  node.BoundingRect.Y / UIInkCanvasOverlay.ActualHeight,
                                                                  node.BoundingRect.Width / UIInkCanvasOverlay.ActualWidth,
                                                                  node.BoundingRect.Height / UIInkCanvasOverlay.ActualHeight);
                            drawnRects.Add(boundingRectNormalized);

                            // Mark ink node strokes for deletion
                            foreach (var strokeId in node.GetStrokeIds())
                            {
                                var stroke = UIInkCanvasOverlay.InkPresenter.StrokeContainer.GetStrokeById(strokeId);
                                stroke.Selected = true;
                            }
                        }
                        m_inkAnalyzer.RemoveDataForStrokes(node.GetStrokeIds());
                    }
                    UIInkCanvasOverlay.InkPresenter.StrokeContainer.DeleteSelected();

                    await m_bboxLock.WaitAsync();

                    {
                        m_drawnRects.AddRange(drawnRects);
                        m_objectTrackRenderer.RenderRects(m_drawnRects);
                    }
                    m_bboxLock.Release();
                }
            }
        }