Ejemplo n.º 1
0
        private IEnumerable <UIElement> AnalyzeWords()
        {
            var inkwordNodes = _inkAnalyzer.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord);

            foreach (InkAnalysisInkWord node in inkwordNodes)
            {
                var textblock = DrawText(node.RecognizedText, node.BoundingRect);

                var strokesIds = node.GetStrokeIds();
                _strokeService.RemoveStrokesByIds(strokesIds);
                _inkAnalyzer.RemoveDataForStrokes(strokesIds);

                yield return(textblock);
            }
        }
Ejemplo n.º 2
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();
                }
            }
        }
Ejemplo n.º 3
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());
                }
            }
        }
Ejemplo n.º 4
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();
                }
            }
        }
Ejemplo n.º 5
0
        private void InkCanvas_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
        {
            // Convert the selected paragraph or list item
            if (paragraphSelected != null)
            {
                Rect rect = paragraphSelected.BoundingRect;
                var  text = ExtractTextFromParagraph(paragraphSelected);

                if ((rect.X > 0) && (rect.Y > 0) && (text != string.Empty))
                {
                    // Create text box with recognized text
                    var textBlock = new TextBlock();
                    textBlock.Text      = text;
                    textBlock.MaxWidth  = rect.Width;
                    textBlock.MaxHeight = rect.Height;
                    Canvas.SetLeft(textBlock, rect.X);
                    Canvas.SetTop(textBlock, rect.Y);

                    // Remove strokes from InkPresenter
                    IReadOnlyList <uint> strokeIds = paragraphSelected.GetStrokeIds();
                    foreach (var strokeId in strokeIds)
                    {
                        var stroke = inkPresenter.StrokeContainer.GetStrokeById(strokeId);
                        stroke.Selected = true;
                    }
                    inkPresenter.StrokeContainer.DeleteSelected();

                    // Remove strokes from InkAnalyzer
                    inkAnalyzer.RemoveDataForStrokes(strokeIds);

                    // Hide the SelectionRect
                    SelectionRect.Visibility = Visibility.Collapsed;

                    canvas.Children.Add(textBlock);
                    paragraphSelected = null;
                }
            }
        }
Ejemplo n.º 6
0
        private void ConvertShapes()
        {
            IReadOnlyList <IInkAnalysisNode> drawings = inkAnalyzer.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkDrawing);

            foreach (IInkAnalysisNode drawing in drawings)
            {
                var shape = (InkAnalysisInkDrawing)drawing;
                if (shape.DrawingKind == InkAnalysisDrawingKind.Drawing)
                {
                    // Omit unsupported shape
                    continue;
                }

                if (shape.DrawingKind == InkAnalysisDrawingKind.Circle || shape.DrawingKind == InkAnalysisDrawingKind.Ellipse)
                {
                    // Create a Circle or Ellipse on the canvas.
                    AddEllipseToCanvas(shape);
                }
                else
                {
                    // Create a Polygon on the canvas.
                    AddPolygonToCanvas(shape);
                }

                // Select the strokes that were recognized, so we can delete them.
                // The effect is that the shape added to the canvas replaces the strokes.
                foreach (var strokeId in shape.GetStrokeIds())
                {
                    InkStroke stroke = inkPresenter.StrokeContainer.GetStrokeById(strokeId);
                    stroke.Selected = true;
                }

                // Remove the recognized strokes from the analyzer
                // so it won't re-analyze them.
                inkAnalyzer.RemoveDataForStrokes(shape.GetStrokeIds());
            }
            inkPresenter.StrokeContainer.DeleteSelected();
        }
Ejemplo n.º 7
0
 private void InkPresenter_StrokesErased(InkPresenter sender, InkStrokesErasedEventArgs args)
 {
     _inkAnalyzer.RemoveDataForStrokes(args.Strokes.Select(i => i.Id));
 }
Ejemplo n.º 8
0
 private async void InkPresenter_StrokesErased(Windows.UI.Input.Inking.InkPresenter sender, Windows.UI.Input.Inking.InkStrokesErasedEventArgs args)
 {
     _analyzer.RemoveDataForStrokes(args.Strokes.Select(x => x.Id));
     await Analyze();
 }
Ejemplo n.º 9
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();
                }
            }
        }
Ejemplo n.º 10
0
        //Recognizes text from ink strokes
        private async void timer_TickAsync(object sender, object e)
        {
            timer.Stop();

            if (!analyzerText.IsAnalyzing)
            {
                InkAnalysisResult resultText = await analyzerText.AnalyzeAsync();

                if (resultText.Status == InkAnalysisStatus.Updated)
                {
                    var words = analyzerText.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord);

                    foreach (InkAnalysisInkWord word in words)
                    {
                        if (gameEnded)
                        {
                            NewGame();
                        }
                        else if (gridResults[currentCanvasNumber] == ' ')
                        {
                            if (isXNext && (word.RecognizedText == "x" || word.RecognizedText == "X"))
                            {
                                DrawText(word.RecognizedText, word.BoundingRect);

                                gridResults[currentCanvasNumber] = 'x';

                                isXNext = !isXNext;

                                Info.Text = "Now it is Player 2's turn. Draw an 'O' in any remaining square.";
                            }
                            else if (!isXNext && (word.RecognizedText == "o" || word.RecognizedText == "O" || word.RecognizedText == "0"))
                            {
                                DrawText(word.RecognizedText, word.BoundingRect);

                                gridResults[currentCanvasNumber] = 'o';

                                isXNext = !isXNext;

                                Info.Text = "Now it is Player 1's turn. Draw an 'X' in any remaining square.";
                            }
                        }

                        foreach (var strokeId in word.GetStrokeIds())
                        {
                            var stroke = inkCanvas[currentCanvasNumber].InkPresenter.StrokeContainer.GetStrokeById(strokeId);
                            stroke.Selected = true;
                        }
                        analyzerText.RemoveDataForStrokes(word.GetStrokeIds());
                    }
                    inkCanvas[currentCanvasNumber].InkPresenter.StrokeContainer.DeleteSelected();
                }
                else
                {
                    timer.Start();
                }

                analyzerText.ClearDataForAllStrokes();

                checkForWinner();
            }
        }
