Python 将从Google Vision API中提取为完整单词的文本分组
我试图通过Google Vision API重现“文档文本检测”示例UI上传器的输出。但是,当我需要将单词分组在一起时,我从中获得的输出仅提供单个字符作为输出 库中是否有一项功能,允许使用“单词”来分组,而不是使用Python中的文档\u TEXT\u DETECT endpoint或Python 将从Google Vision API中提取为完整单词的文本分组,python,image-recognition,google-vision,Python,Image Recognition,Google Vision,我试图通过Google Vision API重现“文档文本检测”示例UI上传器的输出。但是,当我需要将单词分组在一起时,我从中获得的输出仅提供单个字符作为输出 库中是否有一项功能,允许使用“单词”来分组,而不是使用Python中的文档\u TEXT\u DETECT endpoint或image.DETECT\u full\u TEXT()函数 我不是在寻找全文提取,因为我的.jpg文件的视觉结构不符合image.detect\u text()函数的要求 谷歌的示例代码: def detect_
image.DETECT\u full\u TEXT()
函数
我不是在寻找全文提取,因为我的.jpg文件的视觉结构不符合image.detect\u text()
函数的要求
谷歌的示例代码:
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))
谷歌提供的现成样本的样本输出:
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"
}
这种反应来得太晚了。我猜你是在找下面这样的东西
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
该函数返回图像中的完整文本。GCV中有两种类型: 1.文本检测和2。文档文本检测 文本检测用于检测图像中的某些文本。基本上,它给出了在其中找到的文本值。您不能依赖它的准确性,例如,它不能用于读取收据或任何文档数据 然而,文档文本检测的准确性非常好,可以检测文档中每一分钟的细节。在这种方法中,单词彼此分开,例如2017年3月12日将作为0 3/1 2/等及其坐标。这实际上是为了更准确
现在,根据您的问题,您最好使用第一种方法,即文本检测,它将为您提供完整单词及其坐标的结果。这是正确的,但是有没有更简单的方法来获取按行分组的所有边界框,而不是在循环中运行它来查找排序?