Exemple #1
0
        public async Task <OcrHWResult> GetText(Stream stream, string url = null)
        {
            var visionResult = await(stream != null ? visionClient.RecognizeTextAsync(stream, "en", false) :  visionClient.RecognizeTextAsync(url, "en", false));

            var lines = visionResult.Regions.SelectMany(r => r.Lines).Select(l =>

                                                                             new lineResult()
            {
                boundingBox = ConvertBoundingBox(l.BoundingBox),
                words       = l.Words.Select(w => new WordResult()
                {
                    boundingBox = ConvertBoundingBox(w.BoundingBox),
                    text        = w.Text
                }).ToArray()
            }
                                                                             );


            var result = new OcrHWResult()
            {
                lines = lines.ToArray()
            };

            return(result);
        }
Exemple #2
0
        public OcrHWResult Concat(OcrHWResult result)
        {
            var newResult = new OcrHWResult();

            newResult.lines  = lines.Concat(result.lines).ToArray();
            newResult.Tags   = Tags.Concat(result.Tags).ToArray();
            newResult.Width  = Width + result.Width;
            newResult.Height = Height + result.Height;
            return(newResult);
        }
Exemple #3
0
        public async Task <OcrHWResult> GetVision(Stream stream, string url = null)
        {
            var features     = new[] { VisualFeature.Tags, VisualFeature.ImageType, VisualFeature.Description, VisualFeature.Adult };
            var visionResult = stream != null ?
                               await visionClient.AnalyzeImageAsync(stream, features)
                : await visionClient.AnalyzeImageAsync(url, features);

            List <lineResult> lines = new List <lineResult>();

            lines.AddRange(visionResult.Description.Captions.Select(c => new lineResult()
            {
                words = c.Text.Split(' ').Select(w => new WordResult()
                {
                    text = w
                }).ToArray()
            }
                                                                    ));

            lines.Add(new lineResult()
            {
                words = visionResult.Tags.Select(t => new WordResult()
                {
                    text = t.Name
                }).ToArray()
            });

            var result = new OcrHWResult()
            {
                lines      = lines.ToArray(),
                AdultScore = visionResult.Adult.AdultScore,
                RacyScore  = visionResult.Adult.RacyScore,
                Tags       = visionResult.Tags.Select(t => t.Name),
                Width      = visionResult.Metadata.Width,
                Height     = visionResult.Metadata.Height,
            };

            return(result);
        }
Exemple #4
0
        public static OcrHWResult FixLinesAndWordOrder(OcrHWResult result)
        {
            var allWords = result.lines.SelectMany(l => l.words);

            var avgWordHeight = (float)allWords.Average(w => w.Height);
            int lineTol       = (int)(avgWordHeight * .4);
            var wordGroups    = GetClusters(allWords, l => l.CenterY, lineTol);

            var foo = wordGroups.OrderBy(lg => lg.First().CenterY);

            Console.WriteLine();
            Console.WriteLine("text:");
            foreach (var lg in foo)
            {
                //Console.WriteLine("Y: " + lg.First().CenterY);
                foreach (var l in lg.OrderBy(l => l.StartX))
                {
                    Console.Write(" " + l.text);
                }
                Console.WriteLine();
            }

            return(result);
        }
        public IEnumerable <WordResult> AddPage(OcrHWResult hw, string imageUrl)
        {
            // page
            metadata.WriteLine($"  <div class='ocr_page' id='page_{pageCount}' title='image \"{imageUrl}\"; bbox 0 0 {hw.Width} {hw.Height}; ppageno {pageCount}'>");
            metadata.WriteLine($"    <div class='ocr_carea' id='block_{pageCount}_1'");

            var allwords = new List <WordResult>();

            int li = 0;
            int wi = 0;

            foreach (var line in hw.lines /*.SortLines(hw.lines)*/)
            {
                metadata.WriteLine($"    <span class='ocr_line' id='line_{pageCount}_{li}' title='baseline -0.002 -5; x_size 30; x_descenders 6; x_ascenders 6'>");

                var words = line.words.FirstOrDefault()?.boundingBox == null ? line.words : line.words.OrderBy(l => l.boundingBox[0]).ToArray();

                foreach (var word in words)
                {
                    var bbox = word.boundingBox != null && word.boundingBox.Length == 8 ? $"bbox {word.boundingBox[0]} {word.boundingBox[1]} {word.boundingBox[4]} {word.boundingBox[5]}" : "";
                    metadata.WriteLine($"      <span class='ocrx_word' id='word_{pageCount}_{li}_{wi}' title='{bbox}'>{word.text}</span>");
                    text.WriteLine(word.text);
                    wi++;
                    allwords.Add(word);
                }
                li++;
                metadata.WriteLine(" </span>"); // line
            }

            metadata.WriteLine("    </div>"); // reading area
            metadata.WriteLine("  </div>");   // page

            pageCount++;

            return(allwords);
        }
Exemple #6
0
        public async Task <OcrHWResult> GetHandwritingText(Stream stream, string url = null)
        {
            var client = new HttpClient();

            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "92e5723b229149519f717c1e1ae81443");

            var uri = "https://westus.api.cognitive.microsoft.com/vision/v1.0/recognizeText?handwriting=true";

            HttpResponseMessage response;

            // Request body
            if (stream != null)
            {
                using (var content = new StreamContent(stream))
                {
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                    response = await client.PostAsync(uri, content);
                }
            }
            else
            {
                var json = JsonConvert.SerializeObject(new { url = url });
                using (var content = new StringContent(json))
                {
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    response = await client.PostAsync(uri, content);
                }
            }

            OcrHWResult          result = null;
            IEnumerable <string> opLocation;

            if (!response.IsSuccessStatusCode)
            {
                var err = await response.Content.ReadAsStringAsync();

                response.EnsureSuccessStatusCode();
            }



            if (response.Headers.TryGetValues("Operation-Location", out opLocation))
            {
                while (true)
                {
                    response = await client.GetAsync(opLocation.First());

                    var txt = await response.Content.ReadAsStringAsync();

                    var status = JsonConvert.DeserializeObject <AsyncStatusResult>(txt);
                    if (status.status == "Running" || status.status == "NotStarted")
                    {
                        await Task.Delay(TimeSpan.FromMilliseconds(500));
                    }
                    else
                    {
                        result = status.recognitionResult;
                        break;
                    }
                }
            }

            return(result);
        }