Ejemplo n.º 11
0
        private void InkPresenter_StrokesErased(InkPresenter sender, InkStrokesErasedEventArgs args)
        {
            var ids = from a in args.Strokes select a.Id;

            inkAnalyzer.RemoveDataForStrokes(ids);
        }
Ejemplo n.º 12
0
        //Recognizes text from the ink strokes
        private async void timer_TickAsync(object sender, object e)
        {
            timer.Stop();

            if (!analyzerText.IsAnalyzing)
            {
                InkAnalysisResult resultText = await analyzerText.AnalyzeAsync();

                if (resultText.Status == InkAnalysisStatus.Updated)
                {
                    var words = analyzerText.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord);

                    foreach (InkAnalysisInkWord word in words)
                    {
                        int userAnswer = 0;

                        if (Int32.TryParse(word.RecognizedText, out userAnswer))
                        {
                            if (userAnswer == answer)
                            {
                                Info.Text = "Correct!";

                                await Task.Delay(TimeSpan.FromSeconds(2));

                                Info.Text = "";

                                generateNewEquation();
                            }
                            else
                            {
                                Info.Text = "Wrong, try again!";

                                await Task.Delay(TimeSpan.FromSeconds(2));

                                Info.Text = "";
                            }
                        }
                        else
                        {
                            Info.Text = "Could not detect a number. Try again!";

                            await Task.Delay(TimeSpan.FromSeconds(2));

                            Info.Text = "";
                        }

                        foreach (var strokeId in word.GetStrokeIds())
                        {
                            var stroke = inkCanvas.InkPresenter.StrokeContainer.GetStrokeById(strokeId);
                            stroke.Selected = true;
                        }
                        analyzerText.RemoveDataForStrokes(word.GetStrokeIds());
                    }
                    inkCanvas.InkPresenter.StrokeContainer.DeleteSelected();
                }
                else
                {
                    timer.Start();
                }

                analyzerText.ClearDataForAllStrokes();
            }
        }
Ejemplo n.º 13
0
        //Recognizes text from ink strokes
        private async void timer_TickAsync(object sender, object e)
        {
            timer.Stop();

            if (!analyzerText.IsAnalyzing)
            {
                InkAnalysisResult resultText = await analyzerText.AnalyzeAsync();

                if (resultText.Status == InkAnalysisStatus.Updated)
                {
                    var words = analyzerText.AnalysisRoot.FindNodes(InkAnalysisNodeKind.InkWord);

                    foreach (InkAnalysisInkWord word in words)
                    {
                        if (calculated)
                        {
                            clear();
                        }
                        else if (currentCanvasNumber == 0)
                        {
                            int temp = numbers[0];

                            if (Int32.TryParse(word.RecognizedText, out numbers[0]))
                            {
                                textBlock[0].Text = numbers[0] + "";

                                hasValues[0] = true;
                            }
                            else if (hasValues[0])
                            {
                                numbers[0] = temp;
                            }
                        }
                        else if (currentCanvasNumber == 1)
                        {
                            if (word.RecognizedText == "/")
                            {
                                mathOperator = '/';

                                textBlock[1].Text = "/";
                            }
                            else if (word.RecognizedText == "*" || word.RecognizedText == "x" || word.RecognizedText == "X")
                            {
                                mathOperator = '*';

                                textBlock[1].Text = "*";
                            }
                            else if (word.RecognizedText == "+")
                            {
                                mathOperator = '+';

                                textBlock[1].Text = "+";
                            }
                            else if (word.RecognizedText == "-" || word.RecognizedText == '_')
                            {
                                mathOperator = '-';

                                textBlock[1].Text = "-";
                            }
                        }
                        else if (currentCanvasNumber == 2)
                        {
                            int temp = numbers[1];

                            if (Int32.TryParse(word.RecognizedText, out numbers[1]))
                            {
                                textBlock[2].Text = numbers[1] + "";

                                hasValues[1] = true;
                            }
                            else if (hasValues[1])
                            {
                                numbers[1] = temp;
                            }
                        }
                        else if (currentCanvasNumber == 3)
                        {
                            if (word.RecognizedText == "=" && canCalculate())
                            {
                                textBlock[3].Text = "=";

                                calculate();
                            }
                        }

                        foreach (var strokeId in word.GetStrokeIds())
                        {
                            var stroke = inkCanvas[currentCanvasNumber].InkPresenter.StrokeContainer.GetStrokeById(strokeId);
                            stroke.Selected = true;
                        }
                        analyzerText.RemoveDataForStrokes(word.GetStrokeIds());
                    }
                    inkCanvas[currentCanvasNumber].InkPresenter.StrokeContainer.DeleteSelected();
                }
                else
                {
                    timer.Start();
                }

                analyzerText.ClearDataForAllStrokes();
            }
        }
Ejemplo n.º 14
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();
        }
Ejemplo n.º 15
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();
                }
            }
        }