【问题标题】:Grouping text extracted as full words from the Google Vision API对从 Google Vision API 中提取为完整单词的文本进行分组
【发布时间】:2017-11-29 10:42:08
【问题描述】:

我正在尝试通过 Google Vision API 重现“文档文本检测”示例 UI 上传器的输出。但是,当我需要将单词组合在一起时,我从 sample code 获得的输出仅提供单个字符作为输出。

库中是否有允许按“单词”分组而不是从 DOCUMENT_TEXT_DETECT 端点或 Python 中的 image.detect_full_text() 函数进行分组的功能?

我不是在寻找全文提取,因为我的 .jpg 文件在视觉上的结构不符合 image.detect_text() 函数的要求。

Google 的示例代码:

def detect_document(path):
    """Detects document features in an image."""
    vision_client = vision.Client()

    with io.open(path, 'rb') as image_file:
        content = image_file.read()

    image = vision_client.image(content=content)

    document = image.detect_full_text()

    for page in document.pages:
        for block in page.blocks:
            block_words = []
            for paragraph in block.paragraphs:
                block_words.extend(paragraph.words)

            block_symbols = []
            for word in block_words:
                block_symbols.extend(word.symbols)

            block_text = ''
            for symbol in block_symbols:
                block_text = block_text + symbol.text

            print('Block Content: {}'.format(block_text))
            print('Block Bounds:\n {}'.format(block.bounding_box))

Google 提供的现成示例的示例输出:

property {
  detected_languages {
    language_code: "mt"
  }
}
bounding_box {
  vertices {
    x: 1193
    y: 1664
  }
  vertices {
    x: 1206
    y: 1664
  }
  vertices {
    x: 1206
    y: 1673
  }
  vertices {
    x: 1193
    y: 1673
  }
}
symbols {
  property {
    detected_languages {
      language_code: "en"
    }
  }
  bounding_box {
    vertices {
      x: 1193
      y: 1664
    }
    vertices {
      x: 1198
      y: 1664
    }
    vertices {
      x: 1198
      y: 1673
    }
    vertices {
      x: 1193
      y: 1673
    }
  }
  text: "P"
}
symbols {
  property {
    detected_languages {
      language_code: "en"
    }
    detected_break {
      type: LINE_BREAK
    }
  }
  bounding_box {
    vertices {
      x: 1200
      y: 1664
    }
    vertices {
      x: 1206
      y: 1664
    }
    vertices {
      x: 1206
      y: 1673
    }
    vertices {
      x: 1200
      y: 1673
    }
  }
  text: "M"
}


block_words
Out[47]: 
[property {
   detected_languages {
     language_code: "en"
   }
 }
 bounding_box {
   vertices {
     x: 1166
     y: 1664
   }
   vertices {
     x: 1168
     y: 1664
   }
   vertices {
     x: 1168
     y: 1673
   }
   vertices {
     x: 1166
     y: 1673
   }
 }
 symbols {
   property {
     detected_languages {
       language_code: "en"
     }
   }
   bounding_box {
     vertices {
       x: 1166
       y: 1664
     }
     vertices {
       x: 1168
       y: 1664
     }
     vertices {
       x: 1168
       y: 1673
     }
     vertices {
       x: 1166
       y: 1673
     }
   }
   text: "2"
 }

【问题讨论】:

    标签: python image-recognition google-vision


    【解决方案1】:

    此回复来晚了。我猜你正在寻找类似下面的东西。

    def parse_image(image_path=None):
        """
        Parse the image using Google Cloud Vision API, Detects "document" features in an image
        :param image_path: path of the image
        :return: text content
        :rtype: str
        """
    
        client = vision.ImageAnnotatorClient()
        response = client.text_detection(image=open(image_path, 'rb'))
        text = response.text_annotations
        del response
    
        return text[0].description
    

    函数返回图像中的完整文本。

    【讨论】:

    • 这是真的,但是有没有更简单的方法来获取每行分组的所有边界框,而不是在循环中运行它来查找排序?
    【解决方案2】:

    GCV 有两种类型: 1.文本检测和2.文档文本检测

    文本检测用于检测图像中的某些文本。基本上它给出了在其中找到的文本值。您不能依赖它的准确性,例如它不能用于读取收据或任何文档数据。

    然而,文档文本检测的准确性非常好,可以检测文档中的每一个细节。在这种方法中,单词彼此分开,例如2017 年 3 月 12 日将作为 0 3 / 1 2 / 等连同其坐标一起出现。这实际上是为了提高准确性。

    现在根据您的问题,您最好使用第一种方法,即文本检测,它会为您提供包含完整单词及其坐标的结果。

    【讨论】:

      猜你喜欢
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      • 2020-03-20
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多