How do I use cv2.imread on a file stored in Google Cloud Storage?

喜欢而已 提交于 2020-05-15 19:06:50

问题


Say I have a picture titled sunset.jpg stored at the following URL on google cloud storage gs://example-bucket/testing_data

so the full URL for the image is:

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

If I then do something like:

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

But while this doesn't crash or fail no image is loaded. How do I access/provide the right URL to cv2.imread to do this??


回答1:


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)

To convert gs to normal URL.

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

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

print gcs_url


来源:https://stackoverflow.com/questions/44211221/how-do-i-use-cv2-imread-on-a-file-stored-in-google-cloud-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!