如何在Google Cloud Storage中存储的文件上使用cv2.imread? [英] How do I use cv2.imread on a file stored in Google Cloud Storage?

查看:123
本文介绍了如何在Google Cloud Storage中存储的文件上使用cv2.imread?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我在Google云端存储gs://example-bucket/testing_data上的以下网址处存储了一张标题为Sunset.jpg的图片

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

因此图像的完整URL为:

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')

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

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??

推荐答案

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转换为普通网址.

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

这篇关于如何在Google Cloud Storage中存储的文件上使用cv2.imread?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