-1

In local machine, I am able to extract the feature of images and store the corresponding .npy file in a different folder.

from PIL import Image
from feature_extractor import FeatureExtractor
from pathlib import Path
import numpy as np

if __name__ == '__main__':
    fe = FeatureExtractor()

    for img_path in sorted(Path("./static/img").glob("*.jpg")):
        print(img_path)  # e.g., ./static/img/xxx.jpg
        feature = fe.extract(img=Image.open(img_path))
        feature_path = Path("./static/feature") / (img_path.stem + ".npy")  # e.g., ./static/feature/xyz.npy
        np.save(feature_path, feature)

Now I want to host this in google cloud. But for this I need to read and write the images from Google Cloud Storage. Is there any library like Path for GCS ?

davidism
  • 121,510
  • 29
  • 395
  • 339
Durgendra
  • 145
  • 2
  • 11

1 Answers1

3

There is official client lib for this pip install --upgrade google-cloud-storage

See: https://cloud.google.com/storage/docs/reference/libraries#client-libraries-install-python


Edit: source: https://stackoverflow.com/a/22399617/7888973

If you want to list flies within a GCS bucket:

from google.cloud import storage

client = storage.Client()
for blob in client.list_blobs('bucketname', prefix='abc/myfolder'):
  print(str(blob))
Lucjan
  • 119
  • 1
  • 3
  • I tried using this library (google-cloud-storage) but couldn't solve this issue. I was able to connect with GCS but I had to download the image first and then upload the feature array. – Durgendra Nov 29 '21 at 07:34
  • I don't know how exactly FeatureExtractor.extract works but I imagine you need to have the image on your machine in order to compute it. So you have to download it first, then extract and then upload .npy back again. – Lucjan Nov 29 '21 at 08:01
  • If you are asking how to download and upload from GCS using python: Look at official GCS python lib GitHub example usage: https://github.com/googleapis/python-storage/#example-usage ||| |1. Retrieve an existing bucket (`bucket = client.get_bucket('bucket-id')`) |2. download orginal file (`blob = bucket.get_blob('remote/path/filename.img')`) |3. FeatureExtractor.extract |4. upload npy file to GCS (`new_blob = bucket.blob('remote/path/filename.npy')` `new_blob.upload_from_filename(filename='/local/path/filename.npy')`) – Lucjan Nov 29 '21 at 08:17
  • Yeah. I am also trying with this method. I am able to read the image and extract the feature too. Right now, I am getting issue in uploading the .npy file in GSC. Let's see. – Durgendra Nov 29 '21 at 08:17