来自边界框的 Google Vision Api 文本检测布局信息

Posted

技术标签:

【中文标题】来自边界框的 Google Vision Api 文本检测布局信息【英文标题】:Google Vision Api text detection layout information from bounding box 【发布时间】: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

【问题讨论】:

【参考方案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

【讨论】:

以上是关于来自边界框的 Google Vision Api 文本检测布局信息的主要内容,如果未能解决你的问题,请参考以下文章

来自 Google Vision API 的 OCR 置信度得分

来自 Google Vision API OCR 的响应 400,带有指定图像的 base64 字符串

将 Google Cloud Vision OCR X 和 Y 坐标转换为 bbox 坐标

Google Vision 文本检测返回过多不必要的数据

json Google Cloud Vision API OpenAPI

IndexError:带有 Google Cloud Vision API 的字节数组