private static string PrintRecoUnits(InkRecognitionRoot root, IEnumerable <InkRecognitionUnit> recoUnits)
        {
            var recognizedText = new StringBuilder();

            foreach (var recoUnit in recoUnits)
            {
                switch (recoUnit.Kind)
                {
                case RecognitionUnitKind.InkWord:
                    var word = recoUnit as InkWord;
                    recognizedText.Append(word.RecognizedText).Append(" ");
                    break;

                case RecognitionUnitKind.Line:
                    var line         = recoUnit as InkLine;
                    var lineChildren = root.GetChildNodesById(line.Id).ToList();
                    recognizedText.Append(PrintLineChildren(root, lineChildren)).AppendLine();
                    break;

                case RecognitionUnitKind.InkDrawing:
                    var shape     = recoUnit as InkDrawing;
                    var shapeKind = String.Format("[{0}] ", shape.RecognizedShape);
                    recognizedText.Append(shapeKind);
                    break;

                case RecognitionUnitKind.InkBullet:
                    var bullet = recoUnit as InkBullet;
                    recognizedText.Append(bullet.RecognizedText);
                    break;
                }
            }
            return(recognizedText.ToString());
        }
Пример #2
0
        private static string PrintRecoUnits(InkRecognitionRoot root, IEnumerable <InkRecognitionUnit> recoUnits)
        {
            var recognizedText = new StringBuilder();

            foreach (var recoUnit in recoUnits)
            {
                switch (recoUnit.Kind)
                {
                case RecognitionUnitKind.InkWord:
                    var word     = recoUnit as InkWord;
                    var wordKind = String.Format("[{0}, TopY: {1}, Height: {2}] ", word.RecognizedText, word.BoundingRect.TopY, word.BoundingRect.Height);
                    recognizedText.Append(wordKind).Append(" ");
                    break;

                case RecognitionUnitKind.Line:
                    var line       = recoUnit as InkLine;
                    var lineFormat = String.Format("[{0}, TopY: {1}, Height: {2}] ", line.RecognizedText, line.BoundingRect.TopY, line.BoundingRect.Height);
                    //recognizedText.Append(PrintLineChildren(root, lineChildren)).AppendLine();
                    recognizedText.Append(lineFormat);
                    break;

                case RecognitionUnitKind.InkDrawing:
                    var shape     = recoUnit as InkDrawing;
                    var shapeKind = String.Format("[{0}, TopY: {1}, Height: {2}] ", shape.RecognizedShape, shape.BoundingRect.TopY, shape.BoundingRect.Height);
                    recognizedText.Append(shapeKind);
                    break;

                case RecognitionUnitKind.InkBullet:
                    var bullet = recoUnit as InkBullet;
                    recognizedText.Append(bullet.RecognizedText);
                    break;
                }
            }
            return(recognizedText.ToString());
        }
Пример #3
0
        public string[] GetOCR(string jsonStrokes, string url, string key, string language)
        {
            var configuration = new ConfigurationBuilder().AddJsonFile("config.json").Build();

            if (configuration != null)
            {
                string endpoint        = configuration["endpoint"];
                string subscriptionKey = key; //configuration["key"];

                string lang = language;       //configuration["language"];
                jsonStrokes = jsonStrokes.Replace("\"language\": \"en-US\"", "\"language\": \"" + lang + "\"");

                string requestData = jsonStrokes;
                string apiAddress  = url;

                using (HttpClient client = new HttpClient {
                    BaseAddress = new Uri(apiAddress)
                })
                {
                    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

                    var content = new StringContent(requestData, Encoding.UTF8, "application/json");
                    var res     = client.PutAsync(endpoint, content).Result;
                    if (res.IsSuccessStatusCode)
                    {
                        string result = res.Content.ReadAsStringAsync().Result;

                        InkRecognitionRoot root = JSONProcessor.ParseInkRecognizerResponse(result);

                        if (root != null)
                        {
                            List <InkLine> lstLines = root.GetLines().ToList();
                            string         tekst    = string.Empty;
                            foreach (InkLine line in lstLines)
                            {
                                tekst += tekst + line.RecognizedText;
                            }
                            string[] words = lstLines.Select(q => q.RecognizedText).ToArray();
                            return(words);
                        }
                        else
                        {
                            throw new Exception("Can't parse response");
                        }
                    }
                    else
                    {
                        throw new Exception($"ErrorCode: {res.StatusCode}");
                    }
                }
            }
            else
            {
                throw new Exception("Can't parse config file");
            }
        }
        public static string Print(InkRecognitionRoot root)
        {
            var output = new StringBuilder();

            output.Append(PrintLines(root));
            output.Append(PrintShapes(root));

            return(output.ToString());
        }
Пример #5
0
        private void DrawAllRecognzied(InkRecognitionRoot root)
        {
            var units = root.GetUnits().ToList();

            foreach (var unit in units)
            {
                recogCanvas.Children.Add(GetPolygon(unit.BoundingRect, ColorHelper.FromArgb(255, 0, 255, 0)));
            }
        }
Пример #6
0
 public static InkRecognitionRoot ParseInkRecognizerResponse(string responseJson)
 {
     try
     {
         var responseObj = JsonConvert.DeserializeObject <InkRecognitionResponse>(responseJson,
                                                                                  new InkRecognitionResponseConverter());
         var result = new InkRecognitionRoot(responseObj);
         return(result);
     }
     catch (Exception e)
     {
         throw new JsonWriterException(e.Message);
     }
 }
 private static string PrintLineChildren(InkRecognitionRoot root, IEnumerable <InkRecognitionUnit> lineChildren)
 {
     return(PrintRecoUnits(root, lineChildren));
 }
        public static string PrintShapes(InkRecognitionRoot root)
        {
            var shapes = root.GetShapes();

            return(PrintRecoUnits(root, shapes));
        }
        public static string PrintLines(InkRecognitionRoot root)
        {
            var lines = root.GetLines();

            return(PrintRecoUnits(root, lines));
        }
Пример #10
0
        public DrawsomePic(InkRecognitionRoot root, List <InkRecognizerStroke> inkStrokes, int hookUpDistance)
        {
            this.HookUpDistance = hookUpDistance;
            this.InkRoot        = root;
            if (root.GetShapes().Count() == 0)
            {
                throw new Exception("The Graph is Empty");
            }

            //this.Root = new DrawsomeShape(root.GetShapes().ToList().OrderBy(item => item.BoundingRect.TopY).First());

            // get all recognized shapes and set the text belonged to them
            foreach (var shape in root.GetShapes())
            {
                var shapeToAdd = new DrawsomeShape(shape)
                {
                    Text = root.GetLines().ToList().FindAll(item => shape.Contains(item)).FirstOrDefault()?.RecognizedText
                };
                if (shapeToAdd.Type != ShapeType.Drawing)
                {
                    // add all shapes except line
                    this.AllShapes.Add(shapeToAdd);
                }
            }

            // units include lines and shapes and texts
            foreach (var unit in root.GetUnits())
            {
                var unitToAdd = new DrawsomeObj(unit);
                this.AllUnits.Add(unitToAdd);
            }

            var inkLines = root.GetUnits().Where(item => (item as InkDrawing)?.RecognizedShape == DrawingShapeKind.Drawing).Where(item => item.BoundingRect.Width + item.BoundingRect.Height > 20);



            foreach (var line in inkLines)
            {
                this.AllLines.Add(new DrawsomeLine(line, inkStrokes, 3, HookUpDistance));
            }

            // we iterate each shape to detect the next and previous line
            foreach (var dShape in this.AllShapes)
            {
                var nextLines = this.AllLines.OrderByDescending(item => dShape.OverlapSizeWithLinesBegin(item, item.LittleRects.Count / 10)).Where(item => dShape.OverlapSizeWithLinesBegin(item, item.LittleRects.Count / 10) != 0);
                foreach (var nextLine in nextLines)
                {
                    dShape.Next.Add(nextLine);
                }

                var prevLines = this.AllLines.OrderByDescending(item => dShape.OverlapSizeWithLinesEnd(item, item.LittleRects.Count / 10)).Where(item => dShape.OverlapSizeWithLinesEnd(item, item.LittleRects.Count / 10) != 0);
                foreach (var prevLine in prevLines)
                {
                    prevLine.Next.Add(dShape);
                }
            }

            // reorder the next of each shape to ensure the colored lines behind
            foreach (var dShape in this.AllShapes)
            {
                dShape.Next.Sort((A, B) => (A as DrawsomeLine)?.MainStroke.inkStrokeInternal.DrawingAttributes.Color.R > (B as DrawsomeLine)?.MainStroke.inkStrokeInternal.DrawingAttributes.Color.R ? 1 : -1);
            }

            this.Root = this.AllShapes.OrderBy(item => item.RecogUnit.BoundingRect.TopY).FirstOrDefault();
        }