如何将 google 视觉 API 响应保存到可处理文件

How to save the google vision API response to a handable file

我正在尝试从一些工单中提取 google 云视觉 OCR API 提供给我的所有信息。我可以将文本保存在 .txt 中,但响应的其余部分 response = client.text_detection(image=image) 我不知道如何保存它。

谢谢

我遵循了本教程 Quickstart: Using client libraries

然后,假设您获得了 response,您可以使用以下代码将其保存到文件中:

text_file = open("sample.txt", "w")
text_file.write(str(response))
text_file.close()

终于在控制台上测试了:

cat sample.txt

label_annotations {
  mid: "/m/01yrx"
  description: "Cat"
  score: 0.9895679950714111
  topicality: 0.9895679950714111
}
label_annotations {
  mid: "/m/0307l"
  description: "Felidae"
  score: 0.9541760087013245
  topicality: 0.9541760087013245
}
label_annotations {
  mid: "/m/01l7qd"
  description: "Whiskers"
  score: 0.953809380531311
  topicality: 0.953809380531311
}
label_annotations {
  mid: "/m/01k74n"
  description: "Facial expression"
  score: 0.9447915554046631
  topicality: 0.9447915554046631
}