5

我正在使用计算机视觉 API C# 快速入门中提供的示例 我能够获得 JSON 结果,如示例中所示,但无法仅获得文本内容。

JSON的样本格式如下:

{
  "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"
            }
          ]
        }
      ]
    }
  ]
}

现在,我正在使用 JSON 转换器通过使用下面的类结构为每个单词添加换行符来提取文本节点。

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; }
}

API 中是否提供了任何请求参数来获取响应本身中的直接文本?

4

1 回答 1

6

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();
}
于 2018-02-16T07:07:50.493 回答