我目前正在尝试使用Google的python视觉库。但是我目前仍停留在如何从网络上读取图像上。到目前为止,我已经在下面进行了介绍。我的问题是内容似乎总是空的,当我使用PyCharm检查时,它说它仅包含b''
。
如何打开该图像以便将其用于Google的图书馆?
from google.cloud import vision
from google.cloud.vision import types
from urllib import request
import io
client = vision.ImageAnnotatorClient.from_service_account_json('cred.json')
url = "https://cdn.getyourguide.com/img/location_img-59-1969619245-148.jpg"
img = request.urlopen(url)
with io.open('location_img-59-1969619245-148.jpg','rb') as fhand:
content = fhand.read()
image = types.Image(content=content)
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description)