【问题标题】:Getting vertices where google vision API found words获取 google vision API 找到单词的顶点
【发布时间】:2018-12-15 20:34:13
【问题描述】:

我正在使用 Google Vision API。

我想获取 google vision 找到一个单词块的矩形的顶点((x,y)位置)。到目前为止,我正在从谷歌客户端获取文本。

credentials = service_account.Credentials.from_service_account_file("/api-key.json")
client = vision.ImageAnnotatorClient(credentials=credentials)

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

#call api
image = types.Image(content=content)
response = client.document_text_detection(image=image)
document = response.full_text_annotation

我想要的是在document.text 中获取每个单词块的顶点。

【问题讨论】:

    标签: python vision


    【解决方案1】:

    从 google documentation,您可以找到 API 响应的结构(BLOCK、PARAGRAPH、...)以及如何检索相应的顶点。

    特别是这个功能:

    def get_document_bounds(image_file, feature):
    """Returns document bounds given an image."""
    client = vision.ImageAnnotatorClient()
    
    bounds = []
    
    with io.open(image_file, 'rb') as image_file:
        content = image_file.read()
    
    image = types.Image(content=content)
    
    response = client.document_text_detection(image=image)
    document = response.full_text_annotation
    
    # Collect specified feature bounds by enumerating all document features
    for page in document.pages:
        for block in page.blocks:
            for paragraph in block.paragraphs:
                for word in paragraph.words:
                    for symbol in word.symbols:
                        if (feature == FeatureType.SYMBOL):
                            bounds.append(symbol.bounding_box)
    
                    if (feature == FeatureType.WORD):
                        bounds.append(word.bounding_box)
    
                if (feature == FeatureType.PARA):
                    bounds.append(paragraph.bounding_box)
    
            if (feature == FeatureType.BLOCK):
                bounds.append(block.bounding_box)
    
        if (feature == FeatureType.PAGE):
            bounds.append(block.bounding_box)
    
    # The list `bounds` contains the coordinates of the bounding boxes.
    return bounds
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 2017-11-29
      • 2020-10-02
      相关资源
      最近更新 更多