Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 480
userda Avatar asked Feb 16 '18 01:02

userda


1 Answers

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();
}
like image 165
cthrash Avatar answered Sep 18 '22 03:09

cthrash