Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading images in google colab

Tags:

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?

like image 829
djeetee Avatar asked Mar 09 '18 05:03

djeetee


People also ask

How do I load image dataset folder in Google Colab?

Step 2: Select any Dataset and Click on the Download. Step 3: The downloaded file will be in Zip form, Unzip it. Step 4: Upload Your Dataset file or folder to Google Colab Notebook. On clicking on Upload your folder/file you will get an option to upload your file/ folder as the given image illustrate.

How do I display a PNG image in Colab?

In Google Colab, open the file browser icon (left nav bar) and navigate to usr/local/share/jupyter/nbextensions as described above. Click the ellipsis menu on the nbextensions folder > Upload and select your image to upload.

Can colab notebooks contain images?

A Colab notebook consists of text cells, code cells, and outputs of code cells. Text cells are written in Markdown, a markup language we'll learn about in the next section. This means they can contain formatted text, images, HTML, LaTeX, and more. Code cells are written in Python.


1 Answers

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.

like image 163
korakot Avatar answered Sep 19 '22 13:09

korakot