How to get only text using OCR recognition feature of Microsoft Cognitive Services - Vision API?
Asked Answered
D

1

7

I am using sample provided at Computer Vision API C# Quick Start I am able to get JSON result as shown in Sample but unable to get only text content.

Sample format of JSON is as below:

{
  "textAngle": 0.020943951023932542,
  "orientation": "NotDetected",
  "language": "de",
  "regions": [
    {
      "boundingBox": "46,54,59,71",
      "lines": [
        {
          "boundingBox": "48,54,49,19",
          "words": [
            {
              "boundingBox": "48,54,49,19",
              "text": "Hello"
            }
          ]
        },
        {
          "boundingBox": "46,106,59,19",
          "words": [
            {
              "boundingBox": "46,106,59,19",
              "text": "World"
            }
          ]
        }
      ]
    }
  ]
}

For now, I am using JSON converter to extract text nodes by adding newline for each word using below class structure.

public class Region
{
    public string BoundingBox { get; set; }
    public List<Line> Lines { get; set; }
}

public class Line
{
    public string BoundingBox { get; set; }
    public List<Word> Words { get; set; }
}

public class Word
{
    public string BoundingBox { get; set; }
    public string Text { get; set; }
}

Is there any request parameter provided in API to get direct text in the response itself?

Dickdicken answered 16/2, 2018 at 1:35 Comment(2)
Not accoring to the API documentationBrowband
You can use this class github.com/Microsoft/Cognitive-Vision-DotNetCore/blob/master/… as alternative, not sure about APILuanneluanni
D
8

If you want C# types for the returned response, you can use the official client SDK in github. It's also available in NuGet.

Once you have the OcrResults, and you just want the text, you could write some hacky C# code with Linq like this:

string OcrResultsToString(OcrResult result)
{
    return string.Join("\n",
        result.Regions.ToList().Select(region =>
            string.Join(" ", region.Lines.ToList().Select(line =>
                 string.Join(" ", line.Words.ToList().Select(word =>
                     word.Text).ToArray())).ToArray())).ToArray());
}

Or, if that hurts your eyes, you could use conventional loops like this:

 string OcrResultsToString(OcrResults results)
 {
    StringBuilder stringBuilder = new StringBuilder();

    if (results != null && results.Regions != null)
    {
        foreach (var item in results.Regions)
        {
            foreach (var line in item.Lines)
            {
                foreach (var word in line.Words)
                {
                    stringBuilder.Append(word.Text);
                    stringBuilder.Append(" ");
                }
                stringBuilder.AppendLine();
            }
            stringBuilder.AppendLine();
        }
    }
    return stringBuilder.ToString();
}
Dicker answered 16/2, 2018 at 7:7 Comment(2)
Thanks, would like to see this to be added in API itselfDickdicken
Totally agree .... Can't believe it is notSkyline

© 2022 - 2024 — McMap. All rights reserved.