【问题标题】:Upload ndarray(image in OpenCV) on to google cloud storage as a .jpg or .png将 ndarray(OpenCV 中的图像)以 .jpg 或 .png 格式上传到谷歌云存储
【发布时间】:2018-09-14 00:51:38
【问题描述】:

我有类似的问题,例如How to upload a bytes image on Google Cloud Storage from a Python script

我试过了

from google.cloud import storage
import cv2
from tempfile import TemporaryFile
import google.auth
credentials, project = google.auth.default()
client = storage.Client()
# https://console.cloud.google.com/storage/browser/[bucket-id]/
bucket = client.get_bucket('document')
# Then do other things...
image=cv2.imread('/Users/santhoshdc/Documents/Realtest/15.jpg')
with TemporaryFile() as gcs_image:
    image.tofile(gcs_image)
    blob = bucket.get_blob(gcs_image)
    print(blob.download_as_string())
    blob.upload_from_string('New contents!')
    blob2 = bucket.blob('document/operations/15.png')

    blob2.upload_from_filename(filename='gcs_image')

这是出现的错误

> Traceback (most recent call last):   File
> "/Users/santhoshdc/Documents/ImageShapeSize/imageGcloudStorageUpload.py",
> line 13, in <module>
>     blob = bucket.get_blob(gcs_image)   File "/Users/santhoshdc/.virtualenvs/test/lib/python3.6/site-packages/google/cloud/storage/bucket.py",
> line 388, in get_blob
>     **kwargs)   File "/Users/santhoshdc/.virtualenvs/test/lib/python3.6/site-packages/google/cloud/storage/blob.py",
> line 151, in __init__
>     name = _bytes_to_unicode(name)   File "/Users/santhoshdc/.virtualenvs/test/lib/python3.6/site-packages/google/cloud/_helpers.py",
> line 377, in _bytes_to_unicode
>     raise ValueError('%r could not be converted to unicode' % (value,)) ValueError: <_io.BufferedRandom name=7> could not be
> converted to unicode

谁能指导我出了什么问题或我做错了什么?

【问题讨论】:

  • @A.Queue 请查看此

标签: python google-cloud-platform google-cloud-storage opencv3.0


【解决方案1】:

根据@A.Queue in的建议(29 天后删除)

from google.cloud import storage
import cv2
from tempfile import TemporaryFile

client = storage.Client()

bucket = client.get_bucket('test-bucket')
image=cv2.imread('example.jpg')
with TemporaryFile() as gcs_image:
    image.tofile(gcs_image)
    gcs_image.seek(0)
    blob = bucket.blob('example.jpg')
    blob.upload_from_file(gcs_image)

文件已上传,但上传numpy ndarray 不会在google-cloud-storage 上保存为图像文件

PS:

numpy array在保存前必须转换成任意图片格式。

这个比较简单,使用创建的tempfile来存储图片,代码如下。

with NamedTemporaryFile() as temp:

    #Extract name to the temp file
    iName = "".join([str(temp.name),".jpg"])

    #Save image to temp file
    cv2.imwrite(iName,duplicate_image)

    #Storing the image temp file inside the bucket
    blob = bucket.blob('ImageTest/Example1.jpg')
    blob.upload_from_filename(iName,content_type='image/jpeg')

    #Get the public_url of the saved image 
    url = blob.public_url

【讨论】:

  • 问题最后解决了吗?我问是因为在另一个问题中你写的是路径有问题,但在这里你说它不可读。
  • @A.Queue 是的,问题已解决。 numpy array 直接上传不会以图像格式存储,因此不可读。我已经更新了numpy array 的代码,首先转换并存储在tempfile 然后上传
  • 但是上传后需要删除文件。否则磁盘可能会过满
【解决方案2】:

你打电话给blob = bucket.get_blob(gcs_image) 这毫无意义。 get_blob() 应该得到一个字符串参数,即你想要得到的 blob 的名称。一个名字。但是你传递了一个文件对象。

我提出这个代码:

with TemporaryFile() as gcs_image:
    image.tofile(gcs_image)
    gcs_image.seek(0)
    blob = bucket.blob('documentation-screenshots/operations/15.png')
    blob.upload_from_file(gcs_image)

【讨论】:

  • Traceback (most recent call last): File "/Users/santhoshdc/Documents/ImageShapeSize/imageGcloudStorageUpload.py", line 34, in &lt;module&gt; blob2.upload_from_filename(filename='gcs_image') File "/Users/santhoshdc/.virtualenvs/test/lib/python3.6/site-packages/google/cloud/storage/blob.py", line 1021, in upload_from_filename with open(filename, 'rb') as file_obj: FileNotFoundError: [Errno 2] No such file or directory: 'gcs_image'。这是我在删除这三行时遇到的错误
  • Urgs。 filename='gcs_image' 毫无意义。我将添加我认为可能适用于我的答案的代码。等一下。
  • @PrashantPanwar 我添加了应该可以解决问题的示例代码。
  • 该程序似乎运行良好,没有错误,但没有任何文件上传到存储桶中。尝试在终端中通过gsutil 上传图片似乎上传了图片。不知道哪里出了问题?
  • 尝试运行此代码pastebin.com/Jr6k3SW2。它几乎是相同的,但没有google.auth,这在此处是不必要的,因为 google.cloud 正在后台处理所有内容。这将是一个很好的参考点,因为我已经尝试过它并且它有效。
猜你喜欢
  • 2015-02-28
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-22
  • 2013-11-30
  • 1970-01-01
相关资源
最近更新 更多