TypeError: get() ожидал сообщение dict или protobuf, получил Python

Программы на Python
Anonymous
TypeError: get() ожидал сообщение dict или protobuf, получил

Сообщение Anonymous »

Я пытаюсь использовать Google Cloud Vision API для своего проекта, и вот код, в котором возникает ошибка:

Код: Выделить всё

from google.cloud import vision
from urllib.request import Request, urlopen
import os

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "vision-421011-15870c6b5738.json"

def detect_labels(path):

client = vision.ImageAnnotatorClient()

#with urllib.request.urlopen(path) as image_file:
#    content = image_file.read()

#image = vision.Image(content=content)
#image = urlopen(path).read()
req = Request(
url=path,
headers={'User-Agent': 'Mozilla/5.0'}
)
image = urlopen(req).read()

response = client.label_detection(image=image)
labels = response.label_annotations
print("Labels:")

for label in labels:
print(label.description)

if response.error.message:
raise Exception(
"{}\nFor more info on error messages, check: "
"https://cloud.google.com/apis/design/errors".format(response.error.message)
)

print(detect_labels("https://cdn.discordapp.com/attachments/1165740233837531216/1231434541437026324/75harmonize.png?ex=6636f1c9&is=66247cc9&hm=0f543ddaf6532a1c6874584efe808b4aed125c5cdd53bd2833af2f58a9ade2b4&"))

Это простой код, который я взял из Интернета и слегка изменил, но при его запуске получаю следующую ошибку:

Код: Выделить всё

TypeError: get() expected a dict or protobuf message, got 
Как устранить эту ошибку? Я использую Python 3.11

Подробнее здесь: https://stackoverflow.com/questions/783 ... lass-bytes

Вернуться в «Python»