[英]Python only get specific value in json
我在 python 上使用谷歌視覺 api 獲取圖像上的文字,api 返回此 json 文字:
text_annotations {
locale: "en"
description: "SPEED\n"
bounding_poly {
vertices {
x: 6
y: 60
}
vertices {
x: 151
y: 60
}
vertices {
x: 151
y: 117
}
vertices {
x: 6
y: 117
}
}
}
text_annotations {
description: "SPEED"
bounding_poly {
vertices {
x: 6
y: 60
}
vertices {
x: 151
y: 60
}
vertices {
x: 151
y: 117
}
vertices {
x: 6
y: 117
}
}
}
我如何只得到description:
值?
我一直在嘗試: esp = response['description']
但它返回:
Traceback (most recent call last):
File "C:\Users\fkahd\PycharmProjects\username\ai2.py", line 27, in <module>
resp = response['description']
TypeError: 'AnnotateImageResponse' object is not subscriptable
完整代碼:
import io
import os
import json
# Imports the Google Cloud client library
from google.cloud import vision
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r"C:\Users\fkahd\OneDrive\Bureau\zefoy\vision-apixxxxxxxxxxxx.json"
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.abspath(r"C:\Users\fkahd\OneDrive\Bureau\zefoy\captchas\captcha(22).png")
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
# Performs label detection on the image file
response = client.text_detection(image=image) # returns TextAnnotation
resp = response['description']
我通過添加得到了想要的文本:
print(response.text_annotations[0].description)
感謝您的幫助”
python 中的 JSON 被類似地解釋為字典,您可以通過類似的方式獲取 'description' 的值。 這是你必須做的:
var_name = file_name['description']
file_name
應該是打開的 json 文件的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.