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

som*_*ode 2 google-cloud-storage google-cloud-platform google-cloud-datalab

假设我在谷歌云存储 gs://example-bucket/testing_data 上的以下 URL 中存储了一张名为 sun.jpg 的图片

所以图片的完整网址是:

gs://example-bucket/testing_data/sunset.jpg
Run Code Online (Sandbox Code Playgroud)

如果我再做类似的事情:

image = cv2.imread('gs://example-bucket/testing_data/sunset.jpg')
Run Code Online (Sandbox Code Playgroud)

但是,虽然这不会崩溃或失败,但不会加载任何图像。我如何访问/提供正确的 URL 到 cv2.imread 来做到这一点?

VK3*_*321 5

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)
Run Code Online (Sandbox Code Playgroud)

将 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
Run Code Online (Sandbox Code Playgroud)