python-3.x - 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
,但我不明白为什么我没有得到它们。要将它们转换为非规范化形式,我将规范化顶点乘以高度和宽度,但结果很糟糕,盒子没有很好地定位。
解决方案
要将 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 日之后,将不再提供旧型号。
推荐阅读
- java - Selenium chromedriver 无法点击 SVG 元素
- html - 将采用输入图像的 HTML 页面转换为采用文本的 HTML 页面
- javascript - MailApp.sendEmail 谷歌脚本函数
- oauth-2.0 - 如何使用授权码在 Postman 中模拟 oAuth 2.0 流程
- php - 是否有任何选项可以检查 API 是否需要超过 10 秒才能在 PHP 中调用?
- php - 在 woocommerce 通过空对象的情况下使用条纹进行支付测试后出错
- keras - cnn-keras fit_generator 回调中的值错误
- mongodb - 我正在尝试为一组对象创建架构,但不确定如何对后端执行此操作
- reactjs - 向文本框属性中的状态添加动态值
- unity3d - 无法使用 IL2CPP 构建