【问题标题】:Google cloud vision API error reading pdf谷歌云视觉API错误阅读pdf
【发布时间】:2021-08-10 19:35:26
【问题描述】:

我目前正在尝试使用谷歌云视觉 API 处理一个大型 pdf 文档。阅读文档时,我收到一条错误消息,上面写着“json_format.Parse(错误”。我在下面附上了我的代码。我该如何解决这个问题? Code

【问题讨论】:

  • 请以文本格式发布您的代码,以便社区成员轻松复制。还包括错误的完整堆栈跟踪。

标签: google-cloud-vision


【解决方案1】:

您在该行代码中遇到错误,因为您试图将带有 type: json_string 和一个不存在的对象 vision.types.AnnotateFilesResponse() 传递给 json_format.Parse()这需要:

google.protobuf.json_format.Parse(text, message,ignore_unknown_fields=False, descriptor_pool=None) 解析 JSON 将协议消息表示为消息。

参数:

  • 文本 - 消息 JSON 表示。
  • message – 要合并到的协议缓冲区消息。
  • ignore_unknown_fields – 如果为 True,则不要为未知字段引发错误。
  • descriptor_pool – 用于解析类型的描述符池。如果没有使用 默认。

返回作为参数传递的相同消息。

引发:: ParseError: 关于 JSON 解析问题。

由于您的目标是读取来自 async_batch_annotate_files() 的响应,因此来自此方法的 JSON 响应将保存到定义的 Cloud Storage Bucket 输出位置。您可以通过将json_string 中的数据转换为字典来读取和解析数据。然后,您可以通过参考AnnotateFileResponse reference 在字典中按自己的方式工作。使用下面的代码:

output = blob_list[0]
json_string = output.download_as_string()
response = json.loads(json_string)
first_page_response = response['responses'][0]
annotation = first_page_response['fullTextAnnotation']

print('Full text:\n')
print(annotation['text'])

注意:请确保您获得正确的 JSON 响应文件 (output = blob_list[0]),否则解析结果会产生错误。

【讨论】:

    猜你喜欢
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多