谷歌视觉OCR返回太多信息

时间:2019-11-06 01:38:37

标签: c# api google-api google-vision

我创建了一个简单的类来测试Google视觉OCR API。我正在传递一个包含5个字母的简单图像,该图像应该返回其中包含“ CRAIG”的字符串。尽管此API调用返回了很多额外的信息:

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

我如何只取回信件?

班级:

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


    }

我传入的图像是我用油漆画出的一个简单单词:

a busy cat

2 个答案:

答案 0 :(得分:3)

尝试更改。.

var response = client.DetectDocumentText(image); 

收件人

var response = client.DetectText(image);

说明

以下是GOOGLE CLOUD VISION API文档中的一些信息

Vision API可以检测并提取图像中的文本。有两种支持光学字符识别(OCR)的注释功能:

  • TEXT_DETECTION检测并提取任何图像中的文本。例如,一张照片可能包含路牌或交通标志。 JSON包含提取的整个字符串,单个单词及其边界框。

  • DOCUMENT_TEXT_DETECTION也从图像中提取文本,但是针对密集文本和文档优化了响应。 JSON包括页面,块,段落,单词和中断信息。

答案 1 :(得分:0)

经过研究,以下内容为我提供了一个词,并且输出的内容也更加简洁:

Block Text at (183, 105) - (674, 105) - (674, 253) - (183, 253)
  Paragraph at (183, 105) - (674, 105) - (674, 253) - (183, 253)
    Word: CRAIG

方法:

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