【问题标题】:TypeError when opening images from GCP storage to process them with Vision API从 GCP 存储打开图像以使用 Vision API 处理它们时出现 TypeError
【发布时间】: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


    【解决方案1】:

    所以Image 期待像素的二进制表示。我通过将内容变量更改为以下内容来完成它。

    content = urlopen(file_name).read()
    

    content 现在以二进制模式返回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      相关资源
      最近更新 更多