如何对 Google Cloud Storage 中存储的文件使用 cv2.imread?

2024-04-20

假设我有一张标题为 Sunset.jpg 的图片存储在 Google 云存储 gs://example-bucket/testing_data 上的以下 URL 中

所以图像的完整 URL 是:

gs://example-bucket/testing_data/sunset.jpg

如果我然后做类似的事情:

image = cv2.imread('gs://example-bucket/testing_data/sunset.jpg')

但是,虽然这不会崩溃或失败,但不会加载任何图像。如何访问/提供 cv2.imread 的正确 URL 来执行此操作?


import cv2
import numpy as np
import urllib

url = "https://i.stack.imgur.com/AVWX7.jpg";
resp = urllib.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)

将 gs 转换为普通 URL。

bucket = 'example-bucket'
file = 'sunset.jpg'

gcs_url = 'https://%(bucket)s.storage.googleapis.com/%(file)s' % {'bucket':bucket, 'file':file}

print gcs_url
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何对 Google Cloud Storage 中存储的文件使用 cv2.imread? 的相关文章

随机推荐