首页 > 解决方案 > google vision API 返回空边界框顶点,而不是返回 normalised_vertexes

问题描述

我正在使用vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION在 pdf 文档中提取一些密集的文本。这是我的代码:

from google.cloud import vision

def extract_text(bucket, filename, mimetype):
    print('Looking for text in PDF {}'.format(filename))
    # BATCH_SIZE; How many pages should be grouped into each json output file.
    # """OCR with PDF/TIFF as source files on GCS"""

    # Detect text
    feature = vision.types.Feature(
        type=vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)
    # Extract text from source bucket
    gcs_source_uri = 'gs://{}/{}'.format(bucket, filename)
    gcs_source = vision.types.GcsSource(uri=gcs_source_uri)
    input_config = vision.types.InputConfig(
        gcs_source=gcs_source, mime_type=mimetype)

    request = vision.types.AnnotateFileRequest(features=[feature], input_config=input_config)

    print('Waiting for the ORC operation to finish.')
    ocr_response = vision_client.batch_annotate_files(requests=[request])

    print('OCR completed.')

在响应中,我期望找到ocr_response.responses[1...n].pages[1...n].blocks[1...n].bounding_box一个vertices填写好的列表,但是这个列表是空的。相反,有一个normalized_vertices列表是 0 到 1 之间的归一化顶点。为什么会这样?为什么vertices结构是空的?我正在关注这篇文章,那里的作者使用vertices,但我不明白为什么我没有得到它们。要将它们转换为非规范化形式,我将规范化顶点乘以高度和宽度,但结果很糟糕,盒子没有很好地定位。

标签: python-3.xgoogle-cloud-platformgoogle-cloud-visiongoogle-vision

解决方案


要将 Normalized Vertex 转换为 Vertex,您应该将 NormalizedVertex 的 x 字段与宽度值相乘以获得 Vertex 的 x 字段,并将 NormalizedVertex 的 y 字段与高度值相乘以获得顶点的 y。

之所以得到 Normalized Vertex,Medium 文章的作者之所以得到 Vertex,是因为 TEXT_DETECTION 和 DOCUMENT_TEXT_DETECTION 模型从 2020 年 5 月 15 日开始升级到更新版本,而 medium 文章是在 2018 年 12 月 25 日写的。

要将旧模型用于结果,您必须在 Feature 对象的模型字段中指定“builtin/legacy_20190601”以获取旧模型结果。

但谷歌的文档提到,在 2020 年 11 月 15 日之后,将不再提供旧型号。


推荐阅读