google vision OCR返回的信息过多

问题描述 投票:0回答:2

我创建了一个简单的类来测试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
}

我如何只取回信件?

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


    }

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

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmliYi5jby85SGo3cWI1L3Zpc2lvbi5qcGcifQ==” alt =“忙碌的猫”>

c# api google-api google-vision
2个回答
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))}");
                        }
                    }
                }
            }

0
投票

尝试更改..

var response = client.DetectDocumentText(image); 

收件人

var response = client.DetectText(image);

说明

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.