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 PrintLines(InkRecognitionRoot root) { var lines = root.GetLines(); return(PrintRecoUnits(root, lines)); }
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(); }