c# – 如何使用Microsoft Cognitive Services – Vision API的OCR识别功能获取文本?
作者:互联网
我正在使用Computer Vision API C# Quick Start提供的样本
我能够获得样本中显示的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中是否提供了任何请求参数来获取响应本身的直接文本?
解决方法:
如果您想要返回响应的C#类型,则可以使用github中的官方客户端SDK.它也可以在NuGet中使用.
一旦你有了OcrResults,而你只想要文本,你可以用Linq写一些hacky C#代码,如下所示:
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());
}
或者,如果这会伤害你的眼睛,你可以使用这样的传统循环:
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();
}
标签:c,computer-vision,ocr,microsoft-cognitive,azure-cognitive-services 来源: https://codeday.me/bug/20190522/1153192.html