Python 将数据从Google存储桶读取到阵列

Python 将数据从Google存储桶读取到阵列,python,tensorflow,google-cloud-platform,google-cloud-storage,Python,Tensorflow,Google Cloud Platform,Google Cloud Storage,如果我想从Google云存储桶(与本地存储相反)读取数据并将其放入类似于此函数的数组中,如何替换此函数 def load_data(img_dir): return np.array( [cv2.imread(os.path.join(img_dir, img), 0).flatten() for img in os.listdir(img_dir) if img.endswith(".jpg")]) 您将需要使用OpenCV的函数以及软件包: from google

如果我想从Google云存储桶(与本地存储相反)读取数据并将其放入类似于此函数的数组中,如何替换此函数

def load_data(img_dir):
  return np.array(
    [cv2.imread(os.path.join(img_dir, img), 0).flatten() for img in os.listdir(img_dir) if img.endswith(".jpg")])       

您将需要使用OpenCV的函数以及软件包:

from google.cloud import storage

import numpy as np
import cv2

def load_data(bucket_name):
    bucket = storage.Client().get_bucket(bucket_name)

    return np.array(
        cv2.imdecode(
            np.asarray(bytearray(blob.download_as_string()), dtype=np.uint8), 0
        ).flatten()
        for blob in bucket.list_blobs()
        if blob.name.endswith(".jpg")
    )