I created a simple class to test out googles vision OCR API. I am passing in a simple image with 5 letters that should return a string with "CRAIG" in it. Although this API call returns a lot of extra information:
{
"property": {
"detectedLanguages": [
{
"languageCode": "en"
}
]
},
"boundingBox": {
"vertices": [
{
"x": 183,
"y": 105
},
{
"x": 674,
"y": 105
},
{
"x": 674,
"y": 253
},
{
"x": 183,
"y": 253
}
]
},
"symbols": [
{
"property": {
"detectedLanguages": [
{
"languageCode": "en"
}
]
},
"boundingBox": {
"vertices": [
{
"x": 183,
"y": 105
},
{
"x": 257,
"y": 105
},
{
"x": 257,
"y": 253
},
{
"x": 183,
"y": 253
}
]
},
"text": "C",
"confidence": 0.99
},
{
"property": {
"detectedLanguages": [
{
"languageCode": "en"
}
]
},
"boundingBox": {
"vertices": [
{
"x": 249,
"y": 105
},
{
"x": 371,
"y": 105
},
{
"x": 371,
"y": 253
},
{
"x": 249,
"y": 253
}
]
},
"text": "R",
"confidence": 0.99
},
{
"property": {
"detectedLanguages": [
{
"languageCode": "en"
}
]
},
"boundingBox": {
"vertices": [
{
"x": 459,
"y": 105
},
{
"x": 581,
"y": 105
},
{
"x": 581,
"y": 253
},
{
"x": 459,
"y": 253
}
]
},
"text": "A",
"confidence": 0.99
},
{
"property": {
"detectedLanguages": [
{
"languageCode": "en"
}
]
},
"boundingBox": {
"vertices": [
{
"x": 582,
"y": 105
},
{
"x": 638,
"y": 105
},
{
"x": 638,
"y": 253
},
{
"x": 582,
"y": 253
}
]
},
"text": "I",
"confidence": 0.98
},
{
"property": {
"detectedLanguages": [
{
"languageCode": "en"
}
],
"detectedBreak": {
"type": "LINE_BREAK"
}
},
"boundingBox": {
"vertices": [
{
"x": 636,
"y": 105
},
{
"x": 674,
"y": 105
},
{
"x": 674,
"y": 253
},
{
"x": 636,
"y": 253
}
]
},
"text": "G",
"confidence": 0.99
}
],
"confidence": 0.98
}
How can I get only the letters returned instead?
class:
public static void Main(string[] args)
{
string credential_path = @"C:\Users\35385\nodal.json";
System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credential_path);
// Instantiates a client
var client = ImageAnnotatorClient.Create();
// Load the image file into memory
var image = Image.FromFile("vision.jpg");
// Performs label detection on the image file
var response = client.DetectDocumentText(image);
foreach (var page in response.Pages)
{
foreach (var block in page.Blocks)
{
foreach (var paragraph in block.Paragraphs)
{
Console.WriteLine(string.Join("\n", paragraph.Words));
}
}
}
}
The image I passed in was a simple word that I drew out in paint:
After some researching, the following provides me with the word and also a much cleaner output:
Block Text at (183, 105) - (674, 105) - (674, 253) - (183, 253)
Paragraph at (183, 105) - (674, 105) - (674, 253) - (183, 253)
Word: CRAIG
method:
foreach (var page in response.Pages)
{
foreach (var block in page.Blocks)
{
string box = string.Join(" - ", block.BoundingBox.Vertices.Select(v => $"({v.X}, {v.Y})"));
Console.WriteLine($"Block {block.BlockType} at {box}");
foreach (var paragraph in block.Paragraphs)
{
box = string.Join(" - ", paragraph.BoundingBox.Vertices.Select(v => $"({v.X}, {v.Y})"));
Console.WriteLine($" Paragraph at {box}");
foreach (var word in paragraph.Words)
{
Console.WriteLine($" Word: {string.Join("", word.Symbols.Select(s => s.Text))}");
}
}
}
}