【发布时间】:2020-02-25 02:47:12
【问题描述】:
我正在尝试从谷歌云存储打开/获取公共图像以使用 Vision API 进行处理。这是我的代码。
client = vision.ImageAnnotatorClient()
for file_name in list_blobs(BUCKET_NAME):
response = requests.get(file_name)
content = Image.open(BytesIO(response.content))
image = types.Image(content=content)
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:\n')
for label in labels:
print(label.description)
我收到以下错误
Traceback (most recent call last):
File "/Users/gmwill934/PycharmProjects/PDFRekog/functions_vision.py", line 21, in <module>
image = types.Image(content=content)
TypeError: <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=800x449 at 0x10BA5AD50> has type JpegImageFile, but expected one of: bytes
我很难从网络上打开图片,当我在本地保存图片并使用with open() as image_file....打开时它可以工作
[编辑]
所以我已经修改了我的代码,我将图像转换为bytes 类型,代码按预期运行但没有显示标签。
for file_name in list_blobs(BUCKET_NAME):
image = imageio.imread(file_name)
my_array = np.asarray(image)
content = my_array.tobytes()
image = types.Image(content=content)
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:\n')
for label in labels:
print(label.description)
numpy 数组可以转换为字节吗?还是我还缺少其他东西。
【问题讨论】:
标签: python google-cloud-storage python-imaging-library google-cloud-vision