【问题标题】:Google Vision Api text detection layout information from bounding box来自边界框的 Google Vision Api 文本检测布局信息
【发布时间】:2017-04-20 11:00:46
【问题描述】:

我正在尝试使用 google vision api 对我的图像执行 OCR。 API 调用的 Json 输出返回带有边界框信息的已识别单词。

谁能告诉我如何使用这个边界框信息对我的图像进行布局分析?

如果有一个库将其作为输入并返回句子而不是单词?

{
  "description": "Ingredients:",
  "boundingPoly": {
    "vertices": [
      {
        "x": 14,
        "y": 87
      },
      {
        "x": 53,
        "y": 87
      },
      {
        "x": 53,
        "y": 98
      },
      {
        "x": 14,
        "y": 98
      }
    ]
  }
},
{
  "description": "Chicken",
  "boundingPoly": {
    "vertices": [
      {
        "x": 55,
        "y": 87
      },
      {
        "x": 77,
        "y": 87
      },
      {
        "x": 77,
        "y": 98
      },
      {
        "x": 55,
        "y": 98
      }
    ]
  }
},

例如在上面的 json 中,“Ingredients:”“Chicken”这两个词在同一行。是否有图书馆可以开箱即用地为我提供这些信息?

用于 OCR 的图像source image

【问题讨论】:

    标签: image-processing ocr google-vision


    【解决方案1】:

    尝试使用 Word 或任何其他可让您旋转的工具来旋转图像。 在我的情况下,它产生了正确的答案,即连续读取行中的所有内容。

    【讨论】:

      【解决方案2】:

      有几个client libraries 可用于获取句子而不是单词。 github 中也有官方可用的示例。例如你可以看到here go语言示例文件。 detect.go 包含下一个按块输出文本的函数:

          // detectDocumentText gets the full document text from the Vision API for an image at the given file path.
      func detectDocumentTextURI(w io.Writer, file string) error {
          ctx := context.Background()
      
          client, err := vision.NewImageAnnotatorClient(ctx)
          if err != nil {
              return err
          }
      
          image := vision.NewImageFromURI(file)
          annotation, err := client.DetectDocumentText(ctx, image, nil)
          if err != nil {
              return err
          }
      
          if annotation == nil {
              fmt.Fprintln(w, "No text found.")
          } else {
              fmt.Fprintln(w, "Document Text:")
              fmt.Fprintf(w, "%q\n", annotation.Text)
      
              fmt.Fprintln(w, "Pages:")
              for _, page := range annotation.Pages {
                  fmt.Fprintf(w, "\tConfidence: %f, Width: %d, Height: %d\n", page.Confidence, page.Width, page.Height)
                  fmt.Fprintln(w, "\tBlocks:")
                  for _, block := range page.Blocks {
                      fmt.Fprintf(w, "\t\tConfidence: %f, Block type: %v\n", block.Confidence, block.BlockType)
                      fmt.Fprintln(w, "\t\tParagraphs:")
                      for _, paragraph := range block.Paragraphs {
                          fmt.Fprintf(w, "\t\t\tConfidence: %f", paragraph.Confidence)
                          fmt.Fprintln(w, "\t\t\tWords:")
                          for _, word := range paragraph.Words {
                              symbols := make([]string, len(word.Symbols))
                              for i, s := range word.Symbols {
                                  symbols[i] = s.Text
                              }
                              wordText := strings.Join(symbols, "")
                              fmt.Fprintf(w, "\t\t\t\tConfidence: %f, Symbols: %s\n", word.Confidence, wordText)
                          }
                      }
                  }
              }
          }
      
          return nil
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-02
        • 2018-06-01
        • 1970-01-01
        • 2017-10-20
        • 2019-04-24
        • 2019-02-25
        • 1970-01-01
        • 1970-01-01
        • 2017-11-28
        相关资源
        最近更新 更多