Loading images in google colab

djeetee picture djeetee · Mar 9, 2018 · Viewed 82.9k times · Source

My Jupyter Notebook has the following code to upload an image to Colab:

from google.colab import files
uploaded = files.upload()

I get prompted for the file. Which gets uploaded.

I verify that the file upload was successful using:

!ls

and I see that it's there.

I check the current working directory using:

import os
os.getcwd()

and it tells me that it is /content

now, any of the following calls...

cv2.imread(img_path, 1)
cv2.imread(img_path, 0)
cv2.imread(img_path)

fails to load the file.

they also fail whether i'm using just the file name or the full path.

Any thoughts on what is going on?

Answer

korakot picture korakot · Mar 9, 2018

Use this function to upload files. It will SAVE them as well.

def upload_files():
  from google.colab import files
  uploaded = files.upload()
  for k, v in uploaded.items():
    open(k, 'wb').write(v)
  return list(uploaded.keys())

Update

Now (sep 2018), the left pane has a "Files" tab that let you browse files and upload files easily. You can also download by just double click the file names.